DOC PREVIEW
UCF COP 3502H - Stacks and Queues- implementation

This preview shows page 1-2 out of 5 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 5 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Stacks and Queues- implementationStacks and Queues- implementationCode for manipulations of stacks and queues. Functions are provided for push, pop, enqueue and dequeue operations. The stackis indexed by stacktop, while the queue is being maintained by twoindices – qfront and qback.#include <stdio.h>struct node{ int data; struct node *next;};void push(struct node**stacktop,int d );int pop(struct node**stacktop);void enqueue(struct node**qfront,int d , struct node**qback);int dequeue(struct node**qfront, struct node**qback);void main( ) { int num; int dd, ii; struct node* first = (struct node*) (malloc(sizeof(struct node))); struct node* front = (struct node*) (malloc(sizeof(struct node))); struct node* back = (struct node*) (malloc(sizeof(struct node))); front = NULL; back= NULL; first = NULL;for (ii= 1; ii<=3; ii++) {printf("\n next number? =");scanf("%d",&dd);push(&first,dd);}for (ii= 1; ii<=4; ii++){dd = pop(&first);if (dd !=-999) printf("\n popped number =%d", dd);printf("\n\n");}dd=0;for (ii= 1; ii<=4; ii++){printf("\n give number for queue? ="); scanf("%d",&dd);enqueue(&front,dd,&back);printf("\n front value is %d",front->data);}for (ii= 1; ii<=5; ii++){dd = dequeue(&front,&back);if (dd != -999)printf("\n number from queue =%d",dd); }printf("\n");}//end of main function// stack functions:pushvoid push(struct node**stacktop,int d ){ struct node* pNew = (struct node*) (malloc(sizeof(struct node))); pNew-> data = d ; pNew->next = *stacktop; *stacktop = pNew ;}//Pop Function returns the data from top element, and frees that //element from the stack.int pop(struct node* *stacktop){ struct node* temp; int d; if(*stacktop== NULL) { printf("\nstack empty\n"); d = -999; } else { d = temp->data; temp = *stacktop; *stacktop = temp->next ; free (temp); } return d;}// Enqueue function : qfront and qback point to first element of the queue//and the last element of queue respectively.//Postcondition: if queue is empty, both qfront and qback point to //the new element added. Otherwise, the element is added to the //end of the queue and qback is updated.void enqueue(struct node**qfront,int d , struct node**qback){ struct node* pNew = (struct node*) malloc(sizeof(struct node)); pNew-> data = d ; pNew->next = NULL; if ((*qfront ==NULL) && (*qback == NULL)) { *qfront = pNew; *qback = pNew; } else if (*qback != NULL) { (*qback) ->next = pNew; (*qback )= pNew; } }//preconditon: qfront and qback must be valid pointers to the first //and elements of the queue//post condition: the data in the first element is returned and qfront//updated. If it is the only element in the queue then after dequeue //operation the queue is empty (qfront is NULL ) and so qback is //also set to null.int dequeue(struct node**qfront, struct node**qback){ struct node* temp; int d; if (*qfront== NULL) {printf("\nqueue is empty\n"); d = -999; } else { temp = *qfront; d = temp->data; *qfront = temp->next; if ((*qfront) == NULL) *qback = NULL; free (temp); } return


View Full Document

UCF COP 3502H - Stacks and Queues- implementation

Download Stacks and Queues- implementation
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Stacks and Queues- implementation and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Stacks and Queues- implementation 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?