#include <stdio.h>
#include <stdlib.h>
typedef struct DoubleList
{
struct DoubleList *Pre;
struct DoubleList *Next;
int data;
} Node;
int Nodenum=0;
//增加一个节点
int IsEmpty(Node **Start,Node **End)
{
if((*Start==NULL)&&(*End==NULL))
return 1;
else
return 0;
}
void InsertNode(Node *NewNode,Node **Start,Node **End)
{
Node *CurrentNode;
Node *PreviewNode;
if(*End==NULL) //如果尾部指针值为NULL,说明是空链表
{
NewNode->Next=NULL;
NewNode->Pre =NULL;
*Start=NewNode;
*End =NewNode;
Nodenum++;
return;
}
CurrentNode=*Start;//指向第一个节点
PreviewNode=NULL;
while(CurrentNode!=NULL)
{
if(CurrentNode->data<NewNode->data)
{
PreviewNode=CurrentNode;
CurrentNode=CurrentNode->Next;
}
else
{
if(CurrentNode->Pre!=NULL)//说明不是第一个元素
{
//先解决新节点的前驱后继
NewNode->Next=CurrentNode;
NewNode->Pre=CurrentNode->Pre;
CurrentNode->Pre->Next=NewNode;
CurrentNode->Pre=NewNode;
Nodenum++;
return ;
}
//如果是第一个节点
CurrentNode->Pre=NewNode;
NewNode->Next=CurrentNode;
NewNode->Pre=NULL;
*Start=NewNode;
Nodenum++;
return;
}
}
//如果是在表的末尾的话
NewNode->Next=NULL;
PreviewNode->Next=NewNode;
NewNode->Pre=PreviewNode;
*End=NewNode;
Nodenum++;
}
void CreateList(Node **Start,Node **End)
{
Node *MallocPoint=(Node *)malloc(sizeof(Node));
printf("请输入节点数据!\n");
scanf("%d",&MallocPoint->data);
while(MallocPoint->data!=-1)
{
InsertNode(MallocPoint,Start,End);
MallocPoint=(Node *)malloc(sizeof(Node));
scanf("%d",&MallocPoint->data);
}
}
void desplay(Node **Start,Node **End,int Dir)
{
Node *point;
if(Dir==1)
{
point=*Start;
if(IsEmpty(Start,End))
{
printf("这个链表为空链表\n");
return;
}
printf("正向遍历\n");
while(point!=NULL)
{
printf("%d\n",point->data);
point=point->Next;
}
}
else if(Dir==0)
{
point=*End;
if(IsEmpty(Start,End))
{
printf("这个链表为空链表\n");
return;
}
printf("反向遍历\n");
while(point!=NULL)
{
printf("%d\n",point->data);
point=point->Pre;
}
}
else
{
printf("You are input a wrong Dir\n");
}
}
int SearchNode(Node **Start,Node **End,int data,Node **ReturnNode)
{
Node *Point=*Start;
while((Point!=NULL)&&(data!=Point->data))
{
Point=Point->Next;
}
if(Point!=NULL)
{
*ReturnNode=Point;
return 1;
}
else
{
*ReturnNode=NULL;
return 0;
}
}
int DeleIterm(Node **start,Node **end,int Nodedata)
{
Node *Point=*start;
while((Point!=NULL)&&(Nodedata!=Point->data))
{
Point=Point->Next;
}
if((Point!=NULL)&&(Nodedata==Point->data))
{
if(Point==*start)//如果是第一个节点的话
{
if(Point->Next==NULL)
{
*start=NULL;//只有最后一项了
}else
{
*start=Point->Next;
Point->Next->Pre=NULL;
}
}
else if(Point->Next==NULL)
{
*end=Point->Pre;
Point->Pre->Next=NULL;
}
else
{
Point->Pre->Next=Point->Next;
Point->Next->Pre=Point->Pre;
}
Nodenum--;
free(Point);
return 1;
}
return 0;
}
void DeleList(Node **Start,Node **End)
{
Node *Point;
Point=*Start;
while(Point->Next!=NULL)
{
*Start=Point->Next;
free(Point);
Point=*(Start);
Nodenum--;
}
if(Point->Next==NULL)
{
free(Point);
*Start=NULL;
*End=NULL;
Nodenum--;
}
}
int main()
{
Node *Start = NULL;
Node *End = NULL;
int tmp=0;
Node **ReturnNode;//Node **ReturnNode=NULL;// 不行!
CreateList(&Start,&End);
desplay(&Start,&End,1);
desplay(&Start,&End,0);
printf("输入你要搜索的数字\n");
scanf("%d",&tmp);
if(SearchNode(&Start,&End,tmp,ReturnNode))
{
printf("找到 ");
printf("%d\n",(*ReturnNode)->data);
}
else
{
printf("没找到 %d!\n",tmp);
}
printf("输入你要删除的数字\n");
scanf("%d",&tmp);
if(DeleIterm(&Start,&End,tmp))
{
printf("已经删除 %d\n",tmp);
}else
{
printf("链表中没有找到你要删除的数据 %d\n",tmp);
}
desplay(&Start,&End,1);
printf("清空双向链表!\n");
DeleList(&Start,&End);
desplay(&Start,&End,1);
}
#include <stdlib.h>
typedef struct DoubleList
{
struct DoubleList *Pre;
struct DoubleList *Next;
int data;
} Node;
int Nodenum=0;
//增加一个节点
int IsEmpty(Node **Start,Node **End)
{
if((*Start==NULL)&&(*End==NULL))
return 1;
else
return 0;
}
void InsertNode(Node *NewNode,Node **Start,Node **End)
{
Node *CurrentNode;
Node *PreviewNode;
if(*End==NULL) //如果尾部指针值为NULL,说明是空链表
{
NewNode->Next=NULL;
NewNode->Pre =NULL;
*Start=NewNode;
*End =NewNode;
Nodenum++;
return;
}
CurrentNode=*Start;//指向第一个节点
PreviewNode=NULL;
while(CurrentNode!=NULL)
{
if(CurrentNode->data<NewNode->data)
{
PreviewNode=CurrentNode;
CurrentNode=CurrentNode->Next;
}
else
{
if(CurrentNode->Pre!=NULL)//说明不是第一个元素
{
//先解决新节点的前驱后继
NewNode->Next=CurrentNode;
NewNode->Pre=CurrentNode->Pre;
CurrentNode->Pre->Next=NewNode;
CurrentNode->Pre=NewNode;
Nodenum++;
return ;
}
//如果是第一个节点
CurrentNode->Pre=NewNode;
NewNode->Next=CurrentNode;
NewNode->Pre=NULL;
*Start=NewNode;
Nodenum++;
return;
}
}
//如果是在表的末尾的话
NewNode->Next=NULL;
PreviewNode->Next=NewNode;
NewNode->Pre=PreviewNode;
*End=NewNode;
Nodenum++;
}
void CreateList(Node **Start,Node **End)
{
Node *MallocPoint=(Node *)malloc(sizeof(Node));
printf("请输入节点数据!\n");
scanf("%d",&MallocPoint->data);
while(MallocPoint->data!=-1)
{
InsertNode(MallocPoint,Start,End);
MallocPoint=(Node *)malloc(sizeof(Node));
scanf("%d",&MallocPoint->data);
}
}
void desplay(Node **Start,Node **End,int Dir)
{
Node *point;
if(Dir==1)
{
point=*Start;
if(IsEmpty(Start,End))
{
printf("这个链表为空链表\n");
return;
}
printf("正向遍历\n");
while(point!=NULL)
{
printf("%d\n",point->data);
point=point->Next;
}
}
else if(Dir==0)
{
point=*End;
if(IsEmpty(Start,End))
{
printf("这个链表为空链表\n");
return;
}
printf("反向遍历\n");
while(point!=NULL)
{
printf("%d\n",point->data);
point=point->Pre;
}
}
else
{
printf("You are input a wrong Dir\n");
}
}
int SearchNode(Node **Start,Node **End,int data,Node **ReturnNode)
{
Node *Point=*Start;
while((Point!=NULL)&&(data!=Point->data))
{
Point=Point->Next;
}
if(Point!=NULL)
{
*ReturnNode=Point;
return 1;
}
else
{
*ReturnNode=NULL;
return 0;
}
}
int DeleIterm(Node **start,Node **end,int Nodedata)
{
Node *Point=*start;
while((Point!=NULL)&&(Nodedata!=Point->data))
{
Point=Point->Next;
}
if((Point!=NULL)&&(Nodedata==Point->data))
{
if(Point==*start)//如果是第一个节点的话
{
if(Point->Next==NULL)
{
*start=NULL;//只有最后一项了
}else
{
*start=Point->Next;
Point->Next->Pre=NULL;
}
}
else if(Point->Next==NULL)
{
*end=Point->Pre;
Point->Pre->Next=NULL;
}
else
{
Point->Pre->Next=Point->Next;
Point->Next->Pre=Point->Pre;
}
Nodenum--;
free(Point);
return 1;
}
return 0;
}
void DeleList(Node **Start,Node **End)
{
Node *Point;
Point=*Start;
while(Point->Next!=NULL)
{
*Start=Point->Next;
free(Point);
Point=*(Start);
Nodenum--;
}
if(Point->Next==NULL)
{
free(Point);
*Start=NULL;
*End=NULL;
Nodenum--;
}
}
int main()
{
Node *Start = NULL;
Node *End = NULL;
int tmp=0;
Node **ReturnNode;//Node **ReturnNode=NULL;// 不行!
CreateList(&Start,&End);
desplay(&Start,&End,1);
desplay(&Start,&End,0);
printf("输入你要搜索的数字\n");
scanf("%d",&tmp);
if(SearchNode(&Start,&End,tmp,ReturnNode))
{
printf("找到 ");
printf("%d\n",(*ReturnNode)->data);
}
else
{
printf("没找到 %d!\n",tmp);
}
printf("输入你要删除的数字\n");
scanf("%d",&tmp);
if(DeleIterm(&Start,&End,tmp))
{
printf("已经删除 %d\n",tmp);
}else
{
printf("链表中没有找到你要删除的数据 %d\n",tmp);
}
desplay(&Start,&End,1);
printf("清空双向链表!\n");
DeleList(&Start,&End);
desplay(&Start,&End,1);
}