Queue Implementation Using Linked List

  • October 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Queue Implementation Using Linked List as PDF for free.

More details

  • Words: 381
  • Pages: 2
QUEUE IMPLEMENTATION USING LINKED LIST #include<stdio.h> #include #include struct qnode { int data; int prio; struct qnode *next; }; void quein(struct qnode**,struct qnode**,int ,int); int quedel(struct qnode**,struct qnode**,int *,int *); int main(void) { int tab[10]={2,8,3,5,4,9,6,7,1,0}; struct qnode*first=NULL; struct qnode*last=NULL; int val,prio,i; clrscr(); for(i=0;i<10;i++) { val=tab[i],prio=i; printf("\n Inaerting value : %d with priority :%d",prio,val); quein(&first,&last,val,prio); } printf("=-=\n"); for(i=0;i<11;i++) { val=tab[i],prio=i; if(quedel(&first,&last,&val,&prio)!=-1) printf("Deleting value:%d with priority:%d \n,prio,val"); } getch(); return(0); } int quedel(struct qnode**first,struct qnode**last,int *prio,int *val) { struct qnode*temp=NULL; if((NULL == *last) && (*last == *first)) { fprintf(stderr,"Empty queue...............\n"); return -1; } *va l=(* f i r s t ) - >data , *p r i o=(* f i r s t ) - >pr io ; temp=* f i r s t , * f i r s t=(* f i r s t ) - >next ; i f ( * l a s t==temp)

* l as t=(* l as t ) - >next ; f ree ( temp) ; re tu rn 0 ; } vo id que in ( s t ruc t qnode * * f i r s t , s t ruc t qnode ** l as t , i n t pr io , i n t va l ) { s t ruc t qnode * temp=NULL ; s t ruc t qnode * temp1=NULL ; temp=(s t ruc t qnode* ) mal loc ( s i zeo f ( s t ruc t qnode) ) ; temp->data=va l ; temp->pr io=pr io ; temp->next=NULL ; i f ( * l a s t==NULL ) { * l as t=temp; * f i r s t=* las t ; } e l se { i f ( ( * l as t ) - >pr io<pr io ) { temp->next=* f i r s t ; * f i r s t=temp; } e l se { i f ( ( * l as t ) - >pr io>pr io ) { ( * l as t ) - > next = temp; * l as t=temp; } e l se { { temp1=* f i r s t ; whi le ( ( temp1->next ) - >pr io>=pr io ) { temp1=temp1->next ; } temp1->next=temp1->next ; temp1->next=temp; } } } }}

Related Documents