Introduction to Data Structures Systems Programming Concepts.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Linked List Alternate approach to maintaining an array of elements Rather than allocating one large group of elements, allocate elements as needed Q: how.
Stacks, Queues, and Linked Lists
Linked Lists.
Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
CHP-5 LinkedList.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Structures in C.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Elementary Data Structures: Part 2: Strings, 2D Arrays, Graphs
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
C Slides and captured lecture (video and sound) are available at:
Linked List (I) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Introduction to Data Structures Systems Programming.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Lecture 14 Linked Lists 14-1 Richard Gesick. Linked Lists Dynamic data structures can grow and shrink at execution time. A linked list is a linear collection.
 2008 Pearson Education, Inc. All rights reserved. 1 Member data stores a value of type parameter NODETYPE Member nextPtr stores a pointer to the next.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Programming Practice 3 - Dynamic Data Structure
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 5.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Data Structures - Prabir Sarkar. AGENDA Stack Queue Linked List Trees Graphs Searching and Sorting Algorithm.
CPT: Lists/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to describe linked lists in C 15. Lists.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
(1-3) Basics of a Linked List I Instructor - Andrew S. O’Fallon CptS 122 (June 9, 2016) Washington State University.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Dynamic Allocation Review Structure and list processing
Linked List :: Basic Concepts
5.13 Recursion Recursive functions Functions that call themselves
Linked Lists Chapter 6 Section 6.4 – 6.6
12 C Data Structures.
12 C Data Structures.
Elementary Data Structures
CISC181 Introduction to Computer Science Dr
Introduction to Data Structures
(2-1) Data Structures & The Basics of a Linked List I
(2-1) Data Structures & The Basics of a Linked List I
Chapter 16-2 Linked Structures
Review & Lab assignments
Presentation transcript:

Introduction to Data Structures Systems Programming Concepts

2 Intro to Data Structures  Self-referential Structures  Dynamic Memory Allocation  A Simple malloc Example  Linear Lists  Linked Lists  Insertion Example (using strings)  Linked List Example Systems Programming Introduction to Data Structures 2

Self-Referential Structures   Self-referential structures contain a pointer member that points to a structure of the same structure type. Example: struct node { int data; struct node *nextPtr; }   nextPtr – –is a pointer member that points to a structure of the same type as the one being declared. – –is referred to as a link. Links can tie one node to another node.  Self-referential structures can be linked together to form useful data structures such as lists, queues, stacks and trees. Systems Programming Introduction to Data Structures 3

Fig Self-Referential Structures Linked Together Terminated with a NULL pointer (0) Not setting the link in the last node of a list to NULL can lead to runtime errors.  2007 Pearson Ed -All rights reserved. Systems Programming Introduction to Data Structures 4

12.3 Dynamic Memory Allocation  dynamic memory allocation,  Creating and maintaining dynamic data structures requires dynamic memory allocation, namely, the ability to obtain and release memory at execution time.   In C, the functions malloc and free and the operator sizeof are essential to dynamic memory allocation.  2007 Pearson Ed -All rights reserved. Systems Programming Introduction to Data Structures 5

malloc   malloc – –Takes as its argument the number of bytes to allocate. Thus, sizeof is used to determine the size of an object. – –Returns a pointer of type void * A void * pointer may be assigned to any pointer. If no memory is available, malloc returns NULL. Example newPtr = malloc( sizeof( struct node )) Note – the allocated memory is NOT initialized.  2007 Pearson Ed -All rights reserved. Systems Programming Introduction to Data Structures 6

free   free – –Deallocates memory allocated by malloc. – –Takes a pointer as an argument. Example free ( newPtr );  2007 Pearson Ed -All rights reserved. Systems Programming Introduction to Data Structures 7

A Simple malloc Example int main () { int x = 11; int *pptr, *qptr; pptr = (int *) malloc(sizeof (int)); *pptr = 66; qptr = pptr; printf ("%d %d %d\n", x, *pptr, *qptr); x = 77; *qptr = x + 11; printf ("%d %d %d\n", x, *pptr, *qptr); pptr = (int *) malloc(sizeof (int)); *pptr = 99; printf ("%d %d %d\n", x, *pptr, *qptr); return 0; } $./malloc Systems Programming Introduction to Data Structures 8

A Simple free Example int main () { int x = 11; int *pptr, *qptr; pptr = (int *) malloc(sizeof (int)); *pptr = 66; qptr = (int *) malloc(sizeof (int)); *qptr = 55; printf ("%d %d %d\n", x, *pptr, *qptr); free(pptr); x = 77; pptr = qptr; qptr = (int *) malloc(sizeof (int)); *qptr = x + 11; printf ("%d %d %d\n", x, *pptr, *qptr); pptr = (int *) malloc(sizeof (int)); *pptr = 99; printf ("%d %d %d\n", x, *pptr, *qptr); free(qptr); printf ("%d %d %d\n", x, *pptr, *qptr); return 0; }./free Systems Programming Introduction to Data Structures 9 Cannot trust contents of freed memory!!

Linear Lists  implicit in the index.  With a linear list, the assumption is the next element in the data structure is implicit in the index.  This saves space, but is expensive for insertions! Aray[0] Aray[99] Systems Programming Introduction to Data Structures 10

12.4 Linked Lists Linked list :: – –A linear collection of self-referential class objects, called nodes. – –Connected by pointer links. – –Accessed via a pointer to the first node of the list. – –Subsequent nodes are accessed via the link pointer member of the current node. – –The link pointer in the last node is set to NULL to mark the list’s end.  2007 Pearson Ed -All rights reserved. Systems Programming Introduction to Data Structures 11

12.4 Linked Lists   Use a linked list instead of an array when – –You have an unpredictable number of data elements. – –When members of node are of different types. – –Your list needs to be sorted quickly. – –Keeping the list sorted implies numerous insertions.  2007 Pearson Ed -All rights reserved. Systems Programming Introduction to Data Structures 12

Fig Linked List Graphical Representation  2007 Pearson Ed -All rights reserved. Systems Programming Introduction to Data Structures 13 Line added to indicate pointer may be passed to a called routine where data structure is “hidden” and operated on.

Fig Inserting a Node in an Ordered List  2007 Pearson Ed -All rights reserved. Systems Programming Introduction to Data Structures 14 A pointer to a pointer is used here.

Fig Deleting a Node in an Ordered List  2007 Pearson Ed -All rights reserved. Systems Programming Introduction to Data Structures 15

Linked Lists sptr nptr->link = *sptr; *sptr = nptr; nptr name link Systems Programming Introduction to Data Structures 16

Linked List Insertion Example /* An Example that uses strings in character arrays as part of a linked list */ #define SIZE 4 #include typedef struct { char name[4]; struct Node *link; } Node; typedef Node *Link; /* init inserts a new string in a Node at the FRONT of the linked list */ void init (Link *sptr, char cray[]) /* Note this uses a pointer to a pointer */ { Link nptr; nptr = malloc(sizeof(Node)); /* nptr points to an empty Node */ if (nptr != NULL) /* malloc returns NULL when out of memory!! */ { strcpy(nptr->name, cray); /* string copy string2 to string1 */ nptr->link = *sptr; *sptr = nptr; } Systems Programming Introduction to Data Structures 17

Linked List Insertion Example   /* Insert a new string into the list in sorted order */ void insert( Link *sPtr, char *cray ) { Link newPtr; /* new node */ Link previousPtr; /* previous node */ Link currentPtr; /* current node */ /* dynamically allocate memory */ newPtr = malloc( sizeof( Node ) ); /* if newPtr does not equal NULL */ if ( newPtr ) { strcpy(newPtr->name, cray); newPtr->link = NULL; previousPtr = NULL; /* set currentPtr to start of list */ currentPtr = *sPtr; /* loop to find correct location in list */ while ( currentPtr != NULL && strcmp(cray, currentPtr->name) > 0) { previousPtr = currentPtr; currentPtr = currentPtr->link; } /* end while */ /* insert at beginning of list */ if ( previousPtr == NULL ) { newPtr->link = *sPtr; *sPtr = newPtr; } /* end if */ else { /* insert node between previousPtr and currentPtr */ previousPtr->link = newPtr; newPtr->link = currentPtr; } /* end else */ } /* end if */ else { printf( "%s not inserted. No memory available.\n", cray ); } /* end else */ } /* end function insert */ Systems Programming Introduction to Data Structures 18

Linked List Insertion Example /* Print the linked list */ void printList( Link currentPtr ) /* Note here just the pointer itself is passed */ { /* if list is empty */ if ( !currentPtr ) { printf( "List is empty.\n\n" ); } /* end if */ else { /* loop while currentPtr does not equal NULL */ while ( currentPtr ) { printf( "%s ", currentPtr->name ); currentPtr = currentPtr->link; } /* end while */ printf( "*\n" ); } /* end else */ } /* end function printList */ Systems Programming Introduction to Data Structures 19

Linked List Insertion Example int main (void) { int i; /* Five strings to place in the linked list */ char b[] = "Bat"; char c[] = "Cat"; char h[] = "hat"; char m[] = "mat"; char v[] = "vat"; char n[4]; char atray[5][4]; Link lptr = NULL; /* Set the linked list to empty */ strcpy (&atray[0][0], v); /* string copy string2 to string1 */ strcpy (&atray[1][0], m); strcpy (&atray[2][0], c); strcpy (&atray[3][0], b); for (i = 0; i < SIZE; i++) printf("%s ", &atray[i][0]); printf("\n");./LList vat mat Cat Bat Systems Programming Introduction to Data Structures 20

Linked List Insertion Example /* init assumes the linked list is being initialized with node elements entered in reverse alphabetical order */ for (i = 0; i < SIZE; i++) init(&lptr, &atray[i][0]); printList (lptr); /* insert is the same as in the textbook except it compares strings to insert in alphabetical order */ insert (&lptr, h);// insert ‘hat’ in alphabetical ASCII order printList (lptr); scanf("%s", n); /* continue reading and inserting until EOF */ while (strcmp(n, "EOF") != 0) { insert (&lptr, n); printList (lptr); scanf("%s", n); } return; }./LList vat mat Cat Bat Bat Cat mat vat *./LList vat mat Cat Bat Bat Cat mat vat * Bat Cat hat mat vat * Note: a pointer to a pointer!! Systems Programming Introduction to Data Structures 21 Only the pointer passed here.

BatCat matvat \ hat newPtr sPtr \ previousPtr currentPtr Linked List Insertion Example Systems Programming Introduction to Data Structures 22

Each node in the list contains a data element and a pointer to the next node Linked List Implementation  2007 Pearson Ed -All rights reserved. Systems Programming Introduction to Data Structures 23 main is an example of a “driver” program. Its role is to test the operations on the data structure.

Function insert inserts data into the list Function delete removes data from the list Linked List Implementation  2007 Pearson Ed -All rights reserved. Systems Programming Introduction to Data Structures 24

Linked List Implementation  2007 Pearson Ed -All rights reserved. Systems Programming Introduction to Data Structures 25

To insert a node into the list, memory must first be allocated for that node while loop searches for new node’s place in the list Linked List Implementation  2007 Pearson Ed -All rights reserved. Systems Programming Introduction to Data Structures 26

If there are no nodes in the list, the new node becomes the “start” node Otherwise, the new node is inserted between two others (or at the end of the list) by changing pointers Linked List Implementation  2007 Pearson Ed -All rights reserved. Systems Programming Introduction to Data Structures 27

while loop searches for node’s place in the list Once the node is found, it is deleted by changing pointers and freeing the node’s memory Linked List Implementation  2007 Pearson Ed -All rights reserved. Systems Programming Introduction to Data Structures 28

If the start node is NULL, there are no nodes in the list Linked List Implementation  2007 Pearson Ed -All rights reserved. Systems Programming Introduction to Data Structures 29

Linked List Implementation  2007 Pearson Ed -All rights reserved. Systems Programming Introduction to Data Structures 30

Output from Linked List Implementation  2007 Pearson Ed -All rights reserved. Systems Programming Introduction to Data Structures 31

Output from Linked List Implementation  2007 Pearson Ed -All rights reserved. Systems Programming Introduction to Data Structures 32

Review of Intro Data Structures   Introduced malloc and free.   Discussed the tradeoffs between a linear list and a linked list.   Provided two linked examples.   Explained event lists as one instance of linked lists.   Important operations on linked lists: – –Insertion in the list. – –Taking a random node out of the list. – –Taking the ‘next’ node off the front of the list. – –Printing the linked list in order. Systems Programming Introduction to Data Structures 33

Review of Data Structures Seen   Arrays   Arrays of pointers to strings   Arrays of structs (note: this is a linear list)   Linked lists – –singly-linked lists – –[not] doubly-linked lists!! Systems Programming Introduction to Data Structures 34