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

Link List Program D

Book

Uploaded by

vishpawar784
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)
13 views

Link List Program D

Book

Uploaded by

vishpawar784
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/ 22

Link List Program

1.singly link list:-


//Linked List in C: Menu Driven Program
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head;

void beginsert ();


void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 9)
{
printf("\n\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete from
Beginning\n
5.Delete from last\n6.Delete node after specified location\n7.Search for an element\n8.Show\
n9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void beginsert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted");
}

}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
ptr -> next = NULL;
head = ptr;
printf("\nNode inserted");
}
else
{
temp = head;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;

ptr->next = NULL;
printf("\nNode inserted");

}
}
}
void randominsert()
{
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter element value");
scanf("%d",&item);
ptr->data = item;
printf("\nEnter the location after which you want to insert ");
scanf("\n%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\ncan't insert\n");
return;
}

}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
}
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nList is empty\n");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\nNode deleted from the begining ...\n");
}
}
void last_delete()
{
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("\nlist is empty");
}
else if(head -> next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the list deleted ...\n");
}

else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("\nDeleted Node from the last ...\n");
}
}
void random_delete()
{
struct node *ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;

if(ptr == NULL)
{
printf("\nCan't delete");
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted node %d ",loc+1);
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d ",i+1);
flag=0;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("Item not found\n");
}
}

void display()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Nothing to print");
}
else
{
printf("\nprinting values . . . . .\n");
while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
}
}

Output:

2.Doubly link list:-


#include<stdio.h>

#include<conio.h>

struct list

int info;

struct list *l,*r;


};

typedef struct list node;

node *first,*now,*temp,*temp1;

void insert(void);

void delet(void);

void disp(void);

main()

int ch;

clrscr();

do

printf("\n\n\t****VIEW MENU****\n");

printf("\t1..INSERT..\n\t2..DELETE..\n\t3..DISPLAY..\n\t4..EXIT\n");

printf("\nEnter Your Choice:==>");

scanf("%d",&ch);

switch (ch)

case 1:

insert();

break;

case 2:

delet();

break;

case 3:

disp();

break;

case 4:

exit();

}while(ch!=5);
getch();

return;

void insert(void)

int s;

clrscr();

do

printf("\n1..Insert First\n2..Insert Last\n3..Insert At Specified Position\n4..Back to main\n ");

printf("\nEnter Your Choice:==>");

scanf("%d",&s);

switch(s)

case 1:

insertf();

break;

case 2:

insertl();

break;

case 3:

insertb();

break;

case 4:

break;

}while(s!=4);

return;

}
void delet(void)

int ch1;

clrscr();

do

printf("\n1..Delete First\n2..Delete Last\n3..Delete At Specified Position\n4..Back to main\n ");

printf("\nEnter Your Choice:==>");

scanf("%d",&ch1);

switch(ch1)

case 1:

deletf();

break;

case 2:

deletl();

break;

case 3:

deletb();

break;

case 4:

break;

}while(ch1!=4);

return;

deletf()

{ if(first==0)

printf("\nThere is no element in list");

else
{

temp1=first;

first=first->r;

first->l=NULL;

free(temp1);

printf("\nNode is Succesfully Deleted\n");

return;

deletb()

int no=0,i=0;

if(first==0)

printf("\nThere is no Element in Link List");

else

printf("\nEnter Position Of Node:=>");

scanf("%d",&no);

if(no==1)

deletf();

else

temp=first;

for(i=1;temp->r!=NULL && i<no;i++)

// temp1=temp;

temp=temp->r;

if(no>i+1)

printf("\n\nPOSITION IS WRONG\n");

else
{

if(i==no)

temp->l->r=temp->r;

temp->r->l=temp->l;

free(temp);

else if(temp->r==NULL)

deletl();

printf("\nNode is Succesfully Deleted\n");

return;

deletl()

temp=first;

if(temp==NULL)

printf("\nThere is No Node In List\n");

getch();

return;

else

while(temp->r!=NULL)

// temp1=temp;

temp=temp->r;
}

temp->l->r=NULL;

free(temp);

printf("\nNode is Succesfully Deleted\n");

return;

insertf()

if(first==0)

first=(node *)malloc(sizeof(node));

printf("\nEnter Info Of Node:=>");

scanf("%d",&first->info);

first->l=NULL;

first->r=NULL;

else

now=(node *)malloc(sizeof(node));

printf("\nEnter Info Of Node:=>");

scanf("%d",&now->info);

now->l=NULL;

now->r=first;

first->l=now;

first=now;

printf("\nNode is Succesfully Inserted\n");

return;

}
insertl()

if(first==0)

first=(node *)malloc(sizeof(node));

printf("\nEnter Info Of Node:=>");

scanf("%d",&first->info);

first->l=NULL;

first->r=NULL;

else

now=(node *)malloc(sizeof(node));

printf("\nEnter Info Of Node:=>");

scanf("%d",&now->info);

temp=first;

while(temp->r!=NULL)

temp=temp->r;

now->r=NULL;

temp->r=now;

now->l=temp;

printf("\nNode is Succesfully Inserted\n");

return;

insertb()

int pos=0,i=0;

if(first==0)

printf("\nThere is no Element in Link List");

else
{

printf("\nEnter Position Of Node:=>");

scanf("%d",&pos);

if(pos==1)

insertf();

else

temp=first;

for(i=1;temp->r!=NULL && i<pos;i++)

temp=temp->r;

if(pos>i+1)

printf("\n\nPOSITION IS WRONG\n");

return;

else

if(i==pos)

now=(node *)malloc(sizeof(node));

printf("\nEnter Info Of Node:=>");

scanf("%d",&now->info);

now->r=temp;

now->l=temp->l;

temp->l->r=now;

temp->l=now;

//temp1->next=now;

else if(temp->r==NULL)
insertl();

printf("\nNode is Succesfully Inserted\n");

return;

void disp(void)

temp=first;

if(temp==0)

printf("\nThere is no Element in List");

else

while(temp!=0)

printf("%d\n",temp->info);

temp=temp->r;

return;

Output:
3.circular link list:-
//Circular LL

#include<stdio.h>
#include<conio.h>
struct list
{
int info;
struct list *next;
};
typedef struct list node;
node *first,*now,*temp,*temp1,*last;
void insert(void);
void delet(void);
void disp(void);
main()
{
int ch;
clrscr();
do
{
printf("\n\n\t****VIEW MENU****\n");
printf("\t1..INSERT..\n\t2..DELETE..\n\t3..DISPLAY..\n\t4..EXIT\n");
printf("\nEnter Your Choice:==>");
scanf("%d",&ch);
switch (ch)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
disp();
break;
case 4:
exit();

}
}while(ch!=5);
getch();
return;
}
void insert(void)
{
int s;
clrscr();
do
{
printf("\n1..Insert First\n2..Insert Last\n3..Insert At Specified Position\n4..Back to main\n ");
printf("\nEnter Your Choice:==>");
scanf("%d",&s);
switch(s)
{
case 1:
insertf();
break;
case 2:
insertl();
break;
case 3:
insertb();
break;
case 4:
break;

}
}while(s!=4);
return;
}

void delet(void)
{
int ch1;
clrscr();
do
{
printf("\n1..Delete First\n2..Delete Last\n3..Delete At Specified Position\n4..Back to main\n ");
printf("\nEnter Your Choice:==>");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
deletf();
break;
case 2:
deletl();
break;
case 3:
deletb();
break;
case 4:
break;

}
}while(ch1!=4);
return;
}

insertf()
{
if(first==0)
{
first=(node *)malloc(sizeof(node));
printf("\nEnter Info Of Node:=>");
scanf("%d",&first->info);
first->next=0;
last=first;
}
else
{
now=(node *)malloc(sizeof(node));
printf("\nEnter Info Of Node:=>");
scanf("%d",&now->info);
now->next=first;
last->next=now;
first=now;
}
printf("\nNode is Succesfully Inserted\n");
return;
}

insertl()
{
if(first==0)
{

first=(node *)malloc(sizeof(node));
printf("\nEnter Info Of Node:=>");
scanf("%d",&first->info);
first->next=NULL;
last=first;
}
else
{
now=(node *)malloc(sizeof(node));
printf("\nEnter Info Of Node:=>");
scanf("%d",&now->info);
temp=first;
while(temp!=last)
temp=temp->next;
last->next=now;
now->next=first;
last=now;
}
printf("\nNode is Succesfully Inserted\n");
return;
}

insertb()
{
int no=0,i=0;
if(first==NULL)
printf("\nThere is no Element in Link List");
else
{
printf("\nEnter Position Of Node:=>");
scanf("%d",&no);
if(no==1)
insertf();
else
{
temp=first;
for(i=1;temp!=last && i<no;i++)
{
temp1=temp;
temp=temp->next;
}
if(no>i+1)
{
printf("\n\nPOSITION IS WRONG\n");
return;
}
else
{
if(i==no)
{
now=(node *)malloc(sizeof(node));
printf("\nEnter Info Of Node:=>");
scanf("%d",&now->info);
now->next=temp1->next;
temp1->next=now;
}
else if(temp==last)
insertl();
}
}
}
printf("\nNode is Succesfully Inserted\n");
return;
}

void disp(void)
{
/* if(temp==NULL)
{
temp=first;
} */

temp=first;

if(temp==last)
printf("\nThere is no Element in List");
else
{
while(temp!=last)
{
printf("%d\n",temp->info);
temp=temp->next;
}
printf("%d\n",temp->info);
return;
}
}

deletf()
{
if(first==last)
{
// last=last->next;
temp=NULL;
free(last);
free(first);
printf("This is last Element Whish is deleted \n");
printf("\nThere is no element in list");
}
else
{
temp1=first;
first=first->next;
free(temp1);
printf("\nNode is Succesfully Deleted\n");
}
return;
}

deletb()
{
int no=0,i=0;
if(first==NULL)
printf("\nThere is no Element in Link List");
else
{
printf("\nEnter Position Of Node:=>");
scanf("%d",&no);
if(no==1)
deletf();
else
{
temp=first;
for(i=1;temp!=last && i<no;i++)
{
temp1=temp;
temp=temp->next;
}
if(no>i+1)
printf("\n\nPOSITION IS WRONG\n");
else
{
if(i==no)
{
temp1->next=temp->next;
free(temp);
}
else if(temp==last)
deletl();
}
}
}
printf("\nNode is Succesfully Deleted\n");
return;
}
deletl()
{
temp=first;
if(temp==last)
printf("\nThere is No Node In List\n");
else
{
while(temp!=last)
{
temp1=temp;
temp=temp->next;
}
temp1->next=first;
last=temp1;
printf("\nNode is Succesfully Deleted %d ",last->info);
free(temp);
}
return;
}

Output:

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