前面我们介绍了单链表,单链表的优点是没有空间的限制,可以随意开辟空间。但与我们这次要讲的双链表相比,就有点相形见绌了。因为哪怕是单链表,在进行查找、插入、排序等等时都要进行线性表的遍历,而我们往往需要的是目标节点的前一个节点,所以经常一不小心就错过了我们需要的节点,或者经常需要一个当前节点的备份,以便保存我们需要的节点。双链表的出现,极大地统一了线性表的各种算法。
节点结构:
链表结构:
头文件List.h
#ifndef _DLIST_H
#define _DLIST_H
#include<iostream>
#include<assert.h>
using namespace std;
#define ElemType int
typedef struct Node
{
ElemType data;
struct Node *next;
struct Node *prio;
}Node;
typedef struct List
{
Node *first;
Node *last;
size_t size;
}List;
void InitList(List *list);
bool push_back(List *list,ElemType x);
bool push_front(List *list,ElemType x);
void ShowList(List *list);
bool pop_back(List *list);
bool pop_front(List *list);
bool insert_val(List *list,ElemType x);
bool delete_val(List *list,ElemType x);
Node *find(List *list,ElemType x);
ElemType getvalue(List *list,int x);
bool clear(List *list);
#endif
函数实现:List.cpp
#include"DList.h"
void InitList(List *list)
{
Node *s = (Node *)malloc(sizeof(Node));
assert(s != NULL);
s->prio = NULL;
s->next = NULL;
list->first = list->last = s;
list->size = 0;
}
bool push_back(List *list,ElemType x)
{
Node *s=(Node *)malloc(sizeof(Node));
if (s==NULL)
{
return false;
}
s->data=x;
s->next=NULL;
s->prio=list->last;
list->last->next=s;
list->last=s;
list->size++;
return true;
}
bool push_front(List *list,ElemType x)
{
Node *s=(Node *)malloc(sizeof(Node));
s->data=x;
s->next=list->first->next;
s->prio=list->first;
list->first->next=s;
if (list->size==0)
{
list->last=s;
}
list->size++;
return true;
}
void ShowList(List *list)
{
Node *p = list->first->next;
while(p != NULL)
{
cout<<p->data<<"-->";
p = p->next;
}
cout<<"Nul"<<endl;
}
bool pop_back(List *list)
{
if (list->size==0)
{
return false;
}
Node *p=list->last;
list->last=p->prio;
free(p);
list->last->next=NULL;
list->size--;
return true;
}
bool pop_front(List *list)
{
if (list->size==0)
{
return false;
}
Node *p=list->first->next;
if (list->size==1)
{
list->first->next=NULL;
list->last=list->first;
}
else
{
p->next->prio=list->first;
list->first->next=p->next;
}
free(p);
list->size--;
return true;
}
bool insert_val(List *list,ElemType x)
{
Node *s=(Node *)malloc(sizeof(Node));
if (s==NULL)
{
return false;
}
s->data=x;
if (list->size==0)
{
list->first->next=s;
s->prio=list->first;
s->next=NULL;
list->last=s;
}
else
{
Node *p=list->first;
while (p->next!=NULL&&p->next->data<x)
{
p=p->next;
}
if (p==list->last)
{
list->last=s;
s->next=NULL;
s->prio=p;
p->next=s;
list->size++;
return true;
}
s->next=p->next;
p->next->prio=s;
p->next=s;
s->prio=p;
p=p->next;
}
list->size++;
return true;
}
bool delete_val(List *list,ElemType x)
{
Node *p=list->first->next;
while (p->next!=NULL)
{
if (p->data==x)
{
p->prio->next=p->next;
p->next->prio=p->prio;
free(p);
return true;
}
p=p->next;
}
if (p==list->last&&p->data==x)
{
list->last=p->prio;
p->prio->next=NULL;
free(p);
return true;
}
return false;
}
Node *find(List *list,ElemType x)
{
if (list->size==0)
{
return NULL;
}
Node *p=list->first->next;
while (p->next!=NULL)
{
if (p->data==x)
{
return p;
}
p=p->next;
}
if (p==list->last&&p->data==x)
{
return p;
}
return NULL;
}
ElemType getvalue(List *list,int x)
{
if (list->size==0||x<1||x>list->size)
{
return -1;
}
Node *p=list->first;
for (int i=0;i<x;i++)
{
p=p->next;
}
return p->data;
}
bool clear(List *list)
{
if (list->size==0)
{
return false;
}
Node *p=list->first->next;
Node *q=p;
while (p->next!=NULL)
{
free(p);
p=q->next;
}
return true;
}
主函数:main.cpp
#include"DList.h"
void main()
{
List mylist;
InitList(&mylist);
int select = 1;
ElemType item;
Node *p = NULL;
int pos;
while(select)
{
cout<<"**********************************"<<endl;
cout<<"* [1] push_back [2] push_front *"<<endl;
cout<<"* [3] show_seqlist[0] quit_system*"<<endl;
cout<<"* [4] pop_back [5] pop_front *"<<endl;
cout<<"* [6] insert_pos [7] insert_val *"<<endl;
cout<<"* [8] delete_pos [9] delete_val *"<<endl;
cout<<"* [10] find [11]getvalue *"<<endl;
cout<<"* [12] modify [13]clear *"<<endl;
cout<<"* [14] destroy [15]sort *"<<endl;
cout<<"* [16] resver [17]length *"<<endl;
cout<<"* [18] next [19]prio *"<<endl;
cout<<"**********************************"<<endl;
cout<<"请选择:>";
cin>>select;
switch(select)
{
case 1:
cout<<"请输入要插入的数据(-1结束):>";
while(cin>>item,item!=-1)
{
push_back(&mylist,item);
}
break;
case 2:
cout<<"请输入要插入的数据(-1结束):>";
while(cin>>item,item!=-1)
{
push_front(&mylist,item);
}
break;
case 3:
ShowList(&mylist);
break;
case 4:
pop_back(&mylist);
break;
case 5:
pop_front(&mylist);
break;
case 6:
break;
case 7:
cout<<"请输入要插入的值:>";
cin>>item;
insert_val(&mylist,item);
break;
case 8:
break;
case 9:
cout<<"请输入要删除的值:>";
cin>>item;
delete_val(&mylist,item);
break;
case 10:
cout<<"请输入要查找的值:>";
cin>>item;
p=find(&mylist,item);
if(p!=NULL)
{
cout<<p->data<<endl;
}
else
{
cout<<"没有此值!"<<endl;
}
break;
case 11:
cout<<"请输入要查找的值:>";
cin>>item;
cout<<getvalue(&mylist,item)<<endl;
break;
case 12:
break;
case 13:
clear(&mylist);
break;
case 15:
break;
case 16:
break;
case 18:
break;
default:
break;
}
}
}