1 Writing a Good Program 8. Elementary Data Structure.

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Advertisements

Ch. 8 Functions.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
Writing a Good Program 6. Pointers and Arrays
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Lecture No.01 Data Structures Dr. Sohail Aslam
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
Chapter 9 Pointers Fall 2005 Csc 125 Introduction to C++
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9: Pointers.
Pointers Pointer a data type stores a memory address points to whatever the memory location contains A pointer is a variable that can store a memory address.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
Pointers OVERVIEW.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
Dynamic memory allocation and Pointers Lecture 4.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
C HAPTER 03 Pointers Compiled by Dr. Mohammad Alhawarat.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
1  Lecture 12 – Pointer FTMK, UTeM – Sem /2014.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Linked lists.
Pointers Revisited What is variable address, name, value?
CSCE 210 Data Structures and Algorithms
Programmazione I a.a. 2017/2018.
Pointers and Dynamic Variables
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Dynamic Memory Allocation Reference Variables
Chapter 16-2 Linked Structures
understanding memory usage by a c++ program
Popping Items Off a Stack Lesson xx
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Pointers & Functions.
Programming Abstractions
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
Dynamic Memory.
Pointers & Functions.
The Stack.
Standard Version of Starting Out with C++, 4th Edition
Linked lists.
Pointers, Dynamic Data, and Reference Types
Data Structures & Programming
Presentation transcript:

1 Writing a Good Program 8. Elementary Data Structure

2 Linked Lists In using array, one needs to define the size beforehand. If the size is too big, it’s a waste of memory. If the size is too small, it may even crash the system. If we want the number of items in an array to be dynamically defined during the program execution, linked lists are more useful. A linked list is a data structure that consists of a number items, with each item having a pointer points to the location of the next item. Computer Programming and Basic Software Engineering 8. Elementary Data Structure

3 Linked List Free Store or the heap Global Name Space Code Space The Stack CAT 0 CAT 1... CAT 499 A pointer Head is kept to point to the beginning memory location of a linked list of CAT objects in Free Store.. 0. A null pointer means the end of the list Computer Programming and Basic Software Engineering 8. Elementary Data Structure Address of the next item

4. Linked lists have 3 forms Singly linked Doubly linked Tree Data... Head 0 Singly linked Data... Head 0 Doubly linked... 0 Data Tree Head Computer Programming and Basic Software Engineering 8. Elementary Data Structure

5 Linked Lists - Add an item Data... Head 0 Data. 0 Append an item to the end Data.. Head Data. 0 Insert an item in between Data. Computer Programming and Basic Software Engineering 8. Elementary Data Structure

6 Data... Head 0 Data. 0 Remove the last item Linked Lists - Remove an item Data. 0.. Head Remove an item in between Data. Computer Programming and Basic Software Engineering 8. Elementary Data Structure

7 Linked Lists - How can it be done? #include using namespace std; class CAT //Use a number CatNum to represent a cat { public: CAT() {pNext=0;} ~CAT(){;} int GetNum() const {return CatNum;} void SetNum(int num) {CatNum = num;} CAT * GetNext() {return pNext;} void SetNext(CAT *pN) {pNext = pN;} private: int CatNum; CAT *pNext; }; A linked list of CAT objects is to be created Record the cat number Record the pointer of the next cat Computer Programming and Basic Software Engineering 8. Elementary Data Structure All functions implemented

8 CAT * create(int n) { //Create a linked list CAT *pH,*pT,*pL; int i; pH = new CAT; pL = pH; pL->SetNum(0); for (i=1; i<n; i++) {pT = new CAT; pL->SetNext(pT); pL = pT; pL->SetNum(i); } //pL points to last item return pH; } if n = 3 pL. pH pT Computer Programming and Basic Software Engineering 8. Elementary Data Structure

9 int main() { int num; CAT *pHead, *pTemp; cout << "How many Cats: "; cin >> num; pHead = create(num); pTemp = pHead; do { cout GetNum() << endl; pTemp = pTemp->GetNext(); } while (pTemp!=0); return 0; } Ask the user to determine the number of cats As the number of CAT is dynamically determined, we cannot use array approach (array index is not a variable) Call create() to create a linked list of CAT objects Computer Programming and Basic Software Engineering 8. Elementary Data Structure Print out all the CAT number recorded in each CAT object

10 Computer Programming and Basic Software Engineering 8. Elementary Data Structure Add an item to the end // Add an item to the end // Return the head pointer CAT * addend(CAT *pH) { CAT *pCurr, *pPrev, *pCat; int j=0; // CatNum to be written pCurr = pH; do // find the item {pPrev = pCurr; j++; pCurr = pCurr->GetNext(); } while (pCurr != 0); // pPrev is pointing last item pCat = new CAT; pPrev->SetNext(pCat); pCat->SetNum(j); // write CatNum return pH; } pCurr. pH pPrev pCurr 0. pCat pPrev 3 Go from head to end, then add item. How about inserting an item somewhere inside?

11 Computer Programming and Basic Software Engineering 8. Elementary Data Structure Remove an item // Remove the second item n=1 // Return the head pointer CAT * remove(int n, CAT *pH) { CAT *pCurr, *pPrev, *pCat; int i=0; pCurr = pH; do // find the item { pPrev = pCurr; pCurr = pPrev->GetNext(); i++; } while (i<n); // pCurr is pointing the deleted item pCat=pCurr->GetNext(); pPrev->SetNext(pCat); delete pCurr; return pH; } if n = 1 pCurr pCat. pH pPrev How about removing the first item (pointed by pH )? pCurr

12 Exercise 8.1 Redesign the program in p. 9 such that every item contains the name of the cat, which is a string. After creating the linked list, it will continuously ask the user to show all items, insert an item, delete an item in the linked list, or quit. We need to modify the main() and write two functions. CAT * insert(int n, char *pName, CAT *pHead;) // int n - the insert position in the linked list // The first item is in position 0 // char *pName - the name of the cat to be inserted // CAT *pHead - the head pointer (initially pHead = 0) // The function should return the revised head pointer CAT * del(int n, CAT *pHead;) // int n - the delete position in the linked list // CAT *pHead - the head pointer (initially pHead = 0) // The function should return the revised head pointer Computer Programming and Basic Software Engineering 8. Elementary Data Structure

13 Computer Programming and Basic Software Engineering 8. Elementary Data Structure Your program should be able to allocate or free the memory required to implement the two functions above. It should also check whether the integer n is bigger than the size of the list. Exercise 8.1 (cont)

14