#include <iostream>
#include <cstdio>
using namespace std;
typedef int Elem;
typedef int Status;
typedef struct DulList //我猜是 doubole list...
{
/* data */
Elem data;
struct DulList *prior;
struct DulList *next; // 前 后 指针
} DulList, *DulLinkList;
Status InitDulList(DulLinkList &L)
{
L = new DulList;
L->next = NULL;
L->prior = NULL;
return 1;
}
Status CreatDulList_H(DulLinkList &L, int num)
{
L = new DulList;
L->next = NULL;
for (int i = 1; i <= num; i++)
{
DulList *p = new DulList;
cin >> p->data;
p->next = L->next;
if (L->next) L->next->prior = p;
L->next = p;
p->prior = L;
}
return 1;
}
DulList* GetElemDulList(DulLinkList &L, int i) //这个真就 DulList* ...
{
DulList *p = L;
int j = 0;
while (p && j < i)
{
p = p->next;
++j;
}
if (i<1 || j>i)
;//这个不该这么写 其实
return p;
}
Status InsertDulList(DulLinkList &L, int i, Elem e)
{
DulList *p = L;
if (!(p = GetElemDulList(L, i))) //P的位置其实被改了 不会出现 插在头的情况
return -1;
DulList *s = new DulList;
s->data = e;
s->prior = p->prior;
p->prior->next = s;
s->next = p;
p->prior = s;
return 1;
}
Status DelDulList(DulLinkList &L, int i) //Delete
{
DulList *p = L->next;
if (!(p = GetElemDulList(L, i)))
return -1;
DulList *s = new DulList;
p->prior->next = p->next;
p->next->prior = p->prior; //前后跳着指 中间就空一格了
return 1;
}
Status ShowDulList(DulLinkList &L) {
DulList *p = L->next;
while (p)
{
cout << p->data<<" ";
p = p->next;
}
return 1;
}
int main()
{
DulLinkList L;
//1
InitDulList(L);
//1
CreatDulList_H(L, 3);
//1
InsertDulList(L, 2, 1);
//1
ShowDulList(L);
//1
DelDulList(L, 2);
//1
ShowDulList(L);
}