C Program Design C Data Structures 主講人:虞台文. Content Introduction Self-Referential Structures Dynamic Memory Allocation Linked Lists Stacks Queues Trees.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Linked Lists.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Advanced Data Structures Stack –a stack is dynamic data items in a linear order, such that the item first "pushed" in is the last item "popped" out. Think.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
Exercise 6 : Stack 1.Stack is a data structure that supports LIFO (Last In First Out) operations. - In this exercise, you implement Stack data structures.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
C Programming : Elementary Data Structures 2009/04/22 Jaemin
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
COMP 110 Introduction to Programming Mr. Joshua Stough.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
Objectives of these slides:
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
1 Data Structures Lists and Trees. 2 Real-Life Computational Problems All about organizing data! –What shape the data should have to solve your problem.
Grade 12 Computer Studies HG
UNIT-2. Singly Linked List Doubly Linked List Circular Linked List Representing Stack with Linked List. Representing Queue with Linked List.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Introduction to Data Structures Systems Programming.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
CSC2100B Tutorial 2 List and stack implementation.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Binary Trees Definition A binary tree is: (i) empty, or (ii) a node whose left and right children are binary trees typedef struct Node Node; struct Node.
Introduction to Data Structures Systems Programming Concepts.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Chapter 17 – Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
Programming Practice 3 - Dynamic Data Structure
Copyright © 2002 Pearson Education, Inc. Slide 1.
Linked Lists EENG 212 ALGORITHMS And DATA STRUCTURES.
Data Structures Systems Programming. Systems Programming: Data Structures 2 2 Systems Programming: 2 Data Structures  Queues –Queuing System Models –Queue.
Final Exam –Date: Aug 27 th –Time: 9:00am – 12:00pm –Location: TEL 0014.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
© 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.
CSC215 Lecture Data Structures.
Data Structure By Amee Trivedi.
Lists, Stacks and Queues in C
Chapter 12 – Data Structures
5.13 Recursion Recursive functions Functions that call themselves
C Program Design C Data Structures
12 C Data Structures.
12 C Data Structures.
Stack and Queue APURBO DATTA.
Introduction to Data Structures
Chapter 19 – Data Structures
Lesson Objectives Aims
EENG 212 ALGORITHMS And DATA STRUCTURES
Review & Lab assignments
Pointers & Dynamic Data Structures
ECE 103 Engineering Programming Chapter 64 Tree Implementation
Module 13 Dynamic Memory.
Presentation transcript:

C Program Design C Data Structures 主講人:虞台文

Content Introduction Self-Referential Structures Dynamic Memory Allocation Linked Lists Stacks Queues Trees

C Program Design C Data Structures Introduction

Dynamic data structures – Data structures that grow and shrink during execution Linked lists – Allow insertions and removals anywhere Stacks – Allow insertions and removals only at top of stack Queues – Allow insertions at the back and removals from the front Binary trees – High-speed searching and sorting of data and efficient elimination of duplicate data items

C Program Design C Data Structures Self-Referential Structures

Linked Lists List

Linked Lists struct node { int data; struct node *nextNode; }; Structure that contains a pointer to a structure of the same type

Tree struct treeNode { int data; struct treeNode *left; struct treeNode *right; };

C Program Design C Data Structures Dynamic Memory Allocation

Obtain and release memory during execution Allocate Memory Release Memory void *malloc(size_t size); void free(void *ptr);

void *malloc(size_t size); How to determine the size needed? – Record size  #records A structure’s size is not necessarily the sum of the sizes of its members. – This is so because of various machine-dependent boundary alignment requirements. – Use the sizeof operator to determine the size of a structure. Test for a NULL pointer return value. – Print an error message if the requested memory is not allocated. When the allocated memory no longer needed, be sure to release it by calling free(). – Cause memory leak if don’t do so.

void free(void *ptr); Not returning dynamically allocated memory when it is no longer needed can cause the system to run out of memory prematurely. – This is sometimes called a “memory leak.” Freeing memory not allocated dynamically with malloc is an error. Referring to memory that has been freed is an error.

void *malloc(size_t size); #define NUM_RECORDS 100 struct XXX{ }; struct XXX *p = NULL; p = (struct XXX *) malloc(sizeof(struct XXX) * NUM_RECORDS); if(p == NULL){ printf("Memory allocation fail!\n"); return; } free(p); p = NULL; // a good habit to do so #define NUM_RECORDS 100 struct XXX{ }; struct XXX *p = NULL; p = (struct XXX *) malloc(sizeof(struct XXX) * NUM_RECORDS); if(p == NULL){ printf("Memory allocation fail!\n"); return; } free(p); p = NULL; // a good habit to do so

Example #include int main () { int i,n; char * buffer; printf ("How long do you want the string? "); scanf ("%d", &i); buffer = (char*) malloc (i+1); if (buffer==NULL) exit (1); for (n=0; n<i; n++) buffer[n]=rand()%26+'a'; buffer[i]='\0'; printf ("Random string: %s\n",buffer); free (buffer); return 0; } #include int main () { int i,n; char * buffer; printf ("How long do you want the string? "); scanf ("%d", &i); buffer = (char*) malloc (i+1); if (buffer==NULL) exit (1); for (n=0; n<i; n++) buffer[n]=rand()%26+'a'; buffer[i]='\0'; printf ("Random string: %s\n",buffer); free (buffer); return 0; } Generate random string of desired length.

Example #include main() { int array[50]; int i, guess; srand((unsigned) time(NULL)); while(1){ int hit; hit = 0;// set false at game beginning for(i = 0; i<50; i++) array[i] = rand() % ; printf("Enter your guess (1-100) and 0 to end:"); scanf("%d", &guess); if(guess == 0) break; for(i=0; i<50 && !hit; i++) if(array[i] == guess){ printf("Bingo\n"); hit = 1; // set hit so as to break the loop } if(!hit) printf("You guess a wrong number.\n"); } #include main() { int array[50]; int i, guess; srand((unsigned) time(NULL)); while(1){ int hit; hit = 0;// set false at game beginning for(i = 0; i<50; i++) array[i] = rand() % ; printf("Enter your guess (1-100) and 0 to end:"); scanf("%d", &guess); if(guess == 0) break; for(i=0; i<50 && !hit; i++) if(array[i] == guess){ printf("Bingo\n"); hit = 1; // set hit so as to break the loop } if(!hit) printf("You guess a wrong number.\n"); } } Randomly generate 50 integers for guess.

Example #include main() { int array[50]; int i, guess; srand((unsigned) time(NULL)); while(1){ int hit; hit = 0;// set false at game beginning for(i = 0; i<50; i++) array[i] = rand() % ; printf("Enter your guess (1-100) and 0 to end:"); scanf("%d", &guess); if(guess == 0) break; for(i=0; i<50 && !hit; i++) if(array[i] == guess){ printf("Bingo\n"); hit = 1; // set hit so as to break the loop } if(!hit) printf("You guess a wrong number.\n"); } #include main() { int array[50]; int i, guess; srand((unsigned) time(NULL)); while(1){ int hit; hit = 0;// set false at game beginning for(i = 0; i<50; i++) array[i] = rand() % ; printf("Enter your guess (1-100) and 0 to end:"); scanf("%d", &guess); if(guess == 0) break; for(i=0; i<50 && !hit; i++) if(array[i] == guess){ printf("Bingo\n"); hit = 1; // set hit so as to break the loop } if(!hit) printf("You guess a wrong number.\n"); } Randomly generate 50 integers for guess.

Example #include main() { int* array=NULL;// store dynamic pointer int i, guess, size; srand((unsigned) time(NULL)); while(1){ int hit; hit = 0;// set false at game beginning printf("How many number do you want to generate or 0 to end?"); scanf("%d", &size); if(size==0) break; if((array = (int *) malloc(sizeof(int) * size)) == NULL){ printf("Memory allocation fail.\n"); exit(1); } for(i = 0; i<size; i++) array[i] = rand() % ; printf("Enter your guess (1-100):"); scanf("%d", &guess); for(i=0; i < size && !hit; i++) if(array[i] == guess){ printf("Bingo\n"); hit = 1; } if(!hit) printf("You guess a wrong number.\n"); free(array); } #include <stdio.h> #include <stdlib.h> #include <time.h> main() { int* array=NULL;// store dynamic pointer int i, guess, size; srand((unsigned) time(NULL)); while(1){ int hit; hit = 0;// set false at game beginning printf("How many number do you want to generate or 0 to end?"); scanf("%d", &size); if(size==0) break; if((array = (int *) malloc(sizeof(int) * size)) == NULL){ printf("Memory allocation fail.\n"); exit(1); } for(i = 0; i<size; i++) array[i] = rand() % ; printf("Enter your guess (1-100):"); scanf("%d", &guess); for(i=0; i < size && !hit; i++) if(array[i] == guess){ printf("Bingo\n"); hit = 1; } if(!hit) printf("You guess a wrong number.\n"); free(array); } } Randomly generate the number of integers specified by the user for guess.

Example #include main() { int* array=NULL;// store dynamic pointer int i, guess, size; srand((unsigned) time(NULL)); while(1){ int hit; hit = 0;// set false at game beginning printf("How many number do you want to generate or 0 to end?"); scanf("%d", &size); if(size==0) break; if((array = (int *) malloc(sizeof(int) * size)) == NULL){ printf("Memory allocation fail.\n"); exit(1); } for(i = 0; i<size; i++) array[i] = rand() % ; printf("Enter your guess (1-100):"); scanf("%d", &guess); for(i=0; i < size && !hit; i++) if(array[i] == guess){ printf("Bingo\n"); hit = 1; } if(!hit) printf("You guess a wrong number.\n"); free(array); } #include main() { int* array=NULL;// store dynamic pointer int i, guess, size; srand((unsigned) time(NULL)); while(1){ int hit; hit = 0;// set false at game beginning printf("How many number do you want to generate or 0 to end?"); scanf("%d", &size); if(size==0) break; if((array = (int *) malloc(sizeof(int) * size)) == NULL){ printf("Memory allocation fail.\n"); exit(1); } for(i = 0; i<size; i++) array[i] = rand() % ; printf("Enter your guess (1-100):"); scanf("%d", &guess); for(i=0; i < size && !hit; i++) if(array[i] == guess){ printf("Bingo\n"); hit = 1; } if(!hit) printf("You guess a wrong number.\n"); free(array); } Randomly generate the number of integers specified by the user for guess.

C Program Design C Data Structures Linked Lists

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 Link pointer in the last node is set to NULL to mark the list’s end

When use linked lists? You have an unpredictable number of data elements Your list needs to be sorted quickly Data comes and goes frequently

Nodes struct node { int data; struct node *nextNode; }; // start pointer of a list node* rootList; struct node { int data; struct node *nextNode; }; // start pointer of a list node* rootList; rootList

Example A sorted list

Example // ListManagement.h typedef struct node { int data; struct node *nextNode; } Node; Node* GetNode(); void FreeNode(Node* ptr); Node* Insert(Node* ptrRoot, int itemData); Node* Delete(Node* ptrRoot, int itemData); void PrintList(Node* ptrRoot); void FreeList(Node* ptrRoot); // ListManagement.h typedef struct node { int data; struct node *nextNode; } Node; Node* GetNode(); void FreeNode(Node* ptr); Node* Insert(Node* ptrRoot, int itemData); Node* Delete(Node* ptrRoot, int itemData); void PrintList(Node* ptrRoot); void FreeList(Node* ptrRoot); A sorted list

Example // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } A sorted list

Example // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } A sorted list Node* GetNode() { Node *pNode; pNode = (Node*) malloc(sizeof(Node)); if(pNode) pNode->nextNode = NULL; return pNode; } Node* GetNode() { Node *pNode; pNode = (Node*) malloc(sizeof(Node)); if(pNode) pNode->nextNode = NULL; return pNode; }

Example // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } A sorted list void FreeNode(Node* ptr) { free(ptr); } void FreeNode(Node* ptr) { free(ptr); }

Example // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } // ListManagement.c #include <stdlib.h> #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } A sorted list rootList 145

Example // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } A sorted list rootList 145

Example // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } A sorted list rootList 95

Example // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } A sorted list rootList 95

Example // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } A sorted list Node* Insert(Node* ptrRoot, int itemData) { Node* pNodeInsert = GetNode(); Node* pCurNode, *pPreviousNode; pCurNode = ptrRoot; pPreviousNode = NULL; pNodeInsert->data = itemData; while(pCurNode != NULL && itemData > pCurNode->data){ pPreviousNode = pCurNode; pCurNode = pCurNode->nextNode; }; if(pPreviousNode != NULL){ pPreviousNode->nextNode = pNodeInsert; pNodeInsert->nextNode = pCurNode; return ptrRoot; } else{ pNodeInsert->nextNode = pCurNode; return pNodeInsert; } Node* Insert(Node* ptrRoot, int itemData) { Node* pNodeInsert = GetNode(); Node* pCurNode, *pPreviousNode; pCurNode = ptrRoot; pPreviousNode = NULL; pNodeInsert->data = itemData; while(pCurNode != NULL && itemData > pCurNode->data){ pPreviousNode = pCurNode; pCurNode = pCurNode->nextNode; }; if(pPreviousNode != NULL){ pPreviousNode->nextNode = pNodeInsert; pNodeInsert->nextNode = pCurNode; return ptrRoot; } else{ pNodeInsert->nextNode = pCurNode; return pNodeInsert; } }

Example // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } // ListManagement.c #include <stdlib.h> #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } A sorted list rootList Delete(rootList, 235)

Example // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } A sorted list rootList 235 Delete(rootList, 235)

Example // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } A sorted list rootList Delete(rootList, 100)

Example // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } A sorted list rootList 100 Delete(rootList, 100)

Example // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } A sorted list Node* Delete(Node* ptrRoot, int itemData) { Node *pCurNode, *pPreviousNode; pCurNode = ptrRoot; pPreviousNode = NULL; while(pCurNode != NULL && itemData != pCurNode->data){ pPreviousNode = pCurNode; pCurNode = pCurNode->nextNode; }; if(pCurNode == NULL) return ptrRoot; if(pPreviousNode != NULL){ pPreviousNode->nextNode = pCurNode->nextNode; FreeNode(pCurNode); return ptrRoot; } else{ ptrRoot = pCurNode->nextNode; FreeNode(pCurNode); return ptrRoot; } Node* Delete(Node* ptrRoot, int itemData) { Node *pCurNode, *pPreviousNode; pCurNode = ptrRoot; pPreviousNode = NULL; while(pCurNode != NULL && itemData != pCurNode->data){ pPreviousNode = pCurNode; pCurNode = pCurNode->nextNode; }; if(pCurNode == NULL) return ptrRoot; if(pPreviousNode != NULL){ pPreviousNode->nextNode = pCurNode->nextNode; FreeNode(pCurNode); return ptrRoot; } else{ ptrRoot = pCurNode->nextNode; FreeNode(pCurNode); return ptrRoot; } }

Example // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } A sorted list void PrintList(Node* ptrRoot) { while(ptrRoot != NULL){ printf("%d%s", ptrRoot->data, ptrRoot->nextNode ? "-->" : ""); ptrRoot = ptrRoot->nextNode; } printf("\n"); } void PrintList(Node* ptrRoot) { while(ptrRoot != NULL){ printf("%d%s", ptrRoot->data, ptrRoot->nextNode ? "-->" : ""); ptrRoot = ptrRoot->nextNode; } printf("\n"); }

Example // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } // ListManagement.c #include #include "ListManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } Node* Insert(Node* ptrRoot, int itemData) { } Node* Delete(Node* ptrRoot, int itemData) { } void PrintList(Node* ptrRoot) { } void FreeList(Node* ptrRoot) { } A sorted list void FreeList(Node* ptrRoot) { Node* pTemp; while(ptrRoot != NULL){ pTemp = ptrRoot->nextNode; FreeNode(ptrRoot); ptrRoot = pTemp; } void FreeList(Node* ptrRoot) { Node* pTemp; while(ptrRoot != NULL){ pTemp = ptrRoot->nextNode; FreeNode(ptrRoot); ptrRoot = pTemp; } }

Example A sorted list //main.c #include #include "ListManagement.h" enum FUNCTON {INSERT=1, DELETE, EXIT}; void ShowMenu() { printf("1. Insert\n" "2. Delete\n" "3. Exit\n"); } main() { int function, data, end = 0; Node* rootList = NULL; ShowMenu(); while(!end){ printf("? "); scanf("%d", &function); switch(function){ case INSERT: case DELETE: case EXIT: } //main.c #include #include "ListManagement.h" enum FUNCTON {INSERT=1, DELETE, EXIT}; void ShowMenu() { printf("1. Insert\n" "2. Delete\n" "3. Exit\n"); } main() { int function, data, end = 0; Node* rootList = NULL; ShowMenu(); while(!end){ printf("? "); scanf("%d", &function); switch(function){ case INSERT: case DELETE: case EXIT: } } }

Example A sorted list //main.c #include #include "ListManagement.h" enum FUNCTON {INSERT=1, DELETE, EXIT}; void ShowMenu() { printf("1. Insert\n" "2. Delete\n" "3. Exit\n"); } main() { int function, data, end = 0; Node* rootList = NULL; ShowMenu(); while(!end){ printf("? "); scanf("%d", &function); switch(function){ case INSERT: case DELETE: case EXIT: } //main.c #include <stdio.h> #include "ListManagement.h" enum FUNCTON {INSERT=1, DELETE, EXIT}; void ShowMenu() { printf("1. Insert\n" "2. Delete\n" "3. Exit\n"); } main() { int function, data, end = 0; Node* rootList = NULL; ShowMenu(); while(!end){ printf("? "); scanf("%d", &function); switch(function){ case INSERT: case DELETE: case EXIT: } } } printf("Enter data for insertion:"); scanf("%d", &data); rootList = Insert(rootList, data); PrintList(rootList); break; printf("Enter data for insertion:"); scanf("%d", &data); rootList = Insert(rootList, data); PrintList(rootList); break;

Example A sorted list //main.c #include #include "ListManagement.h" enum FUNCTON {INSERT=1, DELETE, EXIT}; void ShowMenu() { printf("1. Insert\n" "2. Delete\n" "3. Exit\n"); } main() { int function, data, end = 0; Node* rootList = NULL; ShowMenu(); while(!end){ printf("? "); scanf("%d", &function); switch(function){ case INSERT: case DELETE: case EXIT: } //main.c #include <stdio.h> #include "ListManagement.h" enum FUNCTON {INSERT=1, DELETE, EXIT}; void ShowMenu() { printf("1. Insert\n" "2. Delete\n" "3. Exit\n"); } main() { int function, data, end = 0; Node* rootList = NULL; ShowMenu(); while(!end){ printf("? "); scanf("%d", &function); switch(function){ case INSERT: case DELETE: case EXIT: } } } printf("Enter data for deletion:"); scanf("%d", &data); rootList = Delete(rootList, data); PrintList(rootList); break; printf("Enter data for deletion:"); scanf("%d", &data); rootList = Delete(rootList, data); PrintList(rootList); break;

Example A sorted list //main.c #include #include "ListManagement.h" enum FUNCTON {INSERT=1, DELETE, EXIT}; void ShowMenu() { printf("1. Insert\n" "2. Delete\n" "3. Exit\n"); } main() { int function, data, end = 0; Node* rootList = NULL; ShowMenu(); while(!end){ printf("? "); scanf("%d", &function); switch(function){ case INSERT: case DELETE: case EXIT: } //main.c #include <stdio.h> #include "ListManagement.h" enum FUNCTON {INSERT=1, DELETE, EXIT}; void ShowMenu() { printf("1. Insert\n" "2. Delete\n" "3. Exit\n"); } main() { int function, data, end = 0; Node* rootList = NULL; ShowMenu(); while(!end){ printf("? "); scanf("%d", &function); switch(function){ case INSERT: case DELETE: case EXIT: } } } FreeList(rootList); end = 1; break; FreeList(rootList); end = 1; break;

Example A sorted list ListManagement.h ListManagement.c Main.c LinkList.exe This is a non-recursive version.

Example A sorted list ListManagement.h RecursiveListManagement.c Main.c LinkList.exe Recursive version.

C Program Design C Data Structures Stacks

Stack – New nodes can be added and removed only at the top – Similar to a pile of dishes – Last-in, first-out (LIFO) – Can be implemented using array or linked list push – Adds a new node to the top of the stack pop – Removes a node from the top – Stores the popped value – Returns true if pop was successful Push Pop

Memory Stack Implementation  Array sizeStack sp stack numItems= stack = (int*) malloc(sizeof(int) * sizeStack); sp = stack + sizeStack; numItems = 0; stack = (int*) malloc(sizeof(int) * sizeStack); sp = stack + sizeStack; numItems = 0;

Memory Stack Implementation  Array sizeStack sp stack numItems= Push(50); 50

Memory Stack Implementation  Array sizeStack sp stack numItems= Push(50); 50 Push(41); 41

Memory Stack Implementation  Array sizeStack sp stack numItems= Push(50); 50 Push(41); 41 Push(38); 38

Memory Stack Implementation  Array sizeStack sp stack numItems= Push(50); 50 Push(41); 41 Push(38); 38 int Push(int val) { if(numItems==sizeStack){ return 0; } else{ *--sp = val; numItems++; return 1; } int Push(int val) { if(numItems==sizeStack){ return 0; } else{ *--sp = val; numItems++; return 1; } }

Memory Stack Implementation  Array sizeStack sp stack numItems= Push(50); 50 Push(41); 41 Push(38); 38 Pop(&data); 38 data  38

Memory Stack Implementation  Array sizeStack sp stack numItems= Push(50); 50 Push(41); 41 Push(38); 38 Pop(&data); 38 data  38 int Pop(int* ptrData) { if(numItems==0){ return 0; } else{ *ptrData = *sp++; numItems--; return 1; } int Pop(int* ptrData) { if(numItems==0){ return 0; } else{ *ptrData = *sp++; numItems--; return 1; } }

Memory Stack Implementation  Array sizeStack sp stack numItems= arrayStack.c arrayStack.exe

Stack Implementation  Linked List stack

Stack Implementation  Linked List stack 145 Push(&stack, 145)

Stack Implementation  Linked List stack 145 Push(&stack, 888) 888

Stack Implementation  Linked List stack 145 Pop(&stack, &data) 888 data  888

Stack Implementation  Linked List stack 145 Pop(&stack, &data) data  888

Stack Implementation  Linked List // stacknode.h typedef struct node { int data; struct node *nextNode; } Node; Node* GetNode(); void FreeNode(Node*); void Push(Node**, int); int Pop(Node**, int*); int IsStackEmpty(); void PrintStack(Node*); void FreeStack(Node*); // stacknode.h typedef struct node { int data; struct node *nextNode; } Node; Node* GetNode(); void FreeNode(Node*); void Push(Node**, int); int Pop(Node**, int*); int IsStackEmpty(); void PrintStack(Node*); void FreeStack(Node*);

Stack Implementation  Linked List #include #include "stackManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } void Push(Node** ptrStack, int val) { } int Pop(Node** ptrStack, int* ptrData) { } int IsStackEmpty(Node* stack) { return stack == NULL; } void PrintStack(Node* stack) { } void FreeStack(Node* stack) { } #include #include "stackManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } void Push(Node** ptrStack, int val) { } int Pop(Node** ptrStack, int* ptrData) { } int IsStackEmpty(Node* stack) { return stack == NULL; } void PrintStack(Node* stack) { } void FreeStack(Node* stack) { }

Stack Implementation  Linked List #include #include "stackManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } void Push(Node** ptrStack, int val) { } int Pop(Node** ptrStack, int* ptrData) { } int IsStackEmpty(Node* stack) { return stack == NULL; } void PrintStack(Node* stack) { } void FreeStack(Node* stack) { } #include <stdlib.h> #include "stackManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } void Push(Node** ptrStack, int val) { } int Pop(Node** ptrStack, int* ptrData) { } int IsStackEmpty(Node* stack) { return stack == NULL; } void PrintStack(Node* stack) { } void FreeStack(Node* stack) { } Node* GetNode() { Node *pNode; pNode = (Node*) malloc(sizeof(Node)); if(pNode) pNode->nextNode = NULL; return pNode; } Node* GetNode() { Node *pNode; pNode = (Node*) malloc(sizeof(Node)); if(pNode) pNode->nextNode = NULL; return pNode; }

Stack Implementation  Linked List #include #include "stackManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } void Push(Node** ptrStack, int val) { } int Pop(Node** ptrStack, int* ptrData) { } int IsStackEmpty(Node* stack) { return stack == NULL; } void PrintStack(Node* stack) { } void FreeStack(Node* stack) { } #include <stdlib.h> #include "stackManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } void Push(Node** ptrStack, int val) { } int Pop(Node** ptrStack, int* ptrData) { } int IsStackEmpty(Node* stack) { return stack == NULL; } void PrintStack(Node* stack) { } void FreeStack(Node* stack) { } void FreeNode(Node* ptr) { free(ptr); } void FreeNode(Node* ptr) { free(ptr); }

Stack Implementation  Linked List #include #include "stackManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } void Push(Node** ptrStack, int val) { } int Pop(Node** ptrStack, int* ptrData) { } int IsStackEmpty(Node* stack) { return stack == NULL; } void PrintStack(Node* stack) { } void FreeStack(Node* stack) { } #include <stdlib.h> #include "stackManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } void Push(Node** ptrStack, int val) { } int Pop(Node** ptrStack, int* ptrData) { } int IsStackEmpty(Node* stack) { return stack == NULL; } void PrintStack(Node* stack) { } void FreeStack(Node* stack) { } void Push(Node** ptrStack, int val) { Node* pNode = GetNode(); if(NULL==pNode){ printf("No node available.\n"); return; } pNode->data = val; pNode->nextNode = *ptrStack; *ptrStack = pNode; } void Push(Node** ptrStack, int val) { Node* pNode = GetNode(); if(NULL==pNode){ printf("No node available.\n"); return; } pNode->data = val; pNode->nextNode = *ptrStack; *ptrStack = pNode; }

Stack Implementation  Linked List #include #include "stackManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } void Push(Node** ptrStack, int val) { } int Pop(Node** ptrStack, int* ptrData) { } int IsStackEmpty(Node* stack) { return stack == NULL; } void PrintStack(Node* stack) { } void FreeStack(Node* stack) { } #include <stdlib.h> #include "stackManagement.h" Node* GetNode() { } void FreeNode(Node* ptr) { } void Push(Node** ptrStack, int val) { } int Pop(Node** ptrStack, int* ptrData) { } int IsStackEmpty(Node* stack) { return stack == NULL; } void PrintStack(Node* stack) { } void FreeStack(Node* stack) { } int Pop(Node** ptrStack, int* ptrData) { Node* pNode; pNode = *ptrStack; if(pNode == NULL) return NULL; *ptrData = pNode->data; *ptrStack = pNode->nextNode; FreeNode(pNode); return 1; } int Pop(Node** ptrStack, int* ptrData) { Node* pNode; pNode = *ptrStack; if(pNode == NULL) return NULL; *ptrData = pNode->data; *ptrStack = pNode->nextNode; FreeNode(pNode); return 1; }

StackManagement.h StackManagement.c StackMain.c Stack.exe Stack Implementation  Linked List

Application: Reverse Polish Calculator Infix notation: (1 - 2) * (4 + 5) Reverse Polish notation: *

Reverse Polish Calculator * 1

Reverse Polish Calculator * 1 2

Reverse Polish Calculator *  -1

Reverse Polish Calculator * 1 2-  -1

Reverse Polish Calculator * 4

Reverse Polish Calculator * 4 5

Reverse Polish Calculator *  9

Reverse Polish Calculator *  9

Reverse Polish Calculator * 9 9 *  -9

Reverse Polish Calculator * -9 9 *  -9

Reverse Polish Calculator * -9

C Program Design C Data Structures Queues

Queue – Similar to a supermarket checkout line – First-in, first-out (FIFO) – Nodes are removed only from the head – Nodes are inserted only at the tail Operations – enqueue (insert) – dequeue (remove)

Memory Queue Implementation  Array sizeQueue queueHead queue numItems queue = (int*) malloc(sizeof(int) * sizeQueue); queueHead = queueTail = 0; numItems = 0; queue = (int*) malloc(sizeof(int) * sizeQueue); queueHead = queueTail = 0; numItems = 0; queueTail = sizeQueue - 1

Memory Queue Implementation  Array sizeQueue queueHead queue int Enqueue(int val) { if(numItems==sizeQueue) return 0; queue[queueTail++] = val; queueTail %= sizeQueue; numItems++; return 1; } int Enqueue(int val) { if(numItems==sizeQueue) return 0; queue[queueTail++] = val; queueTail %= sizeQueue; numItems++; return 1; } queueTail sizeQueue - 1 Enqueue(35); 35 numItems = 0= 1

Memory Queue Implementation  Array sizeQueue queueHead queue int Enqueue(int val) { if(numItems==sizeQueue) return 0; queue[queueTail++] = val; queueTail %= sizeQueue; numItems++; return 1; } int Enqueue(int val) { if(numItems==sizeQueue) return 0; queue[queueTail++] = val; queueTail %= sizeQueue; numItems++; return 1; } queueTail sizeQueue - 1 Enqueue(35); 35 numItems Enqueue(99); 99 = 2

= 1 Memory Queue Implementation  Array sizeQueue queueHead queue int Dequeue(int* ptrData) { if(numItems==0) return 0; *ptrData = queue[queueHead++]; queueHead %= sizeQueue; numItems--; return 1; } int Dequeue(int* ptrData) { if(numItems==0) return 0; *ptrData = queue[queueHead++]; queueHead %= sizeQueue; numItems--; return 1; } queueTail sizeQueue - 1 Dequeue(&data); 35 numItems 99 data  35

Queue Implementation  Array arrayQueue.c arrayQueue.exe

Queue Implementation  Linked List queueHead queueTail typedef struct _queue { Node* queueHead; Node* queueTail; } Queue; typedef struct _queue { Node* queueHead; Node* queueTail; } Queue;

Queue Implementation  Linked List queueHead queueTail Enqueue(&queue, 145) 145

Queue Implementation  Linked List queueHead queueTail Enqueue(&queue, 145) 145 Enqueue(&queue, 67) 67

Queue Implementation  Linked List queueHead queueTail 145 Dequeue(&queue, &data) 67 data  45

Queue Implementation  Linked List QueueManagement.h QueueManagement.c ListQueue.c ListQueue.exe

C Program Design C Data Structures Trees

Tree nodes contain two or more links – All other data structures we have discussed only contain one Binary trees – All nodes contain two links None, one, or both of which may be NULL – The root node is the first node in a tree. – Each link in the root node refers to a child – A node with no children is called a leaf node root

Trees Tree nodes contain two or more links – All other data structures we have discussed only contain one Binary trees – All nodes contain two links None, one, or both of which may be NULL – The root node is the first node in a tree. – Each link in the root node refers to a child – A node with no children is called a leaf node root

Binary Trees Root TLTL TRTR root struct treeNode { int data; struct treeNode *left; struct treeNode *right; }; Data leftright

Binary Search Trees root

Tree Traversals A B F C D H I G E Infix: Prefix: Postfix: ABCDEFGHI FBADCEGIH ACEDBHIGF

Tree Traversals

Binary Tree Implementation // TreeManagement.h typedef struct treeNode { int data; struct treeNode* left; struct treeNode* right; } TreeNode; TreeNode* GetTreeNode(); void FreeTreeNode(TreeNode*); void FreeTree(TreeNode*); void InsertTree(TreeNode**, int); int BinSearch(TreeNode*, int); void DeleteTree(TreeNode**, int); void InfixOrder(TreeNode*); void PrefixOrder(TreeNode*); void PostfixOrder(TreeNode*); // TreeManagement.h typedef struct treeNode { int data; struct treeNode* left; struct treeNode* right; } TreeNode; TreeNode* GetTreeNode(); void FreeTreeNode(TreeNode*); void FreeTree(TreeNode*); void InsertTree(TreeNode**, int); int BinSearch(TreeNode*, int); void DeleteTree(TreeNode**, int); void InfixOrder(TreeNode*); void PrefixOrder(TreeNode*); void PostfixOrder(TreeNode*); Tree node maintenance Main operations Tree Traversal

Binary Tree Implementation TreeManagement.h TreeManagement.c TreeMain.c Tree.exe