1、双向链表的概念
双向链表还是一个链表:在逻辑上是连续存储的,在物理上是不连续存储的。
双向链表的每个结点除了存储后一个结点的地址,还需要存储前一个结点的地址。————从前向后遍历、从后往前遍历。
结构和方法的声明
#include<stdio.h>
typedef int DataType;
typedef struct Node
{
//数据类型
union//结合体、联合体
{
DataType data;//存储元素
int length;//存储元素个数
};
//结点指针类型
struct Node *prior;
struct Node *next;
}DoubleList;
//初始化
void InitDoubleList(DoubleList *head);
//销毁
void DestroyDoubleList(DoubleList *head);
//按位置插入插入
bool InsertPos(DoubleList *head,DataType value,int pos);
bool DeletePos(DoubleList *head,int pos);
//求长度
int GetLength(DoubleList *head);
//输出
bool PrintDoubleList(DoubleList *head);
方法的实现
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
#include"head.h"
static DoubleList *ApplyNode(DataType value,DoubleList *prior,DoubleList *next )
{
DoubleList *new_node=(DoubleList *)malloc(sizeof(DoubleList));
if(new_node==NULL) return NULL;
new_node->data=value;
new_node->next=next;
new_node->prior =prior;
return new_node;
}
void InitDoubleList(DoubleList *head)
{
if(head==NULL) exit(0);
head->length=0;
head->next=head->prior =NULL;
}
int GetLength(DoubleList *head)
{
if(head==NULL) exit(0);
return head->length;
}
bool InsertPos(DoubleList *head,DataType value,int pos)
{
if(head==NULL) exit(0);
if(pos<0||pos>GetLength(head)) return false;
DoubleList *p=head;
while(pos)
{
p=p->next;
pos--;
}
DoubleList *new_node=ApplyNode(value,p,p->next);//生成新的结点
if(new_node==NULL) return false;
if(p->next!= NULL) p->next->prior=new_node;
p->next=new_node;
head->length++;
return true;
}
bool DeletePos(DoubleList *head,int pos)
{
if (head == NULL) exit(0);
if(pos<0||pos>GetLength(head)) return false;
DoubleList *p=head;
while(pos>0)
{
p=p->next;
pos--;
}
DoubleList *q=p->next;
if(q->next!=NULL) q->next->prior=p;
p->next=q->next;
free(q);
head->length--;
return true;
}
bool PointDoubleList(DoubleList *head)
{
if (head == NULL) exit(0);
DoubleList *p = head->next;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
DoubleList *q=head;
int pos=GetLength(head);
while(pos>0)
{
q=q->next;
pos--;
}
while (q != head)
{
printf("%d ", q->data);
q = q->prior;
}
printf("\n");
}
void DestroyDoubleList(DoubleList *head)
{
if(head==NULL) exit(0);
if(head->next!=NULL)
{
DeletePos(head,0);
}
}
int main()
{
DoubleList list;
InitDoubleList(&list);
InsertPos(&list,2,0);
InsertPos(&list,3,1);
InsertPos(&list,4,2);
PointDoubleList(&list);
DeletePos (&list,0);
PointDoubleList(&list);
return 0;
}
双向循环链表
方法声明
#include<stdio.h>
typedef int DataType;
typedef struct Node
{
//数据类型
union//结合体、联合体
{
DataType data;//存储元素
int length;//存储元素个数
};
//结点指针类型
struct Node *prior;
struct Node *next;
}DoubleCycleList;
//初始化
void InitDoubleCycleList(DoubleCycleList *head);
//销毁
void DestroyDoubleCycleList(DoubleCycleList *head);
//按位置插入插入
bool InsertPos(DoubleCycleList *head,DataType value,int pos);
bool DeletePos(DoubleCycleList *head,int pos);
//求长度
int GetLength(DoubleCycleList *head);
//输出
bool PrintDoubleCycleList(DoubleCycleList *head);
方法实现:
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
#include"head.h"
static DoubleCycleList *ApplyNode(DataType value,DoubleCycleList *prior,DoubleCycleList *next )
{
DoubleCycleList *new_node=(DoubleCycleList *)malloc(sizeof(DoubleCycleList));
if(new_node==NULL) return NULL;
new_node->data=value;
new_node->next=next;
new_node->prior =prior;
return new_node;
}
static bool InsertAfterNode(DoubleCycleList *now,DataType value)
{
DoubleCycleList *new_node=ApplyNode(value,now,now->next);
if(new_node==NULL) return false;
now->next->prior=new_node;
now->next=new_node;
return true;
}
void InitDoubleCycleList(DoubleCycleList *head)
{
if(head==NULL) exit(0);
head->length=0;
head->next=head->prior =head;
}
int GetLength(DoubleCycleList *head)
{
if(head==NULL) exit(0);
return head->length;
}
bool InsertPos(DoubleCycleList *head,DataType value,int pos)
{
if(head==NULL) exit(0);
if(pos<0) return false;
pos%=(head->length+1);
DoubleCycleList *p=head;
while(pos)
{
p=p->next;
pos--;
}
if(InsertAfterNode(p,value))
{
head->length++;
return true;
}
return false;
}
bool InsertFront(DoubleCycleList *head,DataType value)
{
return InsertPos(head,value,0);
}
bool InsertRear(DoubleCycleList *head,DataType value)
{
if(head==NULL) exit(0);
DoubleCycleList *p=head->prior;
if(InsertAfterNode(p,value))
{
head->length++;
return true;
}
return false;
}
bool DeletePos(DoubleCycleList *head,int pos)
{
if (head == NULL) exit(0);
if(pos<0||pos>GetLength(head)) return false;
DoubleCycleList *p=head;
pos%=(head->length+1);
while(pos>0)
{
p=p->next;
pos--;
}
DoubleCycleList *q=p->next;
if(q->next!=NULL) q->next->prior=p;
p->next=q->next;
free(q);
head->length--;
return true;
}
bool DeleteRear(DoubleCycleList *head)
{
if(head==NULL) exit(0);
if(head->length==0) return false;
DoubleCycleList *p=head->prior;
p->prior->next=p->next;
p->next->prior=p->prior;
free(p);
head->length--;
return true;
}
bool PointDoubleCycleList(DoubleCycleList *head)
{
if (head == NULL) exit(0);
DoubleCycleList *p = head->next;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
if(p==head) break;
}
printf("\n");
DoubleCycleList *q=head;
int pos=GetLength(head);
while(pos>0)
{
q=q->next;
pos--;
}
while (q != head)
{
printf("%d ", q->data);
q = q->prior;
if(q==head->prior) break;
}
printf("\n");
}
int main()
{
DoubleCycleList list;
InitDoubleCycleList(&list);
InsertPos(&list,2,0);
InsertPos(&list,23,1);
InsertFront(&list,11);
InsertRear(&list,99);
InsertPos(&list,24,2);
PointDoubleCycleList(&list);
DeletePos (&list,0);
DeleteRear(&list);
PointDoubleCycleList(&list);
return 0;
}