Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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
Introduction
Dynamic data structures
–
Data structures that grow and shrink during execution
–
Allow insertions and removals anywhere
–
Allow insertions and removals only at top of stack
–
Allow insertions at the back and removals from the front
–
High-speed searching and sorting of data and efficient
elimination of duplicate data items
Linked lists
Stacks
Queues
Binary trees
C Program Design
C Data Structures
Self-Referential
Structures
Linked Lists
List
List
List
List
List
100
102
235
440
888
Linked Lists
struct node {
int data;
struct node *nextNode;
};
Structure that contains a pointer to a structure of the same type.
100
102
235
440
888
struct treeNode {
int data;
struct treeNode *left;
struct treeNode *right;
};
Tree
300
140
119
750
282
230
405
290
809
510
760
900
C Program Design
C Data Structures
Dynamic Memory
Allocation
Dynamic Memory Allocation
Obtain and release memory during execution
Allocate Memory
void *malloc(size_t size);
Release Memory
void free(void *ptr);
void *malloc(size_t size);
How to determine the size needed?
–
Record size #records
–
This is so because of various machine-dependent boundary
alignment requirements.
Use the sizeof operator to determine the size of a structure.
A structure’s size is not necessarily the sum of the sizes of
its members.
–
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
#include <stdio.h>
#include <stdlib.h>
Example
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);
Generate random
string of desired
length.
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 <stdio.h>
#include <stdlib.h>
#include <time.h>
Example
main()
{
int array[50];
int i, guess;
srand((unsigned) time(NULL));
while(1){
int hit;
hit = 0;
// set false at game beginning
Randomly generate 50
integers for guess.
for(i = 0; i<50; i++) array[i] = rand() % 100 + 1;
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 <stdio.h>
#include <stdlib.h>
#include <time.h>
Example
main()
{
int array[50];
int i, guess;
srand((unsigned) time(NULL));
while(1){
int hit;
hit = 0;
// set false at game beginning
Randomly generate 50
integers for guess.
for(i = 0; i<50; i++) array[i] = rand() % 100 + 1;
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 <stdio.h>
#include <stdlib.h>
#include <time.h>
Example
main()
{
int* array=NULL;
int i, guess, size;
// store dynamic pointer
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;
Randomly generate the
number of integers
specified by the user
for guess.
if((array = (int *) malloc(sizeof(int) * size)) == NULL){
printf("Memory allocation fail.\n");
exit(1);
}
for(i = 0; i<size; i++) array[i] = rand() % 100 + 1;
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>
Example
main()
{
int* array=NULL;
int i, guess, size;
// store dynamic pointer
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;
Randomly generate the
number of integers
specified by the user
for guess.
if((array = (int *) malloc(sizeof(int) * size)) == NULL){
printf("Memory allocation fail.\n");
exit(1);
}
for(i = 0; i<size; i++) array[i] = rand() % 100 + 1;
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);
}
}
C Program Design
C Data Structures
Linked Lists
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
100
102
235
440
888
When use linked lists?
You have an unpredictable number of data elements
Your list needs to be sorted quickly
Data comes and goes frequently
100
102
235
440
888
Nodes
struct node {
int data;
struct node *nextNode;
};
// start pointer of a list
node* rootList;
rootList
100
102
235
440
888
Example
A sorted list
Example
A sorted list
// 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.c
#include <stdlib.h>
#include "ListManagement.h"
Node* GetNode()
{
............................
}
Example
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
// ListManagement.c
#include <stdlib.h>
#include "ListManagement.h"
Node* GetNode()
{
............................
}
Example
A sorted list
void FreeNode(Node* ptr)
{
............................
}
Node* GetNode()
{
Node *pNode;
Node* Insert(Node* ptrRoot, int itemData)
{
............................
}
Node* Delete(Node* ptrRoot, int itemData)
{
............................
}
pNode = (Node*) malloc(sizeof(Node));
if(pNode) pNode->nextNode = NULL;
void PrintList(Node* ptrRoot)
{
return pNode;
............................
}
}
void FreeList(Node* ptrRoot)
{
............................
}
// ListManagement.c
#include <stdlib.h>
#include "ListManagement.h"
Node* GetNode()
{
............................
}
Example
A sorted list
void FreeNode(Node* ptr)
{
............................
}
Node* Insert(Node* ptrRoot, int itemData)
{
............................
}
void FreeNode(Node* ptr)
{ int itemData)
Node* Delete(Node* ptrRoot,
{
free(ptr);
............................
}
}
void PrintList(Node* ptrRoot)
{
............................
}
void FreeList(Node* ptrRoot)
{
............................
}
// ListManagement.c
#include <stdlib.h>
#include "ListManagement.h"
Node* GetNode()
{
............................
}
A sorted list
Example
void FreeNode(Node* ptr)
{
............................
}
Node* Insert(Node* ptrRoot, int itemData)
{
............................
}
Node* Delete(Node* ptrRoot, int itemData)
{
............................
}
rootList
void PrintList(Node* ptrRoot)
{
............................
}
100
102
void FreeList(Node* ptrRoot)
{
............................
}
235
145
440
888
// ListManagement.c
#include <stdlib.h>
#include "ListManagement.h"
Node* GetNode()
{
............................
}
A sorted list
Example
void FreeNode(Node* ptr)
{
............................
}
Node* Insert(Node* ptrRoot, int itemData)
{
............................
}
Node* Delete(Node* ptrRoot, int itemData)
{
............................
}
rootList
void PrintList(Node* ptrRoot)
{
............................
}
100
102
void FreeList(Node* ptrRoot)
{
............................
}
235
145
440
888
// ListManagement.c
#include <stdlib.h>
#include "ListManagement.h"
Node* GetNode()
{
............................
}
A sorted list
Example
void FreeNode(Node* ptr)
{
............................
}
Node* Insert(Node* ptrRoot, int itemData)
{
............................
}
Node* Delete(Node* ptrRoot, int itemData)
{
............................
}
rootList
void PrintList(Node* ptrRoot)
{
............................
}
100
102
void FreeList(Node* ptrRoot)
{
............................
}
235
95
440
888
// ListManagement.c
#include <stdlib.h>
#include "ListManagement.h"
Node* GetNode()
{
............................
}
A sorted list
Example
void FreeNode(Node* ptr)
{
............................
}
Node* Insert(Node* ptrRoot, int itemData)
{
............................
}
Node* Delete(Node* ptrRoot, int itemData)
{
............................
}
rootList
void PrintList(Node* ptrRoot)
{
............................
}
100
102
void FreeList(Node* ptrRoot)
{
............................
}
235
95
440
888
// ListManagement.c
Node* Insert(Node* ptrRoot, int itemData)
#include <stdlib.h>
#include "ListManagement.h"
{
Node* GetNode()
Node* pNodeInsert = GetNode();
{
Node* pCurNode, *pPreviousNode;
............................
}
Example
A sorted list
pCurNode = ptrRoot;
void FreeNode(Node* ptr)
pPreviousNode = NULL;
{
............................
}
pNodeInsert->data = itemData;
Node* Insert(Node* ptrRoot, int itemData)
while(pCurNode != NULL && itemData > pCurNode->data){
{
pPreviousNode = pCurNode;
............................
}
pCurNode = pCurNode->nextNode;
};
Node* Delete(Node* ptrRoot, int itemData)
if(pPreviousNode != NULL){
{
............................
pPreviousNode->nextNode = pNodeInsert;
}
pNodeInsert->nextNode = pCurNode;
return ptrRoot;
void PrintList(Node* ptrRoot)
}
{
............................
else{
}
pNodeInsert->nextNode = pCurNode;
return pNodeInsert;
void FreeList(Node* ptrRoot)
}
{
............................
}
}
// ListManagement.c
#include <stdlib.h>
#include "ListManagement.h"
Delete(rootList, 235)
Node* GetNode()
{
............................
}
rootList
A sorted list
Example
void FreeNode(Node* ptr)
{
............................
100
102
}
235
Node* Insert(Node* ptrRoot, int itemData)
{
............................
}
Node* Delete(Node* ptrRoot, int itemData)
{
............................
}
void PrintList(Node* ptrRoot)
{
............................
}
void FreeList(Node* ptrRoot)
{
............................
}
440
888
// ListManagement.c
#include <stdlib.h>
#include "ListManagement.h"
Delete(rootList, 235)
Node* GetNode()
{
............................
}
rootList
A sorted list
Example
void FreeNode(Node* ptr)
{
............................
100
102
}
235
Node* Insert(Node* ptrRoot, int itemData)
{
............................
}
Node* Delete(Node* ptrRoot, int itemData)
{
............................
}
void PrintList(Node* ptrRoot)
{
............................
}
void FreeList(Node* ptrRoot)
{
............................
}
440
888
// ListManagement.c
#include <stdlib.h>
#include "ListManagement.h"
Delete(rootList, 100)
Node* GetNode()
{
............................
}
rootList
A sorted list
Example
void FreeNode(Node* ptr)
{
............................
100
102
}
235
Node* Insert(Node* ptrRoot, int itemData)
{
............................
}
Node* Delete(Node* ptrRoot, int itemData)
{
............................
}
void PrintList(Node* ptrRoot)
{
............................
}
void FreeList(Node* ptrRoot)
{
............................
}
440
888
// ListManagement.c
#include <stdlib.h>
#include "ListManagement.h"
Delete(rootList, 100)
Node* GetNode()
{
............................
}
rootList
A sorted list
Example
void FreeNode(Node* ptr)
{
............................
100
102
}
235
Node* Insert(Node* ptrRoot, int itemData)
{
............................
}
Node* Delete(Node* ptrRoot, int itemData)
{
............................
}
void PrintList(Node* ptrRoot)
{
............................
}
void FreeList(Node* ptrRoot)
{
............................
}
440
888
// ListManagement.c
#include <stdlib.h>
Node* Delete(Node* ptrRoot, int itemData)
#include "ListManagement.h"
{
Node *pCurNode, *pPreviousNode;
Node* GetNode()
{
............................
pCurNode = ptrRoot;
}
Example
pPreviousNode = NULL;
A sorted list
void FreeNode(Node* ptr)
{
while(pCurNode != NULL && itemData != pCurNode->data){
............................
pPreviousNode = pCurNode;
}
pCurNode = pCurNode->nextNode;
Node* Insert(Node* };
ptrRoot, int itemData)
{
............................
if(pCurNode == NULL) return ptrRoot;
}
!= NULL){
Node* Delete(Node* if(pPreviousNode
ptrRoot, int itemData)
{
pPreviousNode->nextNode = pCurNode->nextNode;
............................
FreeNode(pCurNode);
}
return ptrRoot;
} ptrRoot)
void PrintList(Node*
{
else{
............................
ptrRoot = pCurNode->nextNode;
}
FreeNode(pCurNode);
void FreeList(Node* ptrRoot)
return ptrRoot;
{
}
............................
}
}
// ListManagement.c
#include <stdlib.h>
#include "ListManagement.h"
void PrintList(Node* ptrRoot)
{
A
sorted
list
while(ptrRoot != NULL){
void FreeNode(Node*
ptr)
printf("%d%s",
ptrRoot->data,
{
............................
ptrRoot->nextNode ? "-->" : "");
}
ptrRoot = ptrRoot->nextNode;
Node* Insert(Node* ptrRoot, int itemData)
}
{
............................
printf("\n");
}
} Delete(Node* ptrRoot, int itemData)
Node*
Node* GetNode()
{
............................
}
Example
{
............................
}
void PrintList(Node* ptrRoot)
{
............................
}
void FreeList(Node* ptrRoot)
{
............................
}
// ListManagement.c
#include <stdlib.h>
#include "ListManagement.h"
Node* GetNode()
{
............................
}
A sorted
list
Example
void FreeList(Node*
ptrRoot)
{
void FreeNode(Node* ptr)
{
............................
}
Node* pTemp;
while(ptrRoot != NULL){
pTemp = ptrRoot->nextNode;
FreeNode(ptrRoot);
Node* Delete(Node* ptrRoot, int itemData)
{
ptrRoot = pTemp;
............................
}
}
} ptrRoot)
void PrintList(Node*
Node* Insert(Node* ptrRoot, int itemData)
{
............................
}
{
............................
}
void FreeList(Node* ptrRoot)
{
............................
}
//main.c
#include <stdio.h>
#include "ListManagement.h"
enum FUNCTON {INSERT=1, DELETE, EXIT};
Example
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:
...................
}
}
}
A sorted list
//main.c
#include <stdio.h>
#include "ListManagement.h"
enum FUNCTON {INSERT=1, DELETE, EXIT};
Example
void ShowMenu()
{
printf("1. Insert\n"
"2. Delete\n"
"3. Exit\n");
}
A sorted list
main()
{
int function, data, end = 0;
Node* rootList = NULL;
printf("Enter data for insertion:");
ShowMenu();
scanf("%d", &data);
while(!end){
printf("? ");
rootList = Insert(rootList,
scanf("%d", &function);
PrintList(rootList);
break;
switch(function){
case INSERT:
...................
case DELETE:
...................
case EXIT:
...................
}
}
}
data);
//main.c
#include <stdio.h>
#include "ListManagement.h"
enum FUNCTON {INSERT=1, DELETE, EXIT};
Example
void ShowMenu()
{
printf("1. Insert\n"
"2. Delete\n"
"3. Exit\n");
}
A sorted list
main()
{
int function, data, endprintf("Enter
= 0;
Node* rootList = NULL;
data for deletion:");
scanf("%d", &data);
ShowMenu();
rootList = Delete(rootList, data);
while(!end){
PrintList(rootList);
printf("? ");
scanf("%d", &function);
break;
switch(function){
case INSERT:
...................
case DELETE:
...................
case EXIT:
...................
}
}
}
//main.c
#include <stdio.h>
#include "ListManagement.h"
enum FUNCTON {INSERT=1, DELETE, EXIT};
Example
void ShowMenu()
{
printf("1. Insert\n"
"2. Delete\n"
"3. Exit\n");
}
A sorted list
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;
This is a non-recursive version.
Example
A sorted list
ListManagement.h
ListManagement.c
Main.c
LinkList.exe
Recursive version.
Example
A sorted list
ListManagement.h
RecursiveListManagement.c
Main.c
LinkList.exe
C Program Design
C Data Structures
Stacks
Stacks
Stack
–
–
–
–
push
–
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
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
Stack Implementation Array
stack
sp = stack + sizeStack;
numItems = 0;
sizeStack
stack = (int*) malloc(sizeof(int) * sizeStack);
sp
numItems=0
.
Memory
.
.
Stack Implementation Array
.
Memory
.
.
sp
50
sizeStack
stack
numItems=1
Push(50);
Stack Implementation Array
.
Memory
.
.
sp
41
50
sizeStack
stack
numItems=2
Push(50);
Push(41);
Stack Implementation Array
.
Memory
.
.
sp
38
41
50
sizeStack
stack
numItems=3
Push(50);
Push(41);
Push(38);
Stack Implementation Array
.
Memory
.
.
sp
38
41
50
sizeStack
int Push(int val)
{
if(numItems==sizeStack){
return 0;
}
else{
*--sp = val;
numItems++;
return 1;
}
}
stack
numItems=3
Push(50);
Push(41);
Push(38);
Stack Implementation Array
Push(50);
Push(41);
Push(38);
.
Memory
.
.
sp
38
41
50
sizeStack
data38
numItems=2
Pop(&data);
stack
Stack Implementation Array
Push(50);
Push(41);
Push(38);
int Pop(int* ptrData)
{
if(numItems==0){
return 0;
}
else{
*ptrData = *sp++;
numItems--;
return 1;
}
}
.
Memory
.
.
sp
38
41
50
sizeStack
data38
numItems=2
Pop(&data);
stack
Stack Implementation Array
arrayStack.exe
.
Memory
.
.
sp
38
41
50
sizeStack
arrayStack.c
numItems=2
stack
Stack Implementation Linked List
stack
45
27
62
155
42
Stack Implementation Linked List
Push(&stack, 145)
stack
45
145
27
62
155
42
Stack Implementation Linked List
Push(&stack, 888)
stack
45
27
888
145
62
155
42
data888
Stack Implementation Linked List
Pop(&stack, &data)
stack
45
27
888
145
62
155
42
data888
Stack Implementation Linked List
Pop(&stack, &data)
stack
45
145
27
62
155
42
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*);
#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)
{
...............
}
Stack Implementation Linked List
#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)
{
...............
}
Stack Implementation Linked List
Node* GetNode()
{
Node *pNode;
pNode = (Node*) malloc(sizeof(Node));
if(pNode) pNode->nextNode = NULL;
return pNode;
}
#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)
{
...............
}
Stack Implementation Linked List
void FreeNode(Node* ptr)
{
free(ptr);
}
#include <stdlib.h>
#include "stackManagement.h"
Node* GetNode()
{
...............
}
void FreeNode(Node* ptr)
{
...............
}
void Push(Node** ptrStack, int val)
{
...............
}
void Push(Node**
ptrStack, int val)
int Pop(Node** ptrStack,
int* ptrData)
{
{
...............
Node* pNode = GetNode();
}
int IsStackEmpty(Node* stack)
if(NULL==pNode){
{
printf("No node available.\n");
return stack == NULL;
return;
}
void PrintStack(Node* stack)
}
{
pNode->data = val;
...............
}
pNode->nextNode = *ptrStack;
void FreeStack(Node* stack)
*ptrStack = pNode;
{
............... }
}
Stack Implementation Linked List
#include <stdlib.h>
#include "stackManagement.h"
Node* GetNode()
{
...............
}
void FreeNode(Node* ptr)
{
...............
}
void Push(Node** ptrStack, int val)
{
...............
}
int Pop(Node**
int Pop(Node** ptrStack,
int* ptrData) ptrStack, int* ptrData)
{
{
...............
Node* pNode;
}
int IsStackEmpty(Node* stack)
{
pNode = *ptrStack;
return stack == NULL;
if(pNode == NULL) return NULL;
}
void PrintStack(Node* stack)
*ptrData = pNode->data;
{
*ptrStack = pNode->nextNode;
...............
}
FreeNode(pNode);
void FreeStack(Node* stack)
return 1;
{
............... }
}
Stack Implementation Linked List
Stack Implementation Linked List
StackManagement.h
StackManagement.c
StackMain.c
Stack.exe
Application:
Reverse Polish Calculator
Infix notation:
(1 - 2) * (4 + 5)
Reverse Polish notation:
1 2 - 4 5 + *
Reverse Polish Calculator
1 2 - 4 5 + *
1
Reverse Polish Calculator
1 2 - 4 5 + *
2
1
Reverse Polish Calculator
1 2 - 4 5 + *
2
1
1 - 2 -1
Reverse Polish Calculator
1 2 - 4 5 + *
1 - 2 -1
-1
Reverse Polish Calculator
1 2 - 4 5 + *
4
-1
Reverse Polish Calculator
1 2 - 4 5 + *
5
4
-1
Reverse Polish Calculator
1 2 - 4 5 + *
5
4
-1
4 + 5 9
Reverse Polish Calculator
1 2 - 4 5 + *
9
-1
4 + 5 9
Reverse Polish Calculator
1 2 - 4 5 + *
9
-1
-1 * 9 -9
Reverse Polish Calculator
1 2 - 4 5 + *
-9
-1 * 9 -9
Reverse Polish Calculator
1 2 - 4 5 + *
-9
-9
C Program Design
C Data Structures
Queues
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)
Queue Implementation Array
= 0
queue
0
1
2
queueHead
queueTail
queue = (int*) malloc(sizeof(int) * sizeQueue);
queueHead = queueTail = 0;
numItems = 0;
.
Memory
.
.
sizeQueue
numItems
sizeQueue - 1
Enqueue(35);
Queue Implementation Array
= 1
0
queue
queueHead
35
queueTail
int Enqueue(int val)
{
if(numItems==sizeQueue) return 0;
queue[queueTail++] = val;
queueTail %= sizeQueue;
numItems++;
return 1;
}
.
Memory
.
.
0
1
2
sizeQueue
numItems
sizeQueue - 1
Enqueue(35);
Enqueue(99);
Queue Implementation Array
= 2
1
queue
queueHead
35
99
queueTail
int Enqueue(int val)
{
if(numItems==sizeQueue) return 0;
queue[queueTail++] = val;
queueTail %= sizeQueue;
numItems++;
return 1;
}
.
Memory
.
.
0
1
2
sizeQueue
numItems
sizeQueue - 1
Dequeue(&data);
data35
Queue Implementation Array
= 2
1
queue
queueHead
35
99
queueTail
int Dequeue(int* ptrData)
{
if(numItems==0) return 0;
*ptrData = queue[queueHead++];
queueHead %= sizeQueue;
numItems--;
return 1;
}
.
Memory
.
.
0
1
2
sizeQueue
numItems
sizeQueue - 1
Queue Implementation Array
arrayQueue.c
arrayQueue.exe
Queue Implementation Linked List
typedef struct _queue {
Node* queueHead;
Node* queueTail;
} Queue;
queueTail
queueHead
45
27
62
155
42
Queue Implementation Linked List
Enqueue(&queue, 145)
145
queueHead
45
queueTail
27
62
155
42
Queue Implementation Linked List
Enqueue(&queue, 145)
Enqueue(&queue, 67)
145
queueHead
45
queueTail
27
62
67
155
42
Queue Implementation Linked List
Dequeue(&queue, &data)
queueTail
data45
145
queueHead
45
27
62
67
155
42
Queue Implementation Linked List
QueueManagement.h
QueueManagement.c
ListQueue.c
ListQueue.exe
C Program Design
C Data Structures
Trees
root
300
140
Trees
282
230
405
290
809
510
Tree nodes contain two or more links
–
119
750
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
760
900
root
300
140
Trees
282
230
405
290
809
510
Tree nodes contain two or more links
–
119
750
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
760
900
root
300
140
Binary Trees
750
119
282
230
405
290
809
510
760
struct treeNode {
int data;
struct treeNode *left;
struct treeNode *right;
};
Root
Data
TL
900
TR
left
right
Binary Search Trees
root
300
750
140
230
809
405
282
119
290
510
760
900
Tree Traversals
Infix:
F
A B C D E F G H I
B
A
G
D
Prefix:
I
F B A D C E G I H
C
E
H
Postfix:
A C E D B H I G F
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*);
Tree node maintenance
void InsertTree(TreeNode**, int);
int BinSearch(TreeNode*, int);
void DeleteTree(TreeNode**, int);
Main operations
void InfixOrder(TreeNode*);
void PrefixOrder(TreeNode*);
void PostfixOrder(TreeNode*);
Tree Traversal
Binary Tree Implementation
TreeManagement.h
TreeManagement.c
TreeMain.c
Tree.exe