21
Single linked list Program: #include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int info; struct node *next; }; void main() { struct node *s,*start,*prev,*new1,*temp,*temp1,*ptemp; int cho,i,j,x,n,p; do { clrscr(); printf("\n\t\t SINGLE LINKED LIST \n"); printf("\t\t ****************** \n\n"); printf("\t\t\t 1.Create \n"); printf("\t\t\t 2.Insert \n");

Single linked list - chettinadtech.ac.inchettinadtech.ac.in/storage/11-09-22/11-09-22-05-03-36-1167...Single linked list Program: #include

Embed Size (px)

Citation preview

Single linked list 

Program:

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

struct node

{

int info;

struct node *next;

};

void main()

{

struct node *s,*start,*prev,*new1,*temp,*temp1,*ptemp;

int cho,i,j,x,n,p;

do

{

clrscr();

printf("\n\t\t SINGLE LINKED LIST \n");

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

printf("\t\t\t 1.Create \n");

printf("\t\t\t 2.Insert \n");

printf("\t\t\t 3.Delete \n");

printf("\t\t\t 4.Display \n");

printf("\t\t\t 5.Exit \n");

printf("\n\t\t Enter Your Choice -> ");

scanf("%d",&cho);

switch(cho)

{

case 1:

{

clrscr();

printf("\n\t\t Single Linked List Create \n");

printf("\t\t _________________________ \n");

printf("\n\n\t\t Enter the first value : ");

scanf("%d",&x);

new1=(struct node*)malloc(sizeof(struct node));

new1->info=x;

new1->next=NULL;

prev=new1;

start=new1;

temp=start;

while(x!=0)

{

printf("\t\t Enter the next value : ");

scanf("%d",&x);

if(x!=0)

{

new1=(struct node*)malloc(sizeof(struct node));

new1->info=x;

new1->next=NULL;

start->next=new1;

start=new1;

}

}

printf("\n\t\t Linked List Created");

getch();

break;

}

case 2:

{

clrscr();

printf("\n\t\t Single Linked List Insert \n");

printf("\t\t ~~~~~~~~~~~~~~~~~~~~~~~~~ \n");

start=prev;

printf("\t\t Enter the position : ");

scanf("%d",&p);

printf("\t\t Enter the value : ");

scanf("%d",&x);

s=(struct node*)malloc(sizeof(struct node));

s->info=x;

if(p!=1)

{

for(i=1;i<p;i++)

{

temp1=start;

start=start->next;

}

temp1->next=s;

s->next=start;

}

else if(p==1)

{

s->next=prev;

prev=s;

}

break;

}

case 3:

{

clrscr();

printf("\n\t\t Single Linked List Delete ");

printf("\n\t\t _________________________ \n");

start=prev;

printf("\n\t\t Enter the position : ");

scanf("%d",&p);

if(p!=1)

{

for(i=1;i<p;i++)

{

ptemp=start;

start=start->next;

}

temp=start;

ptemp->next=temp->next;

temp=ptemp->next;

}

else

if(p==1)

{

start=start->next;

prev=start;

}

printf("\n\t\t Deleted ");

break;

}

case 4:

{

clrscr();

printf("\n\t\t Single Linked List Display");

printf("\n\t\t __________________________ \n\n\n\t");

temp=prev;

while(temp!=NULL)

{

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

temp=temp->next;

}

printf("| NULL |");

getch();

break;

}

case 5:

exit(0);

break;

}

}while(cho!=5);

}

DOUBLY LINKED LIST

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

struct node

{

int info;

struct node *next;

struct node *prev1;

};

void main()

{

struct node *s,*start,*prev,*new1,*temp,*temp1,*ptemp;

int p,x,cho,i,n;

do

{

clrscr();

printf("\n\t\t Doubly Linkedlist ");

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

printf("\t\t\t 1.Create \n");

printf("\t\t\t 2.Insert\n");

printf("\t\t\t 3.Delete\n");

printf("\t\t\t 4.Display \n");

printf("\t\t\t 5.Exit \n");

printf("\n\t\t Enter Your Choice: ");

scanf("%d",&cho);

switch(cho)

{

case 1:

{

int x;

clrscr();

printf("\n\t\t DOUBLY LINKED LIST CREATE");

printf("\n\t\t _________________________\n");

new1=(struct node*)malloc(sizeof(struct node));

printf("\n\t\t Enter the First value : ");

scanf("%d", &x);

new1->info=x;

new1->next=NULL;

new1->prev1=NULL;

prev=new1;

start=new1;

temp=start;

while(x!=0)

{

printf("\t\t Enter the next value : ");

scanf("%d",&x);

new1=(struct node*)malloc(sizeof(struct node));

if(x!=0)

{

new1->info=x;

new1->next=NULL;

new1->prev1=start;

start->next=new1;

start=new1;

}

i++;

n++;

}

printf("\n\t\t Linked List Created");

getch();

break;

}

case 2:

{

clrscr();

printf("\n\t\t DOUBLY LINKEDLIST INSERT");

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

start=prev;

s=(struct node*)malloc(sizeof(struct node));

printf("\n\t\t Enter the position ");

scanf("%d",&p);

printf("\n\t\t Enter the value ");

scanf("%d",&x);

s->info=x;

n++;

if(p!=1)

{

for(i=1;i<p;i++)

{

temp1=start;

start=start->next;

}

s->prev1=temp1;

temp1->next=s;

s->next=start;

start=s->next;

}

else

if(p==1)

{

s->prev1=NULL;

s->next=prev;

prev=s;

}

getch();

break;

}

case 3:

{

clrscr();

printf("\n\t\t DOUBLY LINKEDLIST DELETE");

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

start=prev;

printf("\n\t\t Enter the position");

scanf("%d",&p);

if(p!=1)

{

for(i=1;i<p;i++)

{

ptemp=start;

start=start->next;

}

temp=start;

ptemp->next=temp->next;

temp->next=ptemp;

temp=ptemp->next;

}

else if(p==1)

{

start=start->next;

prev=start;

prev->prev1=NULL;

}

printf("\n\t\t Deleted ");

getch();

break;

}

case 4:

{

clrscr();

printf("\n\t\t DOUBLY LINKED LIST DISPLAY");

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

temp=prev;

n=0;

printf("\n\t\t NULL ");

while(temp!=NULL)

{

n++;

printf(" <=> %d ",temp->info);

temp=temp->next;

}

printf("NULL");

getch();

break;

}

case 5:

exit(0);

break;

}

}while(cho!=5);

}

CIRCULAR LINKED LIST

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

struct node

{

int info;

struct node *next;

};

void main()

{

struct node *s,*start,*inode,*head,*prev,*new1,*temp,*temp1,*ptemp;

int cho,i=0,j,x,n,p;

do

{

clrscr();

printf("\n\t\t CIRCULAR LINKED LIST \n");

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

printf("\t\t\t 1.Create \n");

printf("\t\t\t 2.Insert \n");

printf("\t\t\t 3.Delete \n");

printf("\t\t\t 4.Display \n");

printf("\t\t\t 5.Exit \n");

printf("\n\t\t Enter Your Choice -> ");

scanf("%d",&cho);

switch(cho)

{

case 1:

{

clrscr();

printf("\n\t\t Circular Linked List Create \n");

printf("\t\t _________________________ \n");

new1=(struct node*)malloc(sizeof(struct node));

printf("\n\n\t\t Enter the first value : ");

scanf("%d",&x);

new1->info=x;

new1->next=NULL;

prev=new1;

start=new1;

temp=start;

n=0;

while(x!=0)

{

new1=(struct node*)malloc(sizeof(struct node));

printf("\t\t Enter the next value : ");

scanf("%d",&x);

if(x!=0)

{

new1->info=x;

new1->next=temp;

start->next=new1;

start=new1;

}

i++;

n++;

}

printf("\n\t\t Linked List Created");

getch();

break;

}

case 2:

{

clrscr();

printf("\n\t\t Circular Linked List Insert \n");

printf("\t\t ------------------------- \n");

start=prev;

s=(struct node*)malloc(sizeof(struct node));

printf("\t\t Enter the position : ");

scanf("%d",&p);

printf("\t\t Enter the value : ");

scanf("%d",&x);

s->info=x;

n++;

if(p!=1)

{

for(i=1;i<p;i++)

{

temp1=start;

start=start->next;

}

temp1->next=s;

s->next=start;

}

else

if(p==1)

{

s->next=prev;

prev=s;

}

break;

}

case 3:

{

clrscr();

printf("\n\t\t Circular Linked List Delete ");

printf("\n\t\t --------------------------- \n");

inode=prev;

printf("Enter the position : ");

scanf("%d",&p);

if(p!=1)

{

for(i=1;i<p;i++)

{

ptemp=inode;

inode=ptemp->next;

}

temp=inode;

temp= temp->next;

ptemp->next=temp;

temp=ptemp->next;

}

else if(p==1)

{

inode=inode->next;

start->next=inode;

prev=inode;

}

printf("\n\t\t Deleted ");

getch();

break;

}

case 4:

{

clrscr();

printf("\n\t\t circular Linked List Display \n");

printf("\n\t\t ----------------------------\n\n\n\t");

temp=prev;

head= temp;

while(head!=NULL)

{

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

temp=temp->next;

if(head==temp)

break;

}

getch();

break;

}

case 5:

exit(0);

break;

}

}while(cho!=5);

}