Write a C program to simulate an ordinary queue using a singly linked list.
Jawapan:
#include<stdio.h> #include<conio.h> #include<alloc.h> #include<stdlib.h> struct node { int data; struct node *link; }; struct node *cur,*first,*last; void create(); void insert_rear(); void delete_front(); void display(); void create() { printf("\n Enter the first element: "); cur=(struct node *)malloc(sizeof(struct node)); scanf("%d",&cur->data); cur->link=NULL; first=cur; last=cur; } void insert_rear() { printf("\n Enter the next element: "); cur=(struct node *)malloc(sizeof(struct node)); scanf("%d",&cur->data); cur->link=NULL; last->link=cur; last=cur; } void delete_front() { if(first==NULL) { printf("\t\n Queue is empty\n"); } else { cur=first; first=first->link; cur->link=NULL; printf("\n Deleted element is %d\n",cur->data); free(cur); } } void display() { cur=first; printf("\n"); while(cur!=NULL) { printf("\t%d",cur->data); cur=cur->link; } } void main() { int ch; system("cls"); create(); display(); while(1) { printf("\n--------------------------"); printf("\n 1.INSERT \n 2.DELETE \n"); printf("\n Enter your choice >> "); scanf("%d",&ch); switch(ch) { case 1: insert_rear(); display(); break; case 2: delete_front(); display(); break; default: exit(0); } } }
0 ulasan:
Catat Ulasan
Selamat datang ke 0x2013LΣΣT. Sekiranya anda mempunyai persoalan, pandangan, permintaan, bantuan, cadangan dan sebagainya. Tinggalkan pesanan anda ke dalam kotak komen. Terima kasih !
- http://0x2013.blogspot.com -