CSCE 210 Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

CHP-5 LinkedList.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
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.
Computer programming1 Arrays. Computer programming2 ARRAYS Motivation Introduction to Arrays Static arrays Arrays and Functions Arrays, Classes, and typedef.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
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.
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.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11. The Struct Data Type.
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.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
CSCE 210 Data Structures and Algorithms
STACKS & QUEUES for CLASS XII ( C++).
Pointers and Dynamic Arrays
Data Structure By Amee Trivedi.
Chapter 7 Pointers and C-Strings
Standard Version of Starting Out with C++, 4th Edition
Lectures linked lists Chapter 6 of textbook
Data Structure Interview Question and Answers
Chapter 9: Pointers.
Data Structure and Algorithms
Chapter 10: Pointers Starting Out with C++ Early Objects
CSCE 210 Data Structures and Algorithms
Chapter 4 Linked Lists.
Data Structures Interview / VIVA Questions and Answers
8 Pointers.
Chapter 4 Linked Lists
Chapter 10: Pointers Starting Out with C++ Early Objects
LINKED LISTS CSCD Linked Lists.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Object Oriented Programming COP3330 / CGS5409
understanding memory usage by a c++ program
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 9: Pointers.
Pointers, Dynamic Data, and Reference Types
Further Data Structures
Linked Lists.
Linked Lists Chapter 4.
Pointers.
Objectives You should be able to describe: Addresses and Pointers
Introduction to C++ Linear Linked Lists
Pointers & Dynamic Data Structures
7 Arrays.
Data Structures & Algorithms
Standard Version of Starting Out with C++, 4th Edition
Pointers, Dynamic Data, and Reference Types
Linked Lists Chapter 5 (continued)
LINEAR DATA STRUCTURES
Presentation transcript:

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

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, AUC

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, AUC

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, AUC

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, AUC

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, AUC

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, AUC

2-D Arrays Useful in representing a variety of data, e.g. Tables, Matrices, Graphs and Images Day Fri Sat Sun j i City 35 32 33 34 31 29 28 5 2 3 4 1 9 8 Cairo Tanta Alex A Table of Temp. A Matrix of Integers Prof. Amr Goneid, AUC

Declaration & Indexing <element-type> <arrayName> [size 1][size2]; e.g. double table[NROWS] [NCOLS]; Indexing: table[2] [4]; Row# 0 .. NROWS Column# 0 .. NCOLS Prof. Amr Goneid, AUC

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, AUC

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, AUC

Pointers double x = 3.14; // a variable of type double x p 3.14 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 x p 3.14 0012FF78 Starts at location 0012FF78 Prof. Amr Goneid, AUC

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, AUC

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, AUC

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

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, AUC

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 *p p 15.5 Prof. Amr Goneid, AUC

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 <pointer variable>; delete p; Prof. Amr Goneid, AUC

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, AUC

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 <Type>; it returns the base address of that array. The address returned by new must be assigned to a pointer of type Type. new <Type> [n] n-1 Prof. Amr Goneid, AUC

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 < n; i++) cin >> A[i]; . . . } Prof. Amr Goneid, AUC

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, AUC

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 < n; i++) cin >> A[i]; ……….. delete [ ] A; // Release memory locations } Prof. Amr Goneid, AUC

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, AUC

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. e Last NULL head First next cursor Prof. Amr Goneid, AUC

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, AUC

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 word count next String Integer Address Prof. Amr Goneid, AUC

Building Nodes Allocate storage of 2 nodes Assign data to 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, AUC

Building Nodes p hat 2 ? q top 3 ? Prof. Amr Goneid, AUC

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; p hat 2 next q top 3 NULL Prof. Amr Goneid, AUC

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, AUC

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

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

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

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

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

Demo http://www.csanimated.com/animation.php?t=Linked_list Prof. Amr Goneid, AUC

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

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

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