0% found this document useful (0 votes)
9 views7 pages

Doubly

This C program implements functions to create and manipulate a doubly linked list. It includes functions to insert nodes at the beginning and end of the list, delete nodes from the beginning and end, display the list, search for a node, count the number of nodes, and allows the user to test these functions through a menu-driven interface.

Uploaded by

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

Doubly

This C program implements functions to create and manipulate a doubly linked list. It includes functions to insert nodes at the beginning and end of the list, delete nodes from the beginning and end, display the list, search for a node, count the number of nodes, and allows the user to test these functions through a menu-driven interface.

Uploaded by

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

#include<stdio.

h>
#include<stdlib.h>
#include<conio.h>
#include<malloc.h>

struct linklist{
int info;
struct linklist *next,*prv;
}*first ,*last;
struct linklist *ins_beg()
{
struct linklist *temp;
temp=(struct linklist *)malloc(sizeof(struct linklist));
temp->next=NULL;
temp->prv=NULL;

// temp->info=x;

printf("enter a value:");
scanf("%d",&temp->info);

if(first==NULL)
{
first=temp;

}
else
{
p=frist;
while(p->next!=NULL)
{
p=p->next;
p->next =temp;
temp->prv=p;
}

}
return first;
}

struct linklist *ins_end()


{
struct linklist *temp;
temp=(struct linklist *)malloc(sizeof(struct linklist));
temp->next=NULL;
temp->prv=NULL;

// temp->info=x;

printf("enter a value:");
scanf("%d",&temp->info);

if(first==NULL)
{
first=temp;

}
else
{
p=first;
while(p->next !=NULL)
{
p=p->next;
p->next=temp;
temp->prv=p;
}

}
return first;
}

void Display()
{
struct linklist *p;
if(first==NULL)
{
printf("There is no data");
}
// else if(first->next ==NULL)
// {
// printf("%d->",first->info);
// }
else
{
p=first;
printf("\ndata is:");

while(p!=NULL)
{
printf("%d->",p->info);
p=p->next;
}
// printf("%d ->",p->info);
}
// p=first;
// while(p!=NULL)
// {
// printf("%d -> ",p->info);
// p=p->next;
// }
// }
}

// struct linklist *ins_end(struct linklist *first)


// {
// struct linklist *temp,*p;
// temp=(struct linklist *)malloc(sizeof(struct linklist));
// temp->next=NULL;

// printf("enter a value:");
// scanf("%d",&temp->info);

// if(first==NULL)
// {
// first=temp;
// }
// else
// {
// p=first;
// while(p->next!=NULL)
// { p=p->next;
// }
// p->next=temp;

// }
// return first;
// }
struct linklist *del_beg()
{
struct linklist *temp,*p;
temp=(struct linklist *)malloc(sizeof(struct linklist));
temp->next=NULL;

// printf("enter a value:");
// scanf("%d",&temp->info);

if(first==NULL)
{
printf("no node there");
}
// else if(first==last)
// {
// printf("deleted element is %d",p->info);
// free(first);
// first=last=NULL;

// }
else
{
p=first;
printf("deleted element is %d",p->info);
first=first->next;
first->prv=NULL;
free(p);
p=NULL;

}
return first;
}

struct linklist *del_end()


{
struct linklist *temp,*p,*q;
temp=(struct linklist *)malloc(sizeof(struct linklist));
temp->next=NULL;

// printf("enter a value:");
// scanf("%d",&temp->info);

if(first==NULL)
{
printf("no node there");
}
// else if(first==last)
// {
// printf("deleted element is %d",p->info);
// free(first);
// first=last=NULL;

// }
else
{
p=first;

while(p !=NULL)
{ q=p;
p=p->next;
}
printf("deleted element is %d",p->info);
last=q;
last->next=NULL;
free(p);
p=NULL;
}
return first;
}

int count(struct linklist *first)


{
int cnt=0;
struct linklist *P;
p=first;
while(p!=last)
{
cnt++;
p=p->next;
}
return cnt;
}

void search(struct linklist *first,int x)


{
int flag=0;
struct linklist *p;
p=first;
while(p!=last)
{
if(p->info==x)
{

flag=1;
break;
}
else
{

p=p->next;
}

}
if(flag==1)
printf("value is%d",p->info);
else
printf("no match found");

// struct linklist *ins_pos(struct linklist *first,int pos)


// {
// struct linklist *temp,*p,*q;
// int i=1;
// temp=(struct linklist *)malloc(sizeof(struct linklist));
// temp->next=NULL;

// printf("enter a value:");
// scanf("%d",&temp->info);

// if(first==NULL)
// {
// first=temp;
// }
// else
// {
// p=first;
// while(p!=NULL && i<pos)
// { q=p;
// p=p->next;
// i++;
// }
// q->next=temp;
// temp->next=p;

// }
// return first;
// }

// struct linklist *del_pos(struct linklist *first,int pos)


// {
// struct linklist *temp,*p,*q;
// int i=1;

// if(pos==1)
// {
// first=del_beg(first);
// }
// else
// {
// p=first;
// while(p!=NULL && i<pos)
// { q=p;
// p=p->next;
// i++;
// }
// if(p!=NULL)
// { `
// q->next=p->next;
// free(p);
// p=NULL;
// }
// else
// {
// printf("there is no position avaible");
// }
// }
// return first;
// }
void main()
{
int choice=0,x;
struct linklist *temp,*first=NULL;

do{

printf("\n 1.insert at First \n 2.insert at Last \n 3.delete at first \n");


printf("\n 4.delete at last");
printf("\n \n 5.search \n 6.count \n7.Display 8. Exit\n");

printf("Enter your Choice : \n");


scanf("%d", &choice);
clrscr();
switch (choice)
{
case 1:

first=ins_beg();
break;
case 2:
first=ins_end();
//first=ins_end(first);
break;
case 3:
del_beg();
//printf("enter position:");
//scanf("%d",&x);
//first=ins_pos(first,x);

break;
case 4:
del_end();
//first=del_beg(first);
break;
case 5:
//first=del_end(first);
break;
case 6:
//printf("enter position:");
//scanf("%d",&x);
//first=del_pos(first,x);

break;
case 7:
Display(first);

//printf("\nnumber of node are:%d",x);


break;
case 8:
exit(0);
//printf("\nenter value to search:");
///scanf("%d",&x);
//search(first,x);
break;
// case 9:
// break;
// case 10:

// break;
// case 11:

// break;

default:
printf("Invalid choice !!");
}

}while(choice!=8);
}

You might also like