Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

CHP-5 LinkedList.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Informática II Prof. Dr. Gustavo Patiño MJ
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
 2006 Pearson Education, Inc. All rights reserved Pointers.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 10: Pointers.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11a. The Vector Class.
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.
Review of C++ Programming Part II Sheng-Fang Huang.
Pointer Data Type and Pointer Variables
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified by use by the MSU CMPS Dept. Chapter 10:
C++ Pointers Copies from SEE C++ programming course and from Starting Out with C++: Early Objects, 8/E by Tony Gaddis, Judy Walters and Godfrey Muganda.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
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.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11. The Struct Data Type.
Basic Semantics Associating meaning with language entities.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Review 1 List Data Structure List operations List Implementation Array Linked List.
Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Prof. Amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays.
Pointers Lecture: 5. Topics 1 Pointers and the Address Operator 2 Pointer Variables 3 The Relationship Between Arrays and Pointers 4 Pointer Arithmetic.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CSCE 210 Data Structures and Algorithms
STACKS & QUEUES for CLASS XII ( C++).
Lectures linked lists Chapter 6 of textbook
CSCE 210 Data Structures and Algorithms
Chapter 4 Linked Lists.
CSCE 210 Data Structures and Algorithms
LINKED LISTS CSCD Linked Lists.
Pointers & Dynamic Data Structures
LINEAR DATA STRUCTURES
Presentation transcript:

Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures

Prof. Amr Goneid, AUC2 Elementary Data Structures Static and Dynamic Data Structures Static Arrays Pointers Run-Time Arrays The Linked List Structure Some Linked List Operations Variations on Linked Lists

Prof. Amr Goneid, AUC3 1. Static & Dynamic Data Structures Static data are allocated memory at Compile Time, i.e. before the program is executed. Static data are allocated their memory space in a place called the Data Segment Static data cannot change size during Run Time, i.e. while the program is running.

Prof. Amr Goneid, AUC4 Static & Dynamic Data Structures The Heap ( free memory) A Dynamic Data Structure is allocated memory at run-time. Consists of nodes to store data and pointers to these nodes to access the data. Nodes are created (allocated) and destroyed (de- allocated) at run-time. Using dynamic allocation allows your programs to create data structures with sizes that can be defined while the program is running and to expand the sizes when needed.

Prof. Amr Goneid, AUC5 2. Static Arrays Abstraction: A homogenous sequence of elements with a fixed size that allows direct access to its elements. Elements (members): Any type, but all elements must be of the same type Relationship: Linear (One-To-one). Ordered storage with direct access. Fundamental Operations: create array (by declaring it) with a size fixed at compile time store an element in the array at a given position (direct access) retrieve an element from a given position (direct access)

Prof. Amr Goneid, AUC6 Operations on Arrays Create Array: Size is fixed (constant): e.g. int x[20], string name[50]; Retrieve an element:e.g. z = x[ i ] ; Store an element:e.g. name[ i ] = “Ann” ;

Prof. Amr Goneid, AUC7 Passing to and from Functions Arrays are always passed by reference e.g. int findmax ( int x [ ], int size ); Can use const if array elements are not to be modified, e.g. int findmax ( const int x [ ], int size ); Do not include the array size within the brackets when defining an array parameter. This is because the array name is the base address of the array, and the size is already known.

Prof. Amr Goneid, AUC8 2-D Arrays Useful in representing a variety of data, e.g. Tables, Matrices, Graphs and Images City Cairo Tanta Alex DayFriSatSun i j A Table of Temp. A Matrix of Integers

Prof. Amr Goneid, AUC9 Declaration & Indexing Declaration: [size 1][size2]; e.g. double table[NROWS] [NCOLS]; Indexing: table[2] [4]; Row# 0.. NROWS Column# 0.. NCOLS

Prof. Amr Goneid, AUC10 2-D Arrays as Parameters In 1-D arrays, it is not necessary to specify the size: void display (int A[ ], int n); To follow the same convention with 2-D arrays: void display (int B[ ] [ ], int n, int m); This will not work, because 2-D arrays are stored as 1-D arrays of arrays (1-D arrays of rows) Hence, beside the base address (name) we must also specify the number of columns, e.g. void display (int B[ ][Ncol], int n, int m);

Prof. Amr Goneid, AUC11 3. Pointers: The Address of a Variable The & symbol is called the address operator The purpose of & is to return the address of a variable in memory. For example, if x is a variable then &x is its address in memory. We can store the address of a variable in a special variable called a pointer A Pointer is a variable whose value is a memory address of an item, not its value A pointer knows about the type of the item it points to All pointers have fixed size (typically 4 bytes)

Prof. Amr Goneid, AUC12 Pointers A pointer variable must be declared before it is used. It must be bound to the same type as the variable it will point to. The asterisk operator * must precede each pointer name in the declaration For Example: double x = 3.14; // a variable of type double double *p;// a pointer to double p = &x;// p now stores the address of x 3.14 x p 0012FF78 Starts at location 0012FF78

Prof. Amr Goneid, AUC13 Dereferencing (Indirection) (*) is the dereferencing (or indirection) operator. It can be used to access the value stored in a location. For example, if (p) is a pointer, the value of (*p) is not the address stored in p but is instead the value stored in memory at that address (i.e. 3.14)

Prof. Amr Goneid, AUC14 Indirection Operator An asterisk has two uses with regard to pointers  In a definition, it indicates that the object is a pointer char *s; // s is of type pointer to char  In expressions, when applied to a pointer it evaluates to the object to which the pointer points (indirection or dereferencing) int k = 1; int *p = &k; // p points to k *p = 2; cout << k << endl; // display a 2

Prof. Amr Goneid, AUC15 Nodes & Pointers A node is an anonymous variable (has no name) No name is needed because there is always a pointer pointing to the node. Heap Node Pointer

Prof. Amr Goneid, AUC16 Creating Nodes: the “new” Operator The new operator allocates memory from the heap to a node of specified type at Run Time. It returns the address of that node. The statements: int *p ; ……………………………………. p = new int; create a new node of type int and let a pointer p point to it. No data is put into the node The node created has no name, it is called an Anonymous Variabe. It can only be accessed via its pointer using the indirection operator, i.e. by using (*p)

Prof. Amr Goneid, AUC17 Accessing Data with Pointers * - indirection operator *p = 15.5; // *p reads as: contents of node pointed to by p Stores floating value 15.5 in the node pointed to by p 15.5 p *p

Prof. Amr Goneid, AUC18 Returning Nodes to the Heap Operation: Returns space of node pointed to by pointer back to heap for re-use When finished with a node delete it Pointer is not destroyed but undefined : Example: delete ; delete p;

Prof. Amr Goneid, AUC19 4. Run-Time Arrays Drawbacks of static arrays:  Capacity is fixed at compile time  If size > number of elements, memory is wasted  If size < number of elements, we suffer array overflow Solution: Dynamic (Run-Time) Arrays:  Capacity specified during program execution.  Acquire additional memory as needed.  Release memory locations when they are not needed.

Prof. Amr Goneid, AUC20 Run-Time Arrays The operator new can be used in an expression of the form: n is an integer expression (could be a variable). This allocates an array with n elements, each of type ; it returns the base address of that array. The address returned by new must be assigned to a pointer of type Type. new [n] n-1

Prof. Amr Goneid, AUC21 Example int n; cout << “Enter size of array: "; cin >> n;// size is entered at run-time if (n > 0) { int *A = new int [n]; // A is now the base address // process A for (int i = 0; i > A[i];... }

Prof. Amr Goneid, AUC22 Run-Time Arrays Because run-time arrays can take a lot of memory from the heap, we must de-allocate that space after we finish with it. To return memory allocated to array pointed to by A, use the delete operator in the form: delete [ ] A;

Prof. Amr Goneid, AUC23 Example int n; cout << “Enter size of array: "; cin >> n;// size is entered at run-time if (n > 0) { int *A = new int [n]; // A is now the base address // process A for (int i = 0; i > A[i]; ……….. delete [ ] A; // Release memory locations }

Prof. Amr Goneid, AUC24 5. The Linked List Structure Arrange dynamically allocated structures into a new structure called a linked list Think of a set of children’s pop beads Connecting beads to make a chain You can move things around and re-connect the chain We use pointers to create the same effect

Prof. Amr Goneid, AUC25 The Simple Linked List A sequence of nodes linked by pointers: First node pointed to by head. Contains a data element (e) and a next pointer to next node. Last node’s next is NULL. A cursor points to the current node. It can advance in one way only to next node, e.g. to traverse whole list. head NULL cursor e next First Last

Prof. Amr Goneid, AUC26 Specifying Node Structure (Example) Suppose each node is to contain a word from the dictionary, and the number of times such word occurs in a document. struct elType// specify data element { string word;int count}; struct node// specify node structure {elType e; node *next; }; node *p, *q;// pointers to nodes of type node

Prof. Amr Goneid, AUC27 Specifying Node Structure Each of the pointers p, q can point to a struct of type node: e.word(string) e.count(int) next(pointer to next node) Struct of type node String Integer Address wordcountnext

Prof. Amr Goneid, AUC28 Building Nodes Allocate storage of 2 nodes p = new node; q = new node; Assign data to nodes elType el1, el2; el1.word = “hat”;el1.count = 2; el2.word = “top”;el2. count = 3; p->e = el1;q->e = el2;

Prof. Amr Goneid, AUC29 Building Nodes top3? hat2? q p

Prof. Amr Goneid, AUC30 Connecting Nodes: A linked list of two nodes Suppose the address in q is stored in next field of node pointed to by p and NULL is stored in the last next field: p->next = q; q->next = NULL; top3 NULL hat2 next q p

Prof. Amr Goneid, AUC31 6. Some Linked List Operations Insertion at head of list Inserting a node after a given node Insert at end of list Delete a head node Delete a non-head node

Prof. Amr Goneid, AUC32 Insertion at Head of List hat 2 if 4 top 3 head First p Last New 2 3 elType el; el.word = “if”; el.count = 4; p = new node; p-> e = el; p->next = head; head = p; 1

Prof. Amr Goneid, AUC33 Inserting after a given Node hat 2 the 5 top 3 p New cursor if 4 head el.word = “the”; el.count = 5; p = new node; p-> e = el; p-> next = cursor-> next; cursor->next = p;

Prof. Amr Goneid, AUC34 Insert at End of List hat 2 if 4 top 3 p Last New p = new node; p->e = el; p->next = NULL; cursor->next = p; cursor 1 2 3

Prof. Amr Goneid, AUC35 Delete Head Node hat 2the 5top cursor if 4 head cursor = head; head = head->next; delete cursor; 1

Prof. Amr Goneid, AUC36 Deleting a Non-Head Node hat thetop prev Successor 2 cursor node *q; q = cursor; cursor = cursor->next; prev->next = cursor; delete q ; 1 3 Pre: cursor points to node prev points to predecessor node q cursor

Prof. Amr Goneid, AUC37 mukundan/dsal/LinkListAppl.htmlDemo

Prof. Amr Goneid, AUC38 7. Variations on Linked Lists The Circular List: Notice that tail->next == head head cursor tail

Prof. Amr Goneid, AUC39 Variations on Linked Lists The Doubly Linked List To advance: cursor = cursor->next; To back : cursor = cursor->back; nextback cursor

Prof. Amr Goneid, AUC40 Variations on Linked Lists The Circular Doubly Linked List The 2-D List: