C   44
Untitled
Guest on 25th May 2023 06:05:47 PM


  1. Circular Queue (Array)
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define MAX 30
  5.  
  6. struct queue {
  7.     int que[MAX];
  8.     int front;
  9.     int rear;
  10. };
  11.  
  12. struct queue items;
  13. struct queue *q = &items;
  14.  
  15. void enque();
  16. void deque();
  17. void display();
  18. int Empty();
  19. int Full();
  20.  
  21. int main(int argc, char *argv[]) {
  22.     printf("\n%d\n%s", argc, argv[0]);
  23.     printf("\nCircular implementation of Queue");
  24.     int ch = 1;
  25.     printf("\n\nChoose operation:\n1. enque\n2. deque\n3. display\n0. Exit ");
  26.     scanf("%d", &ch);
  27.     do {
  28.         switch(ch) {
  29.             case 1: enque(); break;
  30.             case 2: deque(); break;
  31.             case 3: display(); break;
  32.             case 0: exit(0);
  33.             default: printf("\nInvalid input.\nClosing..........");
  34.                      exit(0);
  35.         }
  36.         printf("\nfront: %d rear: %d", q->front, q->rear);
  37.         printf("\n\nChoose operation:\n1. enque\n2. deque\n3. display\n0. Exit ");
  38.         scanf("%d", &ch);
  39.     } while(ch != 0);
  40.          
  41.     return 0;
  42. }
  43.  
  44. void enque() {
  45.     int x;
  46.     printf("\nEnter element to add: ");
  47.     scanf("%d", &x);
  48.     if(Full()) {
  49.         printf("\nQueue is full!!");
  50.     } else {
  51.         if(Empty()) {
  52.             q->front = q->rear = 0;
  53.         } else {
  54.             q->rear = (q->rear + 1) % MAX;
  55.         }
  56.         q->que[q->rear] = x;
  57.     }
  58.     display();
  59. }
  60.  
  61. void deque() {
  62.     if(Empty()) {
  63.         printf("\nQueue is empty!");
  64.     } else {
  65.         printf("\nDeleted element: %d", q->que[q->front]);
  66.         if(q->front == q->rear) {
  67.             q->front = q->rear = -1;
  68.         } else {
  69.             q->front = (q->front + 1) % MAX;
  70.         }
  71.     }
  72.     display();
  73. }
  74.  
  75. int Empty() {
  76.     if(q->rear == -1) {
  77.         return 1;
  78.     } else {
  79.         return 0;
  80.     }
  81. }
  82.  
  83. int Full() {
  84.     if((q->rear + 1) % MAX == q->front) {
  85.         return 1;
  86.     } else {
  87.         return 0;
  88.     }
  89. }
  90.  
  91. void display() {
  92.     int i;
  93.     if(Empty()) {
  94.         printf("\nQueue is empty!");
  95.     } else {
  96.         printf("\nQueue elements: ");
  97.         for(i = q->front; i != q->rear; i = (i + 1) % MAX) {
  98.             printf("%d < ", q->que[i]);
  99.         }
  100.         printf("%d", q->que[i]);
  101.     }
  102. }
  103. ----------------------------------------------------------------------------------------------------------------------------------------------
  104. Linked list (ALL)
  105. #include<stdio.h>
  106. #include<conio.h>
  107. #include<stdlib.h>
  108.  
  109. struct node
  110. {
  111.     int data;
  112.     struct node *next;
  113.    
  114. }*head =NULL;
  115.  
  116. struct node* ptr;
  117.  
  118.  
  119. void beginsert()
  120. {
  121. struct node *ptr;
  122. int item;
  123. ptr = (struct node *) malloc(sizeof(struct node *));
  124. if(ptr == NULL)
  125. {
  126. printf("\nOVERFLOW");
  127. }
  128. else
  129. {
  130. printf("\nEnter value\n");
  131. scanf("%d",&item);
  132. ptr->data = item;
  133. ptr->next = head;
  134. head = ptr;
  135. printf("\nNode inserted");
  136. }
  137. }
  138.  
  139.  
  140. void lastinsert()
  141. {
  142. struct node *ptr,*temp;
  143. int item;
  144. ptr = (struct node*)malloc(sizeof(struct node));
  145. if(ptr == NULL)
  146. {
  147. printf("\nOVERFLOW");
  148. }
  149. else
  150. {
  151. printf("\nEnter value?\n");
  152. scanf("%d",&item);
  153. ptr->data = item;
  154. if(head == NULL)
  155. {
  156. ptr -> next = NULL;
  157. head = ptr;
  158. printf("\nNode inserted");
  159. }
  160. else
  161. {
  162. temp = head;
  163. while (temp -> next != NULL)
  164. {
  165. temp = temp -> next;
  166. }
  167. temp->next = ptr;
  168. ptr->next = NULL;
  169. printf("\nNode inserted");
  170. }
  171. }
  172. }
  173.  
  174.  
  175. //Insert Node at inbetween of LL
  176. void randominsert()
  177. {
  178. int i,loc,item;
  179. struct node *ptr, *temp;
  180. ptr = (struct node *) malloc (sizeof(struct node));
  181. if(ptr == NULL)
  182. {
  183. printf("\nOVERFLOW");
  184. }
  185. else
  186. {
  187. printf("\nEnter element value");
  188. scanf("%d",&item);
  189. ptr->data = item;
  190. printf("\nEnter the location after which you want to insert");
  191. scanf("\n%d",&loc);
  192. temp=head;
  193. for(i=1;i<loc;i++)
  194. {
  195. temp = temp->next;
  196. if(temp == NULL)
  197. {
  198. printf("\ncan't insert\n");
  199. return;
  200. }
  201. }
  202. ptr ->next = temp ->next;
  203. temp ->next = ptr;
  204. printf("\nNode inserted");
  205. }
  206. }
  207.  
  208.  
  209. void begin_delete()
  210. {
  211. struct node *ptr;
  212. if(head == NULL)
  213. {
  214. printf("\nList is empty\n");
  215. }
  216. else
  217. {
  218. ptr = head;
  219. head = ptr->next;
  220. free(ptr);
  221. printf("\nNode deleted from the begining ...\n");
  222. }
  223. }
  224.  
  225.  
  226. //Delete Node at Last of LL
  227. void last_delete()
  228. {
  229. struct node *ptr,*ptr1;
  230. if(head == NULL)
  231. {
  232. printf("\nlist is empty");
  233. }
  234. else if(head -> next == NULL)
  235. {
  236. head = NULL;
  237. free(head);
  238. printf("\nOnly node of the list deleted...\n");
  239. }
  240. else
  241. {
  242. ptr = head;
  243. while(ptr->next != NULL)
  244. {
  245. ptr1 = ptr;
  246. ptr = ptr ->next;
  247. }
  248. ptr1->next = NULL;
  249. free(ptr);
  250. printf("\nDeleted Node from thelast ...\n");
  251. }
  252. }
  253.  
  254.  
  255.  
  256. //Delete Node at Mid of LL
  257. void random_delete()
  258. {
  259. struct node *ptr,*ptr1;
  260. int loc,i;
  261. printf("\n Enter the location of the node after which you want to perform deletion \n");
  262. scanf("%d",&loc);
  263. ptr=head;
  264.  
  265. for(i=1;i<=loc;i++)
  266. {
  267. ptr1 = ptr;
  268. ptr = ptr->next;
  269. if(ptr == NULL)
  270. {
  271. printf("\nCan't delete");
  272. return;
  273. }
  274. }
  275.  
  276. ptr1 ->next = ptr ->next;
  277. free(ptr);
  278. printf("\nDeleted node %d ",loc);
  279. }
  280.  
  281.  
  282. void display()
  283. {
  284. struct node *ptr;
  285. ptr = head;
  286. if(ptr == NULL)
  287. {
  288. printf("Nothing to print");
  289. }
  290. else
  291. {
  292. printf("\nprinting values . . . . .\n");
  293. while (ptr!=NULL)
  294. {
  295. printf(" %d --> ",ptr->data);
  296. ptr = ptr -> next;
  297. }
  298. if (ptr==NULL)
  299. {printf("NULL");
  300.     /* code */
  301. }
  302. }}
  303.  
  304.  
  305. void search()
  306. {
  307. struct node *ptr;
  308. int item,i=0,flag;
  309. ptr = head;
  310. if(ptr == NULL) {
  311. printf("\nEmpty List\n"); }
  312. else {
  313. printf("\nEnter item which you want to search?\n");
  314. scanf("%d",&item);
  315. while (ptr!=NULL) {
  316. if(ptr->data == item) {
  317. printf("item found at location %d ",i+1);
  318. flag=0;
  319. }
  320. else
  321. {
  322. flag=1;
  323. }
  324. i++;
  325. ptr = ptr -> next;
  326. }
  327. if(flag==1)
  328. {
  329. printf("Item not found\n");
  330. }
  331. }
  332. }
  333.  
  334. void main(){
  335.    
  336.    
  337.     int c=0;
  338.     printf("\n=========================================================================================");
  339.     printf("\nEnter your choice : \n1.insert at beginning \n2.insert at end \n3.delete from beginning \n4.Delete from end \n5.Delete from mid \n6.Display \n7.Search \n0. exit");
  340.     scanf("%d",&c);
  341.     printf("\n=========================================================================================");
  342.  
  343.     while(c!=0){
  344.         switch(c){
  345.  
  346.             case 1 : beginsert();break;
  347.             case 2 : lastinsert();break;
  348.             case 3 : begin_delete();break;
  349.             case 4 : last_delete();break;
  350.             case 5 : random_delete();break;
  351.             case 6 : display();break;
  352.             case 7 : search();break;
  353.             case 0 : exit(0);
  354.             default : exit(0);
  355.        }
  356.        printf("\n=========================================================================================");
  357.         printf("\nEnter your choice : \n1.insert at beginning \n2.insert at end \n3.delete from beginning \n4.Delete from end \n5.Delete from mid \n6.Display \n7.Search \n0. exit");
  358.         scanf("%d",&c);
  359.         printf("\n=========================================================================================\n");
  360.  
  361.     }
  362.  
  363.    
  364.  
  365.    
  366. }
  367. ----------------------------------------------------------------------------------------------------------------------------------------------
  368. Linked stack
  369. #include<stdio.h>
  370. #include<stdlib.h>
  371.  
  372. struct node{
  373.     int data;
  374.     struct node *next;
  375. };
  376. struct node* top;
  377.  
  378.  
  379. int isStackEmpty(){
  380.     if(top==NULL){ return 1;}
  381.     else  { return 0;
  382.     }}
  383. void pop(){
  384.     if(!isStackEmpty()){
  385.         int item;
  386.         item=top->data;
  387.         printf("Deleted item :%d",item);
  388.         top=top->next;
  389.     }
  390.     else{
  391.         printf("Stack underflow");
  392.         }
  393.  
  394. }
  395.  
  396. void push(int element){
  397.     struct node *temp;
  398.     temp=(struct node*)malloc(sizeof(struct node));
  399.     temp->data=element;
  400.     temp->next=NULL;
  401.  
  402.  
  403.     if(top==NULL){
  404.         top=temp;    
  405.     }
  406.     else{
  407.         temp->next=top;
  408.         top=temp;
  409.         printf("ELement inserted !!");
  410.  
  411.        
  412.     }
  413.  
  414. }
  415.  
  416. void display(){
  417.     if(!isStackEmpty()){printf("%d",top->data);}
  418.     else{
  419.         printf("Empty stack");
  420.     }
  421. }
  422.  
  423.  
  424.  
  425.  
  426.  
  427. void main(){
  428.      int n=1;
  429.     int element;
  430.     top=NULL;
  431.  
  432.     printf("Enter choice \n1.Push 2.Pop 3.empty or not 4.display \n0 exit\n");
  433.     scanf("%d",&n);
  434.  
  435.     while(n!=0){
  436.         switch(n){
  437.             case 1 : printf("enter element :\n");scanf("%d",&element); push(element);break;
  438.             case 2 : pop();break;
  439.             case 3 : if(isStackEmpty()) printf("\nStack empty");
  440.                      else printf("Not empty");
  441.                      break;
  442.             case 4 : display();break;
  443.             case 0 : exit(0);break;
  444.             default : exit(0);
  445.         }
  446.         printf("\n=================================>\nEnter choice \n1.Push 2.Pop 3.empty or not 4.displaytop \n0 exit\n=================================>\n");
  447.     scanf("%d",&n);
  448.  
  449.  
  450.     }
  451. }
  452. ----------------------------------------------------------------------------------------------------------------------------------------------
  453. Priority Queue
  454. //PriorityQueue
  455. //Lower the value higher the priority
  456. #include<stdio.h>
  457. #include<stdlib.h>
  458.  
  459. struct Queue{
  460.     int data;
  461.     int pty;
  462.     struct Queue *next;
  463. };
  464.  
  465. struct Queue *head=NULL;
  466. void display();
  467. void insert();
  468.  
  469. void delete();
  470.  
  471. void main(){
  472.     printf("\nLinked implementation of Priority queue");
  473.     printf("\nEnter your choice : \n1. Insert 2. Delete 3. Display 0. Exit");
  474.     int ch=0;
  475.     scanf("%d",&ch);
  476.     while(ch!=0){
  477.         switch(ch){
  478.             case 1 : insert();break;
  479.             case 2 : delete();break;
  480.             case 3 : display();break;
  481.             case 0 : exit(0);
  482.             default : exit(0);
  483.         }
  484.         printf("\n\nEnter your choice : \n1. Insert 2. Delete 3. Display 0. Exit");
  485.          scanf("%d",&ch);
  486.  
  487.     }
  488.  
  489.  
  490. }
  491. void display(){
  492.     struct Queue *temp;
  493.     temp=head;
  494.     if(temp==NULL){
  495.      printf("\nQueue is empty");
  496.  
  497.     }
  498.     else {
  499.         while(temp!=NULL){
  500.             printf("%d < ",temp->data);
  501.             temp=temp->next;
  502.         }
  503.     }
  504.  
  505. }
  506. void insert(){
  507.     struct Queue *temp=(struct Queue*)malloc(sizeof(struct Queue));
  508.     printf("\nEnter element : ");
  509.     int element;
  510.     scanf("%d",&element);
  511.     printf("\nEnter element with priority : ");  
  512.     int p;
  513.     scanf("%d",&p);
  514.     temp->data=element;
  515.     temp->pty=p;
  516.     temp->next = NULL;
  517.     if(head==NULL){
  518.         head=temp;
  519.  
  520.     }
  521.     else{
  522.         if(temp->pty < head->pty){
  523.             temp->next=head;
  524.             head=temp;
  525.         }
  526.         else{struct Queue *q=head;
  527.             while(q->next!=NULL && temp->pty >= (q->next)->pty ){
  528.                 q=q->next;
  529.  
  530.             }
  531.             temp->next=q->next;
  532.             q->next=temp;
  533.         }
  534.     }  
  535.     display();
  536. }
  537.  
  538.  
  539. void delete(){
  540.     struct Queue *temp=head;
  541.     if (temp == NULL){
  542.         printf("\nQueue is empty");
  543.         }
  544.     else{
  545.         printf("\nElement deleted : %d",head->data);
  546.         head=head->next;
  547.         free(temp);
  548.     }
  549.     display();
  550. }
  551. ----------------------------------------------------------------------------------------------------------------------------------------------
  552. Queue Array
  553. #include<stdio.h>
  554. #include<stdlib.h>
  555. #define MAX 30
  556.  
  557. int queue[MAX];
  558. int front;
  559. int rear;
  560.  
  561. front=-1;
  562. rear=-1;
  563. void display();
  564. void enque();
  565. void deque();
  566. int isEmpty();
  567. int isFull();
  568.  
  569. int main(){
  570.     printf("\nArray/Static implementation of Queue");
  571.     int ch=1;
  572.     printf("\n\nChoose operation : \n1. enque 2. deque 3. display 0. Exit ");
  573.         scanf("%d",&ch);
  574.     do{
  575.        
  576.        
  577.         switch(ch){
  578.             case 1 : enque(); break;
  579.             case 2 : deque(); break;
  580.             case 3 : display(); break;
  581.             case 0 : exit(0);
  582.             default : printf("\ninvalid input. \nclosing..........");
  583.                         exit(0);
  584.         }printf("\nfront: %d rear: %d",front,rear);
  585.         printf("\n\nChoose operation : \n1. enque 2. deque 3. display 0. Exit ");
  586.         scanf("%d",&ch);
  587.     }
  588.     while(!0); 
  589.  
  590. return 0;
  591. }
  592.  
  593. void enque(){
  594.     int x;
  595.     printf("\nEnter element to insert : ");
  596.     scanf("%d",&x);
  597.      if(isFull())
  598.      {
  599.         printf("\nQueue is full!!!");
  600.     }
  601.     else if(isEmpty())
  602.     {
  603.         rear=front=0;
  604.         queue[front]=x;
  605.         printf("\nSuccessfully inserted first element : %d",x);
  606.     }
  607.     else
  608.     {
  609.      queue[++rear]=x;
  610.      printf("\n%d inserted",x);  
  611.     }
  612.     display();
  613.     }
  614.  
  615. void deque(){
  616.     if(isEmpty()){
  617.        
  618.     }
  619.     else if(front==rear){
  620.        
  621.         printf("\nDeleted the only element in queue : %d",queue[front]);
  622.         front=rear=-1;
  623.     }
  624.     else{
  625.         printf("\nElement deleted: %d ",queue[front++]);
  626.        
  627.     }
  628.     display();
  629. }
  630.  
  631. void display(){
  632.     int i;
  633.     printf("\n");
  634.     if(!isEmpty()){
  635.     for(i=front;i<=rear;i++){
  636.         printf("%d < ", queue[i]);
  637.     }
  638.     }
  639.     else{
  640.         printf("Queue is empty");
  641.  
  642.     }
  643. }
  644.  
  645. int isEmpty(){
  646.     if(rear==-1||front==-1){
  647.         return 1;
  648.     }
  649.     else
  650.     {
  651.         return 0;
  652.     }
  653. }
  654.  
  655. int isFull(){
  656.     if(rear==MAX-1){
  657.         return 1;
  658.     }
  659.     else
  660.     {
  661.         return 0;
  662.     }
  663. }
  664. ----------------------------------------------------------------------------------------------------------------------------------------------
  665. Queue Linked list (Same with slight change)
  666. #include <stdio.h>
  667. #include <stdlib.h>
  668.  
  669. struct node
  670. {
  671.     int info;
  672.     struct node *ptr;
  673. }*front,*rear,*temp,*front1;
  674.  
  675. int frontelement();
  676. void enq(int data);
  677. void deq();
  678. void empty();
  679. void display();
  680. void create();
  681. void queuesize();
  682.  
  683. int count = 0;
  684.  
  685. void main()
  686. {
  687.     int no, ch, e;
  688.     printf("Linked implementaion of queue");
  689.    
  690.  
  691.     printf("\n 1 - Enque");
  692.     printf("\n 2 - Deque");
  693.     printf("\n 3 - Front element");
  694.     printf("\n 4 - Empty");
  695.     printf("\n 5 - Queue size");
  696.     printf("\n 6 - Display");
  697.     printf("\n 7 - Exit");
  698.     create();
  699.     while (1)
  700.     {
  701.         printf("\n Enter choice : ");
  702.         scanf("%d", &ch);
  703.         switch (ch)
  704.         {
  705.         case 1:
  706.             printf("Enter data : ");
  707.             scanf("%d", &no);
  708.             enq(no);
  709.             break;
  710.         case 2:
  711.             deq();
  712.             break;
  713.         case 3:
  714.             e = frontelement();
  715.             if (e != 0)
  716.                 printf("Front element : %d", e);
  717.             else
  718.                 printf("\n No front element in Queue as queue is empty");
  719.             break;
  720.         case 4:
  721.             empty();
  722.             break;
  723.         case 5:
  724.            queuesize();
  725.            break;
  726.         case 6:
  727.           display();
  728.             break;
  729.         case 7:
  730.              exit(0);
  731.             break;
  732.         default:
  733.             printf("Wrong choice, Please enter correct choice  ");
  734.             break;
  735.         }
  736.     }
  737. }
  738.  
  739. void create()
  740. {
  741.     front = rear = NULL;
  742. }
  743.  
  744.  
  745. void queuesize()
  746. {
  747.     printf("\n Queue size : %d", count);
  748. }
  749.  
  750.  
  751. void enq(int data)
  752. {
  753.     if (rear == NULL)
  754.     {
  755.         rear = (struct node *)malloc(1*sizeof(struct node));
  756.         rear->ptr = NULL;
  757.         rear->info = data;
  758.         front = rear;
  759.     }
  760.     else
  761.     {
  762.         temp=(struct node *)malloc(1*sizeof(struct node));
  763.         rear->ptr = temp;
  764.         temp->info = data;
  765.         temp->ptr = NULL;
  766.  
  767.         rear = temp;
  768.     }
  769.     count++;
  770. }
  771.  
  772. void display()
  773. {
  774.     front1 = front;
  775.  
  776.     if ((front1 == NULL) && (rear == NULL))
  777.     {
  778.         printf("Queue is empty");
  779.         return;
  780.     }
  781.     while (front1 != rear)
  782.     {
  783.         printf("%d ", front1->info);
  784.         front1 = front1->ptr;
  785.     }
  786.     if (front1 == rear)
  787.         printf("%d", front1->info);
  788. }
  789.  
  790. void deq()
  791. {
  792.     front1 = front;
  793.  
  794.     if (front1 == NULL)
  795.     {
  796.         printf("\n Error: Trying to display elements from empty queue");
  797.         return;
  798.     }
  799.     else
  800.         if (front1->ptr != NULL)
  801.         {
  802.             front1 = front1->ptr;
  803.             printf("\n Dequed value : %d", front->info);
  804.             free(front);
  805.             front = front1;
  806.         }
  807.         else
  808.         {
  809.             printf("\n Dequed value : %d", front->info);
  810.             free(front);
  811.             front = NULL;
  812.             rear = NULL;
  813.         }
  814.         count--;
  815. }
  816.  
  817. int frontelement()
  818. {
  819.     if ((front != NULL) && (rear != NULL))
  820.         return(front->info);
  821.     else
  822.         return 0;
  823. }
  824.  
  825. void empty()
  826. {
  827.      if ((front == NULL) && (rear == NULL))
  828.         printf("\n Queue empty");
  829.     else
  830.        printf("Queue not empty");
  831. }
  832. ---------------------------------------------------------------------------------------------------------------------------------------------
  833. Queue using Array structure
  834. #include <stdio.h>
  835. #include <stdlib.h>
  836. #define MAX 30
  837. struct queue{
  838.     int que[MAX];
  839.     int front;
  840.     int rear;
  841.  
  842. };
  843. struct queue items;
  844. struct queue *q=&items;
  845.  
  846.  
  847. void enque();
  848. void deque();
  849. void display();
  850. int Empty();
  851. int Full();
  852.  
  853. int main(int argc,char *argv[]){
  854.    
  855.  
  856.  
  857. q->front =-1;
  858. q->rear=-1;
  859.     printf("->%d\n%s<-",argc,argv[0]);
  860.      printf("\nArray/Static implementation of Queue");
  861.     int ch=1;
  862.     printf("\n\nChoose operation : \n1. enque 2. deque 3. display 0. Exit ");
  863.         scanf("%d",&ch);
  864.      while(ch!=0){
  865.        
  866.        
  867.         switch(ch){
  868.             case 1 : enque(); break;
  869.             case 2 : deque(); break;
  870.             case 3 : display(); break;
  871.             case 0 : exit(0);
  872.             default : printf("\ninvalid input. \nclosing..........");
  873.                         exit(0);
  874.         }printf("\nfront: %d rear: %d",q->front,q->rear);
  875.         printf("\n\nChoose operation : \n1. enque 2. deque 3. display 0. Exit ");
  876.         scanf("%d",&ch);
  877.     }
  878.    
  879.          
  880.     return 0;
  881. }
  882.  
  883.  
  884. void enque(){int x;
  885.     printf("\nEnter element to add : ");
  886.     scanf("%d",&x);
  887.     if(Full()){
  888.         printf("\nQueue is full!!");
  889.     }
  890.     else if (Empty()){
  891.         q->front=q->rear=0;
  892.         q->que[q->front]=x;
  893.     }
  894.     else
  895.     {
  896.         q->que[++q->rear]=x;
  897.     }
  898.     display();
  899. }
  900.  
  901. void deque(){
  902.     if(Empty()){
  903.         printf("\nQueue is empty!");
  904.  
  905.     }
  906.     else if (q->front==q->rear ){
  907.         printf("\nDeleted the last element of queue : %d",q->que[q->front]);
  908.         q->rear=q->front=-1;
  909.  
  910.     }
  911.     else{
  912.         printf("\nDeleted element : %d",q->que[q->front++]);
  913.  
  914.     }
  915.     display();
  916. }
  917.  
  918. int Empty(){
  919.     if(q->rear==-1){
  920.         return 1;
  921.  
  922.     }
  923.     else return 0;
  924. }
  925.  
  926. int Full(){
  927.     if (q->rear==MAX-1) return 1;
  928.     else return 0;
  929. }
  930.  
  931. void display(){
  932.  
  933.     int i;
  934.     if(Empty()) printf("\nQueue is Empty!!!!");
  935.     else
  936.     for(i=q->front;i<=q->rear;i++){
  937.         printf("%d < ",q->que[i]);
  938.     }
  939.  
  940. }
  941.  
  942.  
  943. ----------------------------------------------------------------------------------------------------------------------------------------------
  944. Queue using linked list
  945. #include<stdio.h>
  946. #include<stdlib.h>
  947.  
  948.  struct Queue{
  949.     int data;
  950.     struct Queue *next;
  951. };
  952.  
  953. int size=0;
  954.  
  955. struct Queue *front=NULL;
  956. struct Queue *rear=NULL;
  957.  
  958. void display(){
  959.      struct Queue *temp=front;
  960.      printf("\n");
  961.      if(temp==NULL) printf("Queue is empty");
  962.      else
  963.      while(temp!=NULL){
  964.         printf("%d < ",temp->data);
  965.         temp=temp->next;
  966.      }
  967.      printf("\nSize of Queue: %d",size);
  968.  
  969.  
  970. }
  971. void enque(){
  972.    
  973.     struct Queue *temp=(struct Queue*)malloc(sizeof(struct Queue));
  974.     printf("\nEnter data to enter : ");
  975.     scanf("%d",&temp->data);
  976.     temp->next=NULL;
  977.  
  978.     if(front==NULL){        
  979.         front=rear=temp;
  980.         size++;      
  981.     }
  982.     else{
  983.         rear->next=temp;
  984.         rear=temp;
  985.         size++;
  986.  
  987.     }
  988.     display();
  989.  
  990. }
  991.  
  992. void deque(){
  993.     if(front==NULL){
  994.         printf("\nQueue is empty!!");
  995.     }
  996.     else{
  997.         struct Queue *temp=front;
  998.         front=front->next;
  999.         printf("\nDeleted element : %d",temp->data);
  1000.  
  1001.         free(temp);
  1002.         size--;
  1003.     }
  1004.     display();
  1005.  
  1006. }
  1007.  
  1008.  
  1009.  
  1010. int main(){
  1011.     printf("\nLinked implementation of linear queue");
  1012.     printf("\nEnter your choice : \n1. Enque 2. Deque 3. Display 0. Exit");
  1013.     int ch;
  1014.     scanf("%d",&ch);
  1015.     while(ch!=0){
  1016.         switch(ch){
  1017.             case 1 : enque();break;
  1018.             case 2 : deque();break;
  1019.             case 3 : display();break;
  1020.             case 0 : exit(0);
  1021.             default : exit(0);
  1022.         }
  1023.         printf("\n\nEnter your choice : \n1. Enque 2. Deque 3. Display 0. Exit");
  1024.          scanf("%d",&ch);
  1025.  
  1026.     }
  1027.  
  1028.  
  1029. }
  1030. ----------------------------------------------------------------------------------------------------------------------------------------------
  1031. Queue
  1032. #include<stdio.h>
  1033. #include<stdlib.h>
  1034. #define MAX 20
  1035.  
  1036. //declaration
  1037. void enqueue();
  1038. void dequeue();
  1039. void display();
  1040. int queue[MAX];
  1041. int rear=-1;
  1042. int front=-1;
  1043.  
  1044. //insertion
  1045. void enqueue()
  1046. {
  1047.         int info,i;
  1048.         if(rear==MAX-1)
  1049.         printf("\nQUEUE OVERFLOW");
  1050.         else
  1051.         {
  1052.                 if(front==-1)
  1053.                 front=0;
  1054.                 printf("\nEnter the element to be inserted: ");
  1055.                 scanf("%d",&info);
  1056.                 rear=rear+1;
  1057.                 queue[rear]=info;
  1058.         }
  1059.         display();
  1060. }
  1061.  
  1062. //deleteion
  1063. void dequeue()
  1064. {
  1065.         if(front==-1)
  1066.         printf("\nQUEUE UNDERFLOW");
  1067.         else
  1068.         {
  1069.                 printf("\nThe deleted element is %d",queue[front]);
  1070.                 front=front+1;
  1071.         }
  1072.         display();
  1073. }
  1074.  
  1075. //display
  1076. void display()
  1077. {
  1078.         int i;
  1079.         if(front==-1)
  1080.         printf("\nQUEUE IS EMPTY");
  1081.         else
  1082.         {
  1083.                 printf("\nThe Queue is:\n");
  1084.                 for(i=front;i<=rear;i++)
  1085.                 {
  1086.                         printf(">> %d <<",queue[i]);
  1087.                         printf("\n");
  1088.                 }
  1089.         }
  1090. }
  1091.  
  1092. //driver code
  1093. void main(){
  1094.         int choice;
  1095.     while (1)
  1096.     {
  1097.         printf("\n----------SEQUENTIAL IMPLEMENTATION OF QUEUE----------\n");
  1098.         printf("1.INSERT element to queue \n");
  1099.         printf("2.DELETE element from queue \n");
  1100.         printf("3.DISPLAY all elements of queue \n");
  1101.         printf("4.EXIT \n");
  1102.         printf("\nEnter your choice : ");
  1103.         scanf("%d", &choice);
  1104.         switch (choice)
  1105.         {
  1106.             case 1:
  1107.             enqueue();
  1108.             break;
  1109.             case 2:
  1110.             dequeue();
  1111.             break;
  1112.             case 3:
  1113.             display();
  1114.             break;
  1115.             case 4:
  1116.             printf("\n-----EXIT POINT-----");
  1117.             exit(0);
  1118.             default:
  1119.             printf("Wrong choice \n");
  1120.         }
  1121.     }
  1122. }
  1123. ----------------------------------------------------------------------------------------------------------------------------------------------
  1124. Stack
  1125. #include <stdio.h>
  1126. #include<stdlib.h>
  1127. int stack[50];
  1128. int maxcapacity=50;
  1129. int top=-1;
  1130.  
  1131. int empty(){
  1132.     if(top == -1) return 1;
  1133.     else return 0;
  1134.  
  1135.  
  1136. }
  1137. int isfull(){
  1138.     if(top == maxcapacity-1) return 1;
  1139.     else return 0;
  1140.  
  1141. }
  1142. void pop(){
  1143.     if(!empty()) printf("\nElement deleted : %d",stack[top--]);
  1144.     else printf("\nStack underflow");
  1145.  
  1146. }
  1147. void push(int element){
  1148.     if(!isfull()){
  1149.         stack[++top]=element;}
  1150.     else {
  1151.         printf("\nstack overflow");}
  1152.  
  1153.  
  1154. }
  1155.  
  1156.  
  1157. void display(){int i;printf("\ntop = %d\n",stack[top]);
  1158.     if(!empty()){
  1159.         for(i=top;i>=0;i--){ printf("%d\n",stack[i]);
  1160.     printf("^ \n");}}
  1161.     else{
  1162.         printf("\nstack underflow");}
  1163.  
  1164.  
  1165. }
  1166.  
  1167.  
  1168. void main(){
  1169.     int n=1;
  1170.     int element;
  1171.  
  1172.     printf("=================================>\nEnter choice \n1.Push 2.Pop 3.empty or not 4.display \n0 exit \n-->");
  1173.  
  1174.     scanf("%d",&n);
  1175.     printf("\n=================================>");
  1176.  
  1177.     while(n!=0){
  1178.         switch(n){
  1179.             case 1 : printf("\nenter element :");scanf("%d",&element); push(element);break;
  1180.             case 2 : pop();break;
  1181.             case 3 : empty()?printf("\nempty"):printf("\nnot empty");break;
  1182.             case 4 : display();break;
  1183.             case 0 : exit(0);break;
  1184.             default : exit(0);
  1185.         }
  1186.         printf("\n=================================>\nEnter choice \n1.Push 2.Pop 3.empty or not 4.displaytop \n0 exit \n--> ");
  1187.     scanf("%d",&n);
  1188.     printf("\n=================================>");
  1189.  
  1190.  
  1191.  
  1192.     }
  1193.  
  1194.  
  1195. }

Raw Paste

Login or Register to edit or fork this paste. It's free.