Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<malloc.h>
- struct node
- {
- int data;
- struct node *ptr;
- };
- struct node * addq(struct node *,int);
- void display(struct node *);
- struct node * deleteq(struct node *);
- struct node *head,*rear;
- int main()
- {
- int choice,item;
- rear=NULL;
- for(;;)
- {
- printf("\n Enter the choice : 1-Insert 2- Display");
- printf(" \n 3-Delete 4-Exit : ");
- scanf("%d",&choice);
- switch (choice)
- {
- case 1 :printf(" \n Enter the number to be added to queue");
- scanf("%d",&item);
- rear=addq(rear,item);
- break;
- case 2 :display(head);
- break;
- case 3 :head=deleteq(head);
- break;
- default:exit(0);
- break;
- }
- }
- }
- struct node * addq(struct node *rear,int item)
- {
- struct node *newnode;
- newnode=(struct node *)malloc(sizeof(struct node));
- newnode->data=item;
- newnode->ptr=NULL;
- if(rear==NULL)
- {
- head=newnode;
- return newnode;
- else
- {
- rear->ptr=newnode;
- return newnode;
- }
- }
- void display(struct node *head)
- {
- while(head!=NULL)
- {
- printf("%d \t",head->data);
- head=head->ptr;
- }
- }
- struct node * deleteq(struct node *head)
- {
- struct node *temp;
- if(head==NULL)
- {
- printf("\n The queue is empty");
- return head;
- }
- else
- {
- temp=head;
- head=head->ptr;
- printf(" \n The deleted item is : %d",temp->data);
- free(temp);
- return head;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement