Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //minimum priority queue
- #include<stdio.h>
- #include<conio.h>
- #include<stdlib.h>
- typedef struct node
- {
- int data;
- struct node *next;
- }node;
- typedef struct
- {
- node *first;
- }head;
- void insertend(head *t,int ele)
- {
- node *p,*q,*r;
- p = (node*)malloc(sizeof(node));
- p->data=ele;
- p->next=NULL;
- if(t->first==NULL)
- {
- t->first=p;
- t->first->next=t->first;
- return;
- }
- q = t->first;
- if(p->data<=t->first->data) //insertion at first node
- {
- while(q->next!=t->first)
- q = q->next;
- p->next = t->first;
- t->first = p;
- q->next = p;
- return;
- }
- else
- {
- q = t->first; //insertion at after first node
- while(p->data>q->next->data&&q->next!=t->first)
- {
- q = q->next;
- }
- p->next= q->next;
- q->next = p;
- return;
- }
- return;
- }
- int count(head *t)
- {
- node *q;
- int c=0;
- if(t->first==NULL)
- {
- printf("Linked List is empty");
- return -1;
- }
- q=t->first;
- do
- {
- c=c+1 ;
- q=q->next;
- }while(q!=t->first);
- return c;
- }
- void deletebeg(head *t)
- {
- int z;
- node *p,*q;
- if(t->first==NULL)
- {
- printf("Linked list is Empty");
- return;
- }
- if(count(&(*t))==1)
- {
- z = t->first->data;
- t->first = NULL;
- printf("deleted element in Linked list %d",z);
- return;
- }
- q = t->first;
- p = q;
- t->first = t->first->next;
- while(q->next!=p)
- q = q->next;
- q->next = t->first;
- z = p->data;
- printf("deleted element in Linked list %d",z);
- return;
- }
- void display(head *t)
- {
- node *q;
- if(t->first==NULL)
- {
- printf("\nLinked list is empty");
- return;
- }
- q = t->first;
- printf("\nElement of Linked list is given below\n");
- do
- {
- printf("%d\t",q->data);
- q = q->next;
- }while(q!=t->first);
- return;
- }
- int main()
- {
- head x;
- int ch,ele;
- x.first=NULL;
- printf("\nImplementation of Circular Linked list with function insertend,display,deletebeg,count\n");
- while(1)
- {
- printf("\nenter choice \n1.InsertEnd 2.Deletebeg 3.Count 4.display 5.Exit\n");
- scanf("%d",&ch);
- if(ch==5)
- {
- break;
- }
- else
- {
- switch(ch)
- {
- case 1:printf("\nenter element to insert ");
- scanf("%d",&ele);
- insertend(&x,ele);
- break;
- case 2:deletebeg(&x);
- break;
- case 3:
- printf("Total no. of elements in Linked list is %d",count(&x));
- break;
- case 4:display(&x);
- break;
- default:printf("\ninvalid input");
- }
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment