This C Program actualize a doubly linked list and give addition, cancellation and show operations.
Doubly linked list is a connected information structure that comprises of an arrangement of consecutively connected records called nodes.
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<dos.h>
typedef struct
{
int id;
char name[36];
int sal;
}EMP; // just alisa name
EMP getdata()
{
EMP te;
printf("\nENTER ID:");
scanf("%d",&te.id);
printf("Enter Name:");
fflush(stdin);
gets(te.name);
printf("Enter Salary: ");
scanf("%D", &te.sal);
return te;
}
EMP updatedata()
{
EMP te;
printf("\n Enter new Id:");
scanf("%d",&te.id);
printf("Enter new Name: ");
fflush(stdin);
gets(te.name);
printf("Enter New Salary");
scanf("%d", &te.sal);
return te;
}
void showdata(EMP te)
{
printf("\n Id:%d Name:%5s SAL:%3d",
te.id,te.name,te.sal);
}
struct dlink
{
struct dlink*prev;
EMP data;
struct dlink*next;
};
typedef struct dlink LINK; // just alisa name
LINK*head=NULL; // global pointer
void addnode()
{
char ch;
LINK*th1,*th2;
if(head==NULL)
{
head=(LINK*)malloc(sizeof(LINK));
head->prev=NULL;
head->data=getdata();
head->next=NULL;
printf("\n DO You want to continue Y ?:");
fflush(stdin);
ch=getch();
if(ch!='Y'&&ch!='y')
return;
}
th1=head;
while(th1->next!=NULL)
th1=th1->next;
do
{
th2=(LINK*)malloc(sizeof(LINK));
th2->prev=th1;
th2->data=getdata();
th2->next=NULL;
th2->next=th2;
th1=th2;
printf("\n Do You want to continue Y?:");
fflush(stdin);
ch=getchar();
}while(ch= = 'Y' || ch = = 'y');
th1=th2=NULL;
return;
}
void displaynode()
{
LINK*th;
if(head = = NULL)
{
printf("LIST IS EMPTY \n");
system("PAUSE");
return;
}
th=head;
do
{
showdata(th->data);
th=th->next;
}while(th!=NULL);
printf("\n");
system("PAUSE");
return;
}
int nodecount()
{
int count =0;
LINK*th;
if(head == NULL)
return count; // default value is 0;
th=head;
do
{
++count;
th=th->next;
}while(th!= NULL);
return count;
}
void insertnode()
{
LINK*th1,*th2;
int i,lp,nl;
if(head = = NULL)
{
printf("LIST IS EMPTY \n");
system("PAUSE");
return;
}
printf("\n Enter Link position: ");
scanf("%d", &lp);
nl = nodecound();
if(lp<1||lp>nl)
{
printf("invalid link position \n");
system("PAUSE");
return;
}
if(lp = = 1)
{
th1=(LINK*)malloc(sizeof(LINK));
th1->prev=NULL;
th1->data = getdata();
th1->next = head;
head->prev=th1;
head=th1;
return;
}
th1=head;
for(i = 1; i<lp-1; i++)
th1 =th1 ->next;
th2=(LINK*)malloc(sizeof(LINK));
th2->prev =th1;
th2->data = getdata();
th2->next=th1->next;
th1->next=th2;
if(nl!=lp)
th2->next->prev=th2;
th1=th2=NULL;
return;
}
void deletenode()
{
LINK*th1,*th2;
int i, lp,nl;
if(head = = NULL)
{
printf("LIST IS EMPTY \n");
system("PAUSE");
return;
}
printf("\n Enter LINK position:");
scanf("%d",&lp);
nl=nodecount();
if(lp<1||lp>nl)
{
printf("invalid link position \n");
system("PAUSE");
return;
}
if(nl==1)
{
free(head);
head=NULL;
printf("node id deleted \n");
system("PAUSE");
return;
}
if(lp= = 1)
{
th1=head;
head =head->next;
head->prev =NULL;
th1->next =NULL;
free(th1);
th1=NULL;
return;
}
th1 = head;
for(i =1; i<lp-1; i++)
th1=th1->next;
th2=th1->next;
th1->next=th2->next;
if(nl!=lp)//if not tail node only required.
th2->next->prev=th1; //th2->next->prev=th2->prev;
th2->prev=NULL;
th2->next=NULL;
free(th2);
th1=th2 =NULL;
return;
}
void updatenode()
{
LINK*th1;
int nl,lp,i;
if(head = = NULL)
{
printf("LIST IS EMPTY \n");
system("PAUSE");
return;
}
printf("Enter link position: ");
scanf("%d",&lp);
nl=nodecount();
if(lp<1||lp>nl)
{
printf("invalid node position: ");
system("PAUSE");
return;
}
if(lp = = 1)
{
haed->data=updatedata();
printf(" Node is updated \n");
system("PAUSE");
return;
}
th1=head;
for(i=1;i<lp; i++)
th1=th1->next;
th1->data=updatedata();
printf("node is updateed \n");
system("PAUSE");
return;
}
void reversenode()
{
LINK*temp=NULL;
LINK*current=head;
int nl;
if(head==NULL)
{
printf("LIST IS EMPTY \n");
system("PAUSE");
return;
}
nl=nodecound();
if(nl = =1)
{
printf(" We can't do reverse \n");
system("PAUSE");
return;
}
while(current!=NULL)
{
temp=current->prev;
current->prev=current->next;
current->next=temp;
current=current->prev;
}
head=temp->prev;
return;
}
int main()
{
int option;
while(1)
{
system("CLS");
printf("\n n1 FOR ADD NODE:");
printf("\n n2 FOR ADD NODE:");
printf("\n n3 FOR ADD NODE:");
printf("\n n4 FOR ADD NODE:");
printf("\n n5 FOR ADD NODE:");
printf("\n n6 FOR ADD NODE:");
printf("\n n7 FOR ADD NODE:");
printf("\n n8 FOR EXIT: ");
scanf("%d", &option);
switch(option)
{
case 1: addnode();
break;
case 2: displaynode();
break;
case 3:
printf("NODE: %d\n", nodecount());
system("PAUSE");
break;
case 4: insertnode();
break;
case 5: deletenode();
break;
case 6: updatenode();
break;
case 7: reversenode();
break;
case 8: free(head);
return EXIT_SUCCESS;
default: printf(" invalid option");
system("PAUSE");
break;
}
}
}

