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

Lab Programs On Linkedlist

Uploaded by

ThontaDari
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)
9 views

Lab Programs On Linkedlist

Uploaded by

ThontaDari
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/ 8

16.

Write a program to insert an element into a singly linked list


#include<stdio.h>
#include<conio.h>
struct node
{ int info;
struct node *link;
};
typedef struct node N;
N *HEAD;
void creation(int);
void display(N*);
void insertpos(int ,int);
void insertbeg(int);
void insertend(int);
void main()
{
int ele, n,ch,pos;
HEAD=NULL;
clrscr();
printf("\n The linked list creation");
do
{
printf("\nEnter the information field\n");
scanf("%d",&ele);
creation(ele);
printf("\nDo you want another node press 1 otherwise 0\n");
scanf("%d",&n);
}while(n==1);
printf("\n");
display(HEAD);
while(1)
{
printf("\n\nInsertion Operation");
printf("\n1.At the Beggining \n2. At the End \n3. At the Given Position
\n4.Exit");
printf("\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nenter the item\n");
scanf("%d", &ele);
insertbeg(ele);
display(HEAD);
break;
case 2: printf("\nenter the item\n");
scanf("%d", &ele);
insertend(ele);
display(HEAD);
break;
case 3: printf("\nenter the position");
scanf("%d", &pos);
printf("\nenter the item\n");
scanf("%d", &ele);
insertpos(pos,ele);
display(HEAD);
break;
case 4: exit(0);
}
}
}
void creation(int ele)
{
N *newnode,*curptr;

newnode=(N*)malloc(sizeof(N));
newnode->info=ele;
newnode->link=NULL;
if(HEAD==NULL)
{
HEAD=newnode;
}
else
{
curptr=HEAD;
while(curptr->link!=NULL)
curptr=curptr->link;
curptr->link=newnode;
}
}
void display(N *curptr)
{
printf("The linked list is\n");
while(curptr!=NULL)
{
printf("%d-> ",curptr->info);
curptr=curptr->link;
}
printf("null");
}
void insertbeg(int ele)
{
N *newnode,*curptr;
newnode=(N*)malloc(sizeof(N));
newnode->info=ele;
newnode->link=HEAD;
HEAD=newnode;
}
void insertend(int ele)
{
N *newnode,*curptr;
newnode=(N*)malloc(sizeof(N));
newnode->info=ele;
newnode->link=NULL;
curptr=HEAD;
while(curptr->link!=NULL)
curptr=curptr->link;
curptr->link=newnode;
}

void insertpos(int pos,int ele)


{
N *newnode, *curptr;
int i;
newnode=(N*)malloc(sizeof(N));
newnode->info=ele;
curptr=HEAD;
for(i=1;i<pos-1&&curptr!=NULL;i++)
{
curptr=curptr->link;
}
if(curptr==NULL)
{
printf("\nposition out of range");
getch();
}
newnode->link=curptr->link;
curptr->link=newnode;
}
17. Write a program to delete an element from a singly linked list
#include<stdio.h>
#include<conio.h>
struct node
{ int info;
struct node *link;
};
typedef struct node N;
N *HEAD;
void creation(int);
void display(N*);
void deletepos(int);
void deletebeg();
void deleteend();
void main()
{
int ele, n,ch,pos;
HEAD=NULL;
clrscr();
printf("\n The linked list creation");
do
{
printf("\nEnter the information field\n");
scanf("%d",&ele);
creation(ele);
printf("\nDo you want another node press 1 otherwise 0\n");
scanf("%d",&n);
}while(n==1);
printf("\n");
display(HEAD);
while(1)
{
printf("\n\nDeletion Operation");
printf("\n1.At the Beggining \n2. At the End \n3. At the Given Position
\n4.Exit");
printf("\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: deletebeg();
display(HEAD);
break;
case 2: deleteend();
display(HEAD);
break;
case 3: printf("\nenter the position");
scanf("%d", &pos);
deletepos(pos);
display(HEAD);
break;
case 4: exit(0);
}
}
}
void creation(int ele)
{
N *newnode,*curptr;

newnode=(N*)malloc(sizeof(N));
newnode->info=ele;
newnode->link=NULL;
if(HEAD==NULL)
{
HEAD=newnode;
}
else
{
curptr=HEAD;
while(curptr->link!=NULL)
curptr=curptr->link;
curptr->link=newnode;
}
}
void display(N *curptr)
{
printf("The linked list is\n");
while(curptr!=NULL)
{
printf("%d-> ",curptr->info);
curptr=curptr->link;
}
printf("null");
}

void deletebeg()
{
N *curptr;
curptr=HEAD;
HEAD=curptr->link;
printf("\nDeleted Element is %d\n",curptr->info);
free(curptr);
}
void deleteend()
{
N *prevptr,*curptr;
curptr=HEAD;
while(curptr->link!=NULL)
{
prevptr=curptr;
curptr=curptr->link;
}
printf("\nDeleted element is %d\n",curptr->info);
prevptr->link=NULL;
free(curptr);
}

void deletepos(int pos)


{
N *prevptr,*curptr;
int i;
curptr=HEAD;
for(i=1;i<pos-1&&curptr!=NULL;i++)
{
prevptr=curptr;
curptr=curptr->link;
}
if(curptr==NULL)
{
printf("\nposition out of range");
getch();
}
printf("\nDeleted Element is %d",curptr->info);
prevptr->link=curptr->link;
free(curptr);
}

You might also like