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

C Program for Linked List Operations

The document contains a C program that implements a singly linked list with functionalities to create, display, and reverse the list. It defines a 'node' structure and includes functions for each operation. The main function provides a menu for user interaction to perform these operations until the user chooses to exit.

Uploaded by

chitrashirsat685
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)
21 views2 pages

C Program for Linked List Operations

The document contains a C program that implements a singly linked list with functionalities to create, display, and reverse the list. It defines a 'node' structure and includes functions for each operation. The main function provides a menu for user interaction to perform these operations until the user chooses to exit.

Uploaded by

chitrashirsat685
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

#include<stdio.

h>
#include<stdlib.h>

typedef struct node


{
int info;
struct node *next;
}node;

void createlist(node *head)


{
int n,cnt;
node *last,*newnode;
printf("\n How many nodes? ");
scanf("%d",&n);
last=head;
for(cnt=1;cnt<=n;cnt++)
{
newnode=(node*)malloc(sizeof(node));
newnode->next=NULL;
printf("\n enter the element");
scanf("%d",&newnode->info);
last->next=newnode;
last=newnode;
}
}

void display(node *head)


{
node *temp;
for(temp=head->next;temp!=NULL;temp=temp->next)
{
printf("%d\t",temp->info);
}
}

void reverseList(node *head)


{
node *t1=head->next;
node *t2,*t3;
if (t1==NULL)
return;
t2=t1->next;
if(t2==NULL)
return;
t3=t2->next;
t1->next=NULL;
while(t3!=NULL)
{t2->next=t1;
t1=t2;
t2=t3;
t3=t3->next;
}
t2->next=t1;
head->next=t2;
printf("\nReversed list is : ");
display(head);
}

main()
{
node *head;
int ch,n,pos;
head=(node *)malloc(sizeof(node));
head->next=NULL;
do
{
printf("\n\n1:create\n2:display\n3:reverse 4:exit\n");
printf("\nenter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
createlist(head);
break;
case 2:
display(head);
break;
case 3:
reverseList(head);
break;
}
}while(ch!=4);
}

You might also like