0% found this document useful (0 votes)
12 views2 pages

Prathmesh

The document contains C code for implementing a queue and a linked list. It includes functions for enqueueing, dequeueing, displaying elements, and managing linked list operations such as creating, searching, appending, inserting, deleting, and sorting. The code is structured with a menu-driven interface for user interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Prathmesh

The document contains C code for implementing a queue and a linked list. It includes functions for enqueueing, dequeueing, displaying elements, and managing linked list operations such as creating, searching, appending, inserting, deleting, and sorting. The code is structured with a menu-driven interface for user interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 2

• #include <stdio.h>#include <stdlib.

h>#define printf("%d\n", queue[i]); i = (i + 1) %


MAX 100 // Define the maximum size of the MAX; } printf("%d\n",
queueint queue[MAX];int front = -1; // Initialize queue[i]); }}int main() { int choice, item;
front of the queueint rear = -1; // Initialize rear init_queue(); while (1) { printf("\
of the queuevoid init_queue() { front = rear = nMenu:\n"); printf("1. Enqueue\n");
-1;}void enqueue(int item) { if ((rear + 1) %
printf("2. Dequeue\n"); printf("3.
MAX == front) { printf("Queue overflow\
n"); } else { if (front == -1) front = Display\n"); printf("4. Exit\n");
0; rear = (rear + 1) % MAX; queue[rear] printf("Enter your choice: "); scanf("%d",
= item; printf("%d enqueued to queue\n", &choice); switch (choice) { case
item); }}int dequeue() { if (front == -1) { 1: printf("Enter the item to
printf("Queue underflow\n"); return -1; } enqueue: "); scanf("%d",
else { int item = queue[front]; if (front &item); enqueue(item);
== rear) { front = rear = -1; } else break; case 2:
{ front = (front + 1) % MAX; } printf("Dequeued item: %d\n",
return item; }} dequeue()); break; case
• void display() { if (front == -1) { 3: display(); break;
printf("Queue is empty\n"); } else { case 4: exit(0);
printf("Queue elements are:\n"); int i = default: printf("Invalid choice!
front; while (i != rear) { Please try again.\n"); } } return 0;}
• #include<stdio.h>#include<stdlib.h>struct NODE{int data; struct NODE *next;};typedef struct NODE node;node
*list=NULL,*last;node * getnode(){
• node * temp;temp=(node *)malloc(sizeof(node)); printf("Enter the data");scanf("%d",&temp->data); temp->next=NULL; return
temp;}void create(){int i,n; node *temp;printf("Enter no of nodes:"); scanf("%d",&n);for(i=0;i<n;i++) {
temp=getnode(); if(list==NULL){list=temp;}else last->next=temp; } last=temp; }}void display(){node *ptr;
for(ptr=list;ptr!=NULL;ptr=ptr->next) { printf("%d\t",ptr->data); }}int linear_search(n,key){ node *ptr;
for(ptr=list;ptr!=NULL;ptr=ptr->next) { if(key==ptr->data) { printf("Key element
is found"); break; } } if(ptr==NULL) { printf("element not found"); }}void
append(){ node*temp; printf("Node to append:"); temp=getnode(); last->next=temp; last=temp;}void
insert_first(){ node*temp; printf("Node to append:"); temp=getnode(); temp->next=list; list=temp;}node
*delete_first(){ node *ptr; ptr=list; list=ptr->next; free(ptr); printf("\n Node is
Deleted."); return list; }node *delete_last(){ node *ptr, *prev; for(ptr=list,prev=list;ptr->next!
=NULL;prev=ptr,ptr=ptr->next); free(ptr); prev->next=NULL; return list;}void bubble_sort(){ node *p,
*q; if (list == NULL || list->next == NULL) { printf("List is empty or has only one node, sorting not required.\n");
return; } for (p = list; p->next != NULL; ) { int swapped = 0; for (q = p->next; q != NULL; q = q->next) { if (p->data >
q->data) { int temp = p->data; p->data = q->data; q->data = temp; swapped = 1; } }
if (!swapped) { break; } p = p->next; }}void main(){ int ch,key,n; while(1) {
printf("\n 1.Create a Linked List:"); printf("\n 2.Display a Linked List:"); printf("\n 3.Search a
number in Linked List:"); printf("\n 4.Create a append node:"); printf("\n 5.Create a node at first:");
printf("\n 6.Delete first node:"); printf("\n 7.Delete last node:"); printf("\n 8.Bubble
Sort"); printf("\n 9.Exit"); printf("\nEnter a Choice:"); scanf("%d",&ch);
switch(ch) { case 1 : create(); break; case 2 : display();
break; case 3 : printf("enter key element:"); scanf("%d",&key);
linear_search(n,key); break; case 4 : append(); display();
break; case 5 : insert_first(); display(); break;
case 6 : delete_first(); display(); break; case 7 :
delete_last(); display(); break; case 8 : bubble_sort ();
display(); break; case 9 : exit(0); } }}

You might also like