Dr. Na Li CSE @ UTA April 9, 2013 Stack and Queue http://www.thelearningpoint.net/computerscience/data-structures-stacks--with-cprogram-source-code http://www.thelearningpoint.net/computerscience/data-structures-queues--with-cprogram-source-code In Programming, we try to match data type to the needs of a programming problem. For example, What constitutes a type? For example, To define your own data type, provide ◦ int – the number of shoes ◦ float or double – the average cost per pair of shoes ◦ Two kinds of information – a set of properties and a set of operations. ◦ int – the properties of integers and operations , like arithmetic operations (addition, subtraction…) ◦ a way to store the data ◦ Ways of manipulating the data Abstract Data Type (ADT) ◦ A formal abstract description of the type’s properties and of the operations you can perform on the type. After having the ADT for a type, we can define prototypes and implement them. A queue is a list with two special properties. First, new items can be added only to the end of the list. Second, items can be removed from the list only at the beginning. A queue is a first in, first out (FIFO) data form. Type Name: Queue Type Properties: ◦ Can hold an ordered sequence of items Type Operations: ◦ ◦ ◦ ◦ ◦ ◦ ◦ Initialize queue to empty Determine whether queue is empty Determine whether queue is full Determine number of items in the queue Add item to rear of queue Remove item from front of queue Empty the queue //Initialize queue to empty void InitializeQueue(Queue *pq); //Determine whether queue is empty int QueueIsEmpty(Queue *pq); //Determine whether queue id full int QueueIsFull(Queue * pq); //Determine number of items in the queue int QueueItemCount(Queue * pq); //Add item to rear of queue int EnQueue(Item item, Queue *pq); //Remove item from front of queue int DeQueue(Item * pitem, Queue * pq); //Empty the queue void EmptyTheQueue(Queue * pq); #include<stdio.h> #define MAXQUEUE 10 typedef int Item; typedef struct node { Item item; struct node * next; }Node; typedef struct queue { Node * front; Node * rear; int items; }Queue; //typedef enables you to create your own name for a type. void InitializeQueue(Queue *pq) { pq->front = pq->rear = NULL; pq->items = 0; } int QueueIsEmpty(Queue *pq) { return pq->items == 0; } int QueueIsFull(Queue * pq) { return pq->items == MAXQUEUE; } int QueueItemCount(Queue * pq) { return pq->items; } int EnQueue(Item item, Queue * pq) { Node * pnew; if(QueueIsFull(pq)) return 0; pnew = (Node *) malloc(sizeof(Node)); if(pnew == NULL) { printf("Unlable to allocate memory!\n"); exit(1); } //CopyToNode(item, pnew); pnew->next = NULL; if(QueueIsEmpty(pq)) pq->front = pnew; else pq->rear->next = pnew; pq->rear = pnew; pq->items++; return 1; } void CopyToNode(Item item, Node * pn) { pn->item = item; } int DeQueue(Item * pitem, Queue * pq) { Node * pt; if(QueueIsEmpty(pq)) return 0; CopyToItem(pq->front,pitem); pt=pq->front; pq->front = pq->front->next; free(pt); pq->items--; if(pq->items == 0) pq->rear = NULL; return 1; } void CopyToItem(Node * pn, Item * pitem) { *pitem = pn->item; } void EmptyTheQueue(Queue * pq) { Item dummy; while(!QueueIsEmpty(pq)) DeQueue(&dummy, pq); } int main(void) { Queue line; Item temp; char ch; InitializeQueue(&line); puts("Testing the Queue interface. Type a to add a value,"); puts("type d to delete a value, and type q to quit."); while((ch = getchar ())!='q') { if(ch != 'a' && ch != 'd') continue; if(ch == 'a') { printf("Integer to add:"); scanf("%d", &temp); if(!QueueIsFull(&line)) { printf("Putting %d into queue\n", temp); EnQueue(temp, &line); } else puts("Queue is full!"); } else { if(QueueIsEmpty(&line)) puts("Nothing to delete!"); else { DeQueue(&temp, &line); printf("Removing %d from queue\n", temp); } } printf("%d items in queue\n", QueueItemCount(&line)); puts("Type a to add, d to delete, q to quit:"); } EmptyTheQueue(&line); puts("Bye!"); return 0; } Allows access to only the last item inserted. An item is inserted or removed from the stack from one end called the “top” of the stack. This mechanism is called Last-In-First-Out (LIFO). Placing a data item on the top is called “pushing”, while removing an item from the top is called “popping” it. push and pop are the primary stack operations. typedef struct node { char s[SIZE]; struct node *next; }Node; typedef struct stack { Node * head; int stackCount; }Stack; void CopyToNode(char * buffer, Node * pn); void CopyToItem(Node * pn, char * buffer); int isfull(Stack * skp); int isempty(Stack * skp); int push(Stack * skp, char buffer[]); int pop(Stack * skp, char buffer[]); void initializeStack(Stack * skp); int countsize(Stack * pq); #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAXSTACK 10 #define SIZE 50 typedef struct node { char s[SIZE]; struct node *next; }Node; typedef struct stack { Node * head; int stackCount; }Stack; void CopyToNode(char * buffer, Node * pn); void CopyToItem(Node * pn, char * buffer); int isfull(Stack * skp); int isempty(Stack * skp); int push(Stack * skp, char buffer[]); int pop(Stack * skp, char buffer[]); void initializeStack(Stack * skp); int countsize(Stack * pq); int main(void) { Stack mystack; char ch; char string[50]; initializeStack(&mystack); puts("Testing the Stack interface. Type a to push a value,"); puts("type d to pop a value, and type q to quit."); while((ch = getchar ())!='q') { if(ch != 'a' && ch != 'd') continue; if(ch == 'a') { printf("String to push:"); scanf("%s", string); if(!isfull(&mystack)) { printf("Pushing %s into stack\n", string); push(&mystack, string); } else puts("Stack is full!"); } else { if(isempty(&mystack)) puts("Nothing to pop!"); else { pop(&mystack, string); printf("Removing %s from queue\n", string); } } printf("%d items in stack\n", countsize(&mystack)); puts("Type a to push, d to pop, q to quit:"); } puts("Bye!"); return 0; } void initializeStack(Stack *pq) { pq->head= NULL; pq->stackCount = 0; } int isempty(Stack *pq) { return pq->stackCount == 0; } int isfull(Stack * pq) { return pq->stackCount == MAXSTACK; } int countsize(Stack * pq) { printf("%d test\n", pq->stackCount); return pq->stackCount; } int push(Stack * pq, char buffer[]) { Node * pnew; if(isfull(pq)) return 0; pnew = (Node *) malloc(sizeof(Node)); if(pnew == NULL) { printf("Unlable to allocate memory!\n"); exit(1); } CopyToNode(buffer, pnew); pnew->next = pq->head; pq->head = pnew; pq->stackCount++; return 1; } void CopyToNode(char * buffer, Node * pn) { strcpy(pn->s, buffer); } int pop(Stack * pq, char buffer[]) { Node * pt; if(isempty(pq)) return 0; CopyToItem(pq->head,buffer); pt=pq->head; pq->head = pt->next; free(pt); pq->stackCount--; return 1; } void CopyToItem(Node * pn, char * buffer) { strcpy(buffer, pn->s); } Array based implementation Linked-list based implementation http://www.w3professors.com/Pages/Course s/Data-Structure/Programs/Data-StructurePrograms.html#StackA Data Structures and Algorithms: Annotated Reference with Examples from Barnett and Del Tongo (a free e-book) ◦ http://dotnetslackers.com/projects/Data-Structures-AndAlgorithms/ C data structures from cprogramminglanguage.net ◦ http://cprogramminglanguage.net/c-datastructure.aspx ◦