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

Singly Linked List

This C program implements a singly linked list. It defines functions to insert nodes at the front and rear of the list, delete nodes from the front and rear, and display the list. The main function contains a menu loop that allows the user to choose these operations and pass a data item to insert or display the current list. Node structures contain a data field and pointer to the next node. Memory is dynamically allocated for each new node.

Uploaded by

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

Singly Linked List

This C program implements a singly linked list. It defines functions to insert nodes at the front and rear of the list, delete nodes from the front and rear, and display the list. The main function contains a menu loop that allows the user to choose these operations and pass a data item to insert or display the current list. Node structures contain a data field and pointer to the next node. Memory is dynamically allocated for each new node.

Uploaded by

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

/*c Program to implement A SINGLY LINK LIST*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
typedef struct node *NODE;

NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(NODE));
if(x==NULL)
printf("Insufficient Memory\n");
return x;
}

void freenode(NODE x)
{
free(x);
}

NODE insert_front(NODE first,int item)


{
NODE temp;
temp=getnode();
temp->info=item;
if(first==NULL)
temp->link=NULL;
return temp;
else
temp->link=first;
return temp;
}
NODE insert_rear(NODE first,int item)
{
NODE temp,cur;
temp=getnode();
temp->link=NULL;
temp->info=item;
if(first==NULL)
return temp;
cur=first;
while(cur->link!=NULL)
{
cur=cur->link;
}
cur->link=temp;
return first;
}

NODE delete_front(NODE first)


{
NODE temp;
if(first==NULL)
{
printf("List is empty...!!\n");
return first;
}
temp=first;
temp=temp->link;
printf("Popped item=%d\n",first->info);
freenode(first);
return temp;
}
NODE delete_rear(NODE first)
{
NODE cur,prev;
if(first==NULL)
{
printf("List is empty...!!\n");
return first;
}
if(first->link==NULL)
{
printf("Deleted item=%d\n",first->info);
freenode(first);
return NULL;
}
prev=NULL;
cur=first;
while(cur->link!=NULL)
{
prev=cur;
cur=cur->link;
}
printf("Deleted item=%d\n",cur->info);
freenode(cur);
prev->link=NULL;
return first;
}

void display(NODE first)


{
NODE temp;
if(first==NULL)
{
printf("Nothing to display!!!\n");
return;
}
temp=first;
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->link;
}
printf("\t");
}

void main()
{
NODE first=NULL;
int choice,item;
for(;;)
{
printf("\n1:Insert from front\n");
printf("\n2:Insert from rear\n");
printf("\n3:Delete from front\n");
printf("\n4:Delete from rear\n");
printf("\n5:DISPLAY\n 6:EXIT\n");
printf("Enter your choice\n");
scanf("%d",&choice);

switch(choice)
{
case 1: printf("Enter an item to add\n");
scanf("%d",&item);
first=insert_front(first,item);
break;
case 2: printf("Enter an item to add\n");
scanf("%d",&item);
first=insert_rear(first,item);
break;
case 3: first=delete_front(first);
break;
case 4: first=delete_rear(first);
break;
case 5: display(first);
break;
default : exit(0);
getch();
}
}
}

You might also like