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

Linked List Programs

this is about linked lists program in c

Uploaded by

sbquantum2005
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)
5 views

Linked List Programs

this is about linked lists program in c

Uploaded by

sbquantum2005
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/ 10

Program 7: Design and Develop a program to demonstrate working of singly linked list

a) Create a linked list.


b) Insertion and deletion of a node at first position and at end position.
c) Display the contents of the linked 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(struct node));
if(x==NULL)
{
printf("out of memory\n");
exit(0);
}
return x;
}

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

NODE insert_front(int item, NODE first)


{
NODE temp;
temp=getnode();
temp->info=item;
temp->link=first;
return temp;
}

NODE delete_front(NODE first)


{
NODE temp;
if(first==NULL)
{
printf("Link is empty\n");
return first;
}
temp=first;
first=first->link;
printf("The deleted item is %d \n",temp->info);
free(temp);
return first;
}

NODE insert_rear(int item,NODE first)


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

NODE delete_rear(NODE first)


{
NODE cur=first, prev=NULL;
if(cur==NULL)
{
printf("Link is empty\n");
return first;
}
while(cur->link!=NULL)
{
prev=cur;
cur=cur->link;
}
free(cur);
prev->link=NULL;
return first;
}

void display(NODE first)


{ NODE temp=first;
if(first==NULL)
{
printf("NO NODES IN THE LIST!!\n");
return first;
}
else
{
while(temp!=NULL)
{
printf("%d ",temp->info);
temp=temp->link;
}
}
}
void main()
{
NODE first=NULL;
int choice, item;
for(;;)
{
printf("\n1.INSERT FRONT\n2.INSERT REAR\n3.DELETE FRONT\n4.DELETE REAR\
n5.DISPLAY\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Enter the item\n");
scanf("%d",&item);
first=insert_front(item,first);
break;
case 2:printf("Enter the item\n");
scanf("%d",&item);
first=insert_rear(item,first);
break;
case 3:first=delete_front(first);
break;
case 4:first=delete_rear(first);
break;
case 5:display(first);
break;
default:exit(0);
}
}
}

Doubly linked list

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *llink;
struct node *rlink;

};
typedef struct node* NODE;

NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
printf("out of memory\n");
exit(0);
}
return x;
}

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

NODE insert_front(int item, NODE first)


{
NODE temp;
temp=getnode();
temp->info=item;
temp->rlink=NULL;
temp->llink=NULL;
if(first==NULL)
{
first=temp;
return;
}
temp->rlink=first;
first->llink=temp;
return temp;
}

NODE delete_front(NODE first)


{
NODE temp;
if(first==NULL)
{
printf("Link is empty\n");
return first;
}
temp=first;
first=first->rlink;
printf("The deleted item is %d \n",temp->info);
free(temp);
return first;
}

NODE insert_rear(int item,NODE first)


{
NODE temp,cur;
temp=getnode();
temp->info=item;
temp->llink=temp->rlink=NULL;
if(first==NULL)
return temp;
cur=first;
while(cur->rlink!=NULL)
{
cur=cur->rlink;
}
cur->rlink=temp;
temp->llink=cur;
cur=temp;
return first;
}

NODE delete_rear(NODE first)


{
NODE cur=first, prev=NULL;
if(cur==NULL)
{
printf("Link is empty\n");
return first;
}
while(cur->rlink!=NULL)
{
prev=cur;
cur=cur->rlink;
}
printf(" deleted ele %d", cur->info);
free(cur);
prev->rlink=NULL;
return first;
}
void display(NODE first)
{ NODE temp=first;
if(first==NULL)
{
printf("NO NODES IN THE LIST!!\n");
return first;
}
else
{
while(temp!=NULL)
{
printf("%d ",temp->info);
temp=temp->rlink;
}
}
}
void main()
{
NODE first=NULL;
int choice, item;
for(;;)
{
printf("\n1.INSERT FRONT\n2.INSERT REAR\n3.DELETE FRONT\n4.DELETE
REAR\n5.DISPLAY\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Enter the item\n");
scanf("%d",&item);
first=insert_front(item,first);
break;
case 2:printf("Enter the item\n");
scanf("%d",&item);
first=insert_rear(item,first);
break;
case 3:first=delete_front(first);
break;
case 4:first=delete_rear(first);
break;
case 5:display(first);
break;
default:exit(0);
}
}
}
Circular Linked 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(struct node));
if(x==NULL)
{
printf("out of memory\n");
exit(0);
}
return x;
}

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

NODE insert_front(int item, NODE last)


{
NODE temp;
temp=getnode();
if (last == NULL)
{
temp->info = item;
temp->link = temp;
last = temp;
}
else
{
temp->info = item;
temp->link = last->link;
last->link = temp;
}
}

NODE delete_front(NODE last)


{
NODE temp;

if (last == NULL)
{
printf("\nList is empty.\n");
return last;
}
else
{
temp = last->link;
last->link = temp->link;
free(temp);
return last;

}
}

NODE insert_rear(int item,NODE last)


{
NODE temp,cur;
temp=getnode();

if (last == NULL)
{
temp->info = item;
temp->link = temp;
last = temp;
return temp;
}

else
{
temp->info = item;
temp->link= last->link;
last->link = temp;
last = temp;
return last;
}
}

NODE delete_rear(NODE last)


{
NODE temp;

if (last == NULL)
printf("\nList is empty.\n");

temp = last->link;

while(temp->link != last)
temp = temp->link;

temp->link = last->link;
last = temp;
}

void display(NODE last)


{
NODE temp=last->link;
if (last == NULL)
printf("\nList is empty\n");

do {
printf("\t %d", temp->info);
temp = temp->link;
} while (temp != last->link);
}

void main()
{
NODE last=NULL;
int choice, item;
//clrscr();
for(;;)
{
printf("\n1.INSERT FRONT\n2.INSERT REAR\n3.DELETE FRONT\n4.DELETE
REAR\n5.DISPLAY\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Enter the item\n");
scanf("%d",&item);
last=insert_front(item,last);
break;
case 2:printf("Enter the item\n");
scanf("%d",&item);
last=insert_rear(item,last);
break;
case 3:last=delete_front(last);
break;
case 4:last=delete_rear(last);
break;
case 5:display(last);
break;
default:exit(0);
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy