出了点问题
//双向链表的插入,创建,删除,长度。
#include <bits/stdc++.h>
using namespace std;
#define ok 1
#define error 0
typedef struct dnode
{
int data;
struct dnode *last;
struct dnode *next;
} dnode, *lilinst;
int chushihua(lilinst &l)
{
l = new dnode;
l->last = NULL;
l->next = NULL;
return ok;
}
int length(lilinst &l)
{
if (l == l->last || l == l->next)
{
return error;
}
dnode *p;
p = p->last;
int i = 0;
while (p != l)
{
i++;
p = p->last;
}
return i;
}
bool insert(lilinst &l, int i, int e)
{
//插入新的元素在第i个节点
dnode *p, *s;
p = l->next;
int j = 0;
while (p != l && j < i - 1)
{
j++;
p = p->next;
}
if (p == l || j > i - 1)
{
return false;
}
s = new dnode;
s->data = e;
s->next = p->next;
p->next->last = s;
p->next = s;
s->last = p;
return true;
}
int deletep(lilinst &l, int i)
{
//删除第i个元素
dnode *p;
p = l->next;
int j = 1;
while (p != l && j < i)
{
p = p->next;
j++;
}
if (p == l || j > i)
{
return false;
}
p->last->next = p->next;
p->next->last = p->last;
delete p;
return true;
}
void clean(lilinst &l)
{
dnode *p, *s;
s = p = l->next;
while (p != l) {
p = p->next;
delete s;
s = p;
}
l->last = l->next = l;
}
void printfl(lilinst &l)
{
dnode *p;
p = l->next;
cout << "这个链表:" << endl;
while (p != l)
{
cout << p->data << " ";
p = p->next;
}
}
void createl(lilinst &l, int n)
{
//后插法,正序输入n个元素的值。
dnode *r, *p;
l = new dnode;
r = l;
cout << "请输入" << n << "个数" << endl;
for (int i = 0; i < n; i++)
{
p = new dnode;
cin >> p->data;
p->next = NULL;
r->next = p;
r = p;
p->last = l->last;
l->last = p;
}
}
int main ()
{
lilinst xh;
int n;
int el = 3;
cout << "请输入n个数" << endl;
cin >> n;
chushihua(xh);
createl(xh, n);
printfl(xh);
insert(xh, 2, el);
printfl(xh);
deletep(xh, 4);
printfl(xh);
}