0% found this document useful (0 votes)
7 views

Queue

The document contains code for implementing a linked list data structure in C. It includes functions for creating, inserting, deleting, traversing and performing other operations on nodes of the linked list.

Uploaded by

arjunkp1678
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Queue

The document contains code for implementing a linked list data structure in C. It includes functions for creating, inserting, deleting, traversing and performing other operations on nodes of the linked list.

Uploaded by

arjunkp1678
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

#include<stdio.

h>

#include<stdlib.h>

struct Node

int data;

struct Node *link;

};

struct Node *head=NULL;

void create()

struct Node *New,*temp;

temp=head;

New=(struct Node *)malloc(sizeof(struct Node));

if(New==NULL) printf("\nOver Flow");

else

printf("\nEnter Data:");

scanf("%d",&New->data);

New->link=NULL;

if(head==NULL)

head=New;

else

while(temp->link!=NULL)

temp=temp->link;

temp->link=New;

}
void display()

struct Node *temp;

temp=head;

if(temp==NULL) printf("\nNothing to print...");

else

while(temp!=NULL)

printf("%d->",temp->data);

temp=temp->link;

printf("NULL");

void reverseList()

struct Node *temp,*prev,*next;

temp=head;

prev=NULL;

next=NULL;

while(temp!=NULL)

next=temp->link;

temp->link=prev;

prev=temp;

temp=next;

head=prev;
}

void count()

struct Node *temp;

int count=0;

temp=head;

if(temp==NULL) printf("\nNumber of Nodes = %d",count);

else

while(temp!=NULL)

count++;

temp=temp->link;

printf("\nNumber of Nodes = %d",count);

void search()

struct Node *temp;

int key,flag=0;

temp=head;

if(temp==NULL) printf("\nNothing to print...");

else

printf("\nEnter Element to Search:");

scanf("%d",&key);

while(temp!=NULL)

{
if(temp->data==key)

flag=1;

break;

temp=temp->link;

if(flag==1)

printf("\nElement Found...");

else

printf("\nElement Not Found...");

void insert_at_first()

struct Node *temp;

temp=(struct Node *)malloc(sizeof(struct Node));

if(temp==NULL) printf("\nOver Flow");

else

printf("\nEnter Data:");

scanf("%d",&temp->data);

temp->link=head;

head=temp;

void insert_at_end()

struct Node *temp,*New;


temp=head;

New=(struct Node *)malloc(sizeof(struct Node));

if(New==NULL) printf("\nOver Flow");

else

while(temp->link!=NULL)

temp=temp->link;

printf("\nEnter Data:");

scanf("%d",&New->data);

New->link=NULL;

temp->link=New;

void insert_pos()

struct Node *temp,*New;

int i=1,pos;

printf("\nEnter Position to Insert Node:");

scanf("%d",&pos);

temp=head;

New=(struct Node *)malloc(sizeof(struct Node));

if(New==NULL) printf("\nOver Flow");

else

while((temp!=NULL) && (i<pos))

temp=temp->link;

i++;

printf("\nEnter Data:");
scanf("%d",&New->data);

New->link=temp->link;

temp->link=New;

void del_at_first()

struct Node *temp;

temp=head;

head=head->link;

free(temp);

void del_at_end()

struct Node *temp,*prev;

temp=head;

prev=head;

temp=temp->link;

while(temp->link!=NULL)

temp=temp->link;

prev=prev->link;

prev->link=NULL;

free(temp);

void del_pos()

{
struct Node *temp,*prev;

int i=1,pos;

printf("\nEnter Position:");

scanf("%d",&pos);

temp=head;

prev=head;

temp=temp->link;

while((temp->link!=NULL) && (i<pos))

temp=temp->link;

prev=prev->link;

i++;

prev->link=temp->link;

free(temp);

void sort()

int i,j,count=0;

struct Node *temp,*p1,*p2;

temp=head;

while(temp!=NULL)

temp=temp->link;

count++;

for(i=0;i<count;i++)

temp=head;

for(j=0;j<count-1-i;j++)
{

p1=temp;

p2=temp->link;

if(p1->data > p2->data)

int t=p1->data;

p1->data=p2->data;

p2->data=t;

temp=p2;

void sum()

int sum=0;

struct Node *temp;

temp=head;

while(temp!=NULL)

sum=sum+temp->data;

temp=temp->link;

printf("\nSum of Elements in List = %d",sum);

void alternate()

struct Node *temp;

temp=head;
while(temp!=NULL)

printf("%d ",temp->data);

if(temp->link!=NULL)

temp=temp->link->link;

else

temp=temp->link;

int main()

int ch,i,n;

do

printf("\n**");

printf("\nLinked List Operations");

printf("\n**");

printf("\n\n1. Create");

printf("\n2. Insert At Begin");

printf("\n3. Insert At End");

printf("\n4. Insert At Any Position");

printf("\n5. Delete At Begin");

printf("\n6. Delete At End");

printf("\n7. Delete At Any Position");

printf("\n8. Display");

printf("\n9. Number of Nodes");

printf("\n10. Search");

printf("\n11. ReverseList");

printf("\n12. Sorting");

printf("\n13. Sum of Elements");


printf("\n14. Print Alternative Nodes");

printf("\n0. Exit");

printf("\nEnter Ur Choice : ");

scanf("%d",&ch);

switch(ch)

case 1:

printf("\nEnter How Many Nodes:");

scanf("%d",&n);

for(i=0;i<n;i++)

create();

break;

case 2:

insert_at_first();

break;

case 3:

insert_at_end();

break;

case 4:

insert_pos();

break;

case 5:

del_at_first();

break;

case 6:

del_at_end();

break;

case 7:

del_pos();

break;
case 8:

display();

break;

case 0:

exit(0);

case 9:

count();

break;

case 10:

search();

break;

case 11:

reverseList();

break;

case 12:sort();

break;

case 13:

sum();

break;

case 14:

alternate();

break;

default:

printf("\nInvalid Option...");

break;

}while(ch!=0);

You might also like