#include<iostream>
using namespace std;
typedef struct node
{
int data;
struct node *next, *prior;
}Node, *DList;
//建立带头结点的双链表(最终成环状)
DList createDList()
{
int num;
Node *head, *p1, *p2;
head = new Node;
p1 = p2 = head->prior = head->next = head; //头结点
cin >> num;
while(-1 != num)
{
p1 = new Node;
p1->data = num;
p2->next = p1;
p1->next = head;
p1->prior = p2;
head->prior = p1;
p2 = p1; //不可少
cin >> num;
}
return head;
}
int getDListLength(DList p)
{
int length = 0;
Node *head = p;
while(head != p->next)
{
length++;
p = p->next;
}
return length;
}
Node *getLocation(DList p, int location)
{
//为了程序的健壮性,最好对location的合理性进行判断
//为了简便,此处不进行判断(但一定要对程序容错性有意识)
Node *head = p;
int i;
for(i = 0; head != p->next && i < location;
双链表的建立、求长、定位、插入、删除、输出和释放(带头结点且成环状)
最新推荐文章于 2022-10-18 22:34:27 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)