#include<stdlib.h>
#include<stdio.h>
typedef struct _NODE_{
int data;
struct _NODE_ * pl;
struct _NODE_ * pr;
} Node,* pNode;
//create node.
pNode CreateNode(int data)
{
pNode new_node=(pNode)malloc(sizeof(Node));
new_node->data=data;
new_node->pl=new_node;
new_node->pr=new_node;
return new_node;
}
//create the list.
pNode CreateList(void)
{
pNode head_list=NULL;
return head_list;
}
//insert the node into the list.
pNode InsertNode(pNode head_list,int data)
{
pNode new_node=CreateNode(data);
if(head_list==NULL)
{
head_list=new_node;
}
else
{
//update the right link.
head_list->pl->pr=new_node;
new_node->pr=head_list;
//update the left link.
new_node->pl=head_list->pl;
head_list->pl=new_node;
}
return head_list;
}
//print the list by right.
void PrintList(pNode head_list)
{
pNode index=head_list;
if(head_list==NULL)
{
printf("the list is empty.\n");
}
else
{
while(index->pr!=head_list)
{
printf("%d->",index->data);
index=index->pr;
}
printf("%d \n",index->data);
}
}
//print the list by right.
void PrintListRev(pNode head_list)
{
pNode index=head_list;
if(head_list==NULL)
{
printf("the list is empty.\n");
}
else
{
while(index->pl!=head_list)
{
printf("%d->",index->data);
index=index->pl;
}
printf("%d \n",index->data);
}
}
//find the node in the list.
pNode FindNode(pNode head_list,int data)
{
pNode index=head_list;
if(head_list==NULL)
{
return NULL;
}
else
{
while(index->pr!=head_list)
{
if(index->data==data)
{
return index;
}
index=index->pr;
}
if(index->data==data)
{
return index;
}
return NULL;
}
}
//update the node in the list.
pNode UpdateNode(pNode head_list,int old_data,int new_data)
{
pNode index=NULL;
if((index=FindNode(head_list,old_data))!=NULL)
{
index->data=new_data;
}
else
{
head_list=InsertNode(head_list,new_data);
}
return head_list;
}
//delete the node in from the list.
pNode DeleteNode(pNode head_list,int data)
{
pNode index=FindNode(head_list,data);
if(index!=NULL)
{
if(index->pr==index)
{
free(head_list);
head_list=NULL;
}
else
{
if(index==head_list)
{
head_list=index->pr;
index->pl->pr=index->pr;
index->pr->pl=index->pl;
}
else
{
index->pl->pr=index->pr;
index->pr->pl=index->pl;
}
free(index);
}
}
return head_list;
}
//destory the list
pNode DestoryList(pNode head_list)
{
pNode index=head_list;
pNode temp=NULL;
if(head_list==NULL)
{
}
else
{
while(index->pr!=head_list)
{
temp=index->pr;
free(index);
index=temp;
}
free(index);
}
return NULL;
}
void MainMenu(void)
{
printf("====Doubly Linked List====\n");
printf("====(1)Create the List====\n");
printf("====(2)Query the Node====\n");
printf("====(3)Update the Node====\n");
printf("====(4)Insert the Node====\n");
printf("====(5)Delete the Node====\n");
printf("====(6)Print by Right===\n");
printf("====(7)Print by Left====\n");
printf("====(8)Destory the List====\n");
printf("====(0)Quit the List====\n");
}
void main()
{
int data;
int data1;
int choice;
int flag=1;
pNode head_list=NULL;
printf("%ld\n",head_list);
while(flag==1)
{
MainMenu();
printf("Input the choice:\n");
scanf("%d",&choice);
if(choice==0)
{
flag=0;
}
switch(choice)
{
case 1:
printf("Create Successed!\n");
head_list=CreateList();
break;
case 2:
printf("Input the data:\n");
scanf("%d",&data);
if(FindNode(head_list,data)!=NULL)
{
printf("Find the Node.\n");
}
else
{
printf("Cannt Find the Node.\n");
}
break;
case 3:
printf("Input the data:eg: 10 5\n");
scanf("%d%d",&data,&data1);
head_list=UpdateNode(head_list,data,data1);
break;
case 4:
printf("Input the data:\n");
scanf("%d",&data);
head_list=InsertNode(head_list,data);
break;
case 5:
printf("Input the data:\n");
scanf("%d",&data);
head_list=DeleteNode(head_list,data);
break;
case 6:
PrintList(head_list);
break;
case 7:
PrintListRev(head_list);
break;
case 8:
head_list=DestoryList(head_list);
break;
default:
break;
}
}
if(head_list!=NULL)
{
DestoryList(head_list);
}
}
#include<stdio.h>
typedef struct _NODE_{
int data;
struct _NODE_ * pl;
struct _NODE_ * pr;
} Node,* pNode;
//create node.
pNode CreateNode(int data)
{
pNode new_node=(pNode)malloc(sizeof(Node));
new_node->data=data;
new_node->pl=new_node;
new_node->pr=new_node;
return new_node;
}
//create the list.
pNode CreateList(void)
{
pNode head_list=NULL;
return head_list;
}
//insert the node into the list.
pNode InsertNode(pNode head_list,int data)
{
pNode new_node=CreateNode(data);
if(head_list==NULL)
{
head_list=new_node;
}
else
{
//update the right link.
head_list->pl->pr=new_node;
new_node->pr=head_list;
//update the left link.
new_node->pl=head_list->pl;
head_list->pl=new_node;
}
return head_list;
}
//print the list by right.
void PrintList(pNode head_list)
{
pNode index=head_list;
if(head_list==NULL)
{
printf("the list is empty.\n");
}
else
{
while(index->pr!=head_list)
{
printf("%d->",index->data);
index=index->pr;
}
printf("%d \n",index->data);
}
}
//print the list by right.
void PrintListRev(pNode head_list)
{
pNode index=head_list;
if(head_list==NULL)
{
printf("the list is empty.\n");
}
else
{
while(index->pl!=head_list)
{
printf("%d->",index->data);
index=index->pl;
}
printf("%d \n",index->data);
}
}
//find the node in the list.
pNode FindNode(pNode head_list,int data)
{
pNode index=head_list;
if(head_list==NULL)
{
return NULL;
}
else
{
while(index->pr!=head_list)
{
if(index->data==data)
{
return index;
}
index=index->pr;
}
if(index->data==data)
{
return index;
}
return NULL;
}
}
//update the node in the list.
pNode UpdateNode(pNode head_list,int old_data,int new_data)
{
pNode index=NULL;
if((index=FindNode(head_list,old_data))!=NULL)
{
index->data=new_data;
}
else
{
head_list=InsertNode(head_list,new_data);
}
return head_list;
}
//delete the node in from the list.
pNode DeleteNode(pNode head_list,int data)
{
pNode index=FindNode(head_list,data);
if(index!=NULL)
{
if(index->pr==index)
{
free(head_list);
head_list=NULL;
}
else
{
if(index==head_list)
{
head_list=index->pr;
index->pl->pr=index->pr;
index->pr->pl=index->pl;
}
else
{
index->pl->pr=index->pr;
index->pr->pl=index->pl;
}
free(index);
}
}
return head_list;
}
//destory the list
pNode DestoryList(pNode head_list)
{
pNode index=head_list;
pNode temp=NULL;
if(head_list==NULL)
{
}
else
{
while(index->pr!=head_list)
{
temp=index->pr;
free(index);
index=temp;
}
free(index);
}
return NULL;
}
void MainMenu(void)
{
printf("====Doubly Linked List====\n");
printf("====(1)Create the List====\n");
printf("====(2)Query the Node====\n");
printf("====(3)Update the Node====\n");
printf("====(4)Insert the Node====\n");
printf("====(5)Delete the Node====\n");
printf("====(6)Print by Right===\n");
printf("====(7)Print by Left====\n");
printf("====(8)Destory the List====\n");
printf("====(0)Quit the List====\n");
}
void main()
{
int data;
int data1;
int choice;
int flag=1;
pNode head_list=NULL;
printf("%ld\n",head_list);
while(flag==1)
{
MainMenu();
printf("Input the choice:\n");
scanf("%d",&choice);
if(choice==0)
{
flag=0;
}
switch(choice)
{
case 1:
printf("Create Successed!\n");
head_list=CreateList();
break;
case 2:
printf("Input the data:\n");
scanf("%d",&data);
if(FindNode(head_list,data)!=NULL)
{
printf("Find the Node.\n");
}
else
{
printf("Cannt Find the Node.\n");
}
break;
case 3:
printf("Input the data:eg: 10 5\n");
scanf("%d%d",&data,&data1);
head_list=UpdateNode(head_list,data,data1);
break;
case 4:
printf("Input the data:\n");
scanf("%d",&data);
head_list=InsertNode(head_list,data);
break;
case 5:
printf("Input the data:\n");
scanf("%d",&data);
head_list=DeleteNode(head_list,data);
break;
case 6:
PrintList(head_list);
break;
case 7:
PrintListRev(head_list);
break;
case 8:
head_list=DestoryList(head_list);
break;
default:
break;
}
}
if(head_list!=NULL)
{
DestoryList(head_list);
}
}