双向链表

在查找元素时，可以反向查找前缀结点

删除结点（分两步）

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

typedef int ElemType;
/*双向链表的结点,包含一个数据域，两个指针域*/
typedef struct DoubleNode{
ElemType data;
struct DoubleNode * prev; // 指向前缀结点
struct DoubleNode * next; //指向后继结点

{
cout << "申请空间失败！";
}
{
cout << "此双向链表为：";
while(temp->next != NULL)
{
temp = temp->next;
cout << temp->data << " ";
}
cout << endl;
}
/*头插法始终有问题，第49行不报错，但是调试的时候会退出，希望有好心人士帮忙解答一下*/
{
for(int i = 0;i <length;i++)
{
cout << "输入第" << i+1 <<"个元素：";
ElemType x;
cin >> x;
node->data = x;

}
}*/

{
for(int i = 0;i < length;i++)
{
cout << "输入第" << i+1 <<"个元素：";
ElemType x;
cin >> x;
node->data = x;

tail->next = node;
node ->prev = tail;
tail = node;

}
tail ->next = NULL;
}
{
int j = 1;
while(j < pos)
{
p = p->next;
j++;
}
//此时的p为前驱结点
node->data = element;

node->next = p->next;
p->next->prev = node;
p->next = node;
node->prev = p;
}
{
int j = 1;
while(j < pos)
{
p = p->next;
j++;
}
//此时的p为前驱结点
deleteNode = p->next;
deleteNode->next ->prev = p;
p->next = deleteNode->next;
free(deleteNode);

}

int main()
{
int length;
cout << "请输入链表长度：";
cin >> length;
cout << "请输入要插入的元素：";
int element;
cin >> element;
cout << "请输入要插入的位置：";
int pos;
cin >> pos;
cout << "请输入要删除元素的位置：";
cin >> pos;
return 0;
}


05-06 2万+
01-29 9211
02-27 8849
09-07 3410
04-20 164
10-17 1124
06-14 9122
04-03 1万+
02-16 172
08-30 9320
02-15 3525
02-27 1963
10-27 4137
09-16 5902