//双链表
#include<iostream>
#include<string>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
typedef struct student
{
int data;
struct student *next;
struct student *prior;
}node;
node *creatlist()
{
node *head, *p, *q;
int x;
head = (node*)malloc(sizeof(node));
head->next = NULL;
head->prior = NULL;
//p = head->next; 头插入法要用的;
p = head;
cout << "请输入数据,以ctrl+z表示结束" << endl;
//这是头插入法;
/*while (cin >> x)
{
q = (node*)malloc(sizeof(node));
q->data = x;
q->prior = head;
q->next = p;
head->next = q;
p = q;
}*/
//这是尾插入法;输出顺序和头插入不一样;
while (cin >> x)
{
q = (node*)malloc(sizeof(node));
q->data = x;
q->next = p->next;
q->prior = p;
p->next = q;
p = q;
}
return(head);
}
void printlist(node *head)
{
node *p;
p = head->next;
cout << "当前双链表数据为:" << endl;
while (p != NULL)
{
cout << p->data << endl;
p = p->next;
}
}
//插入;
void insert(node *r, int x)
{
node *p;
p = (node*)malloc(sizeof(node));
p->data = x;
p->next = r->next;
p->prior = r;
r->next->prior = p;
r->next = p;
}
//删除;
void deletelist(node *head, int x)
{
node *p, *q;
q = head;
p = head->next;
while (p != NULL && p->data != x)
{
q = p;
p = p->next;
}
if (p == NULL)
cout << "该链表没有这个数值" << endl;
else {
q->next = p->next;
p->next->prior = q;
free(p);
}
}
int main()
{
node *head;
head = creatlist();
insert(head, 88);
deletelist(head, 5);
printlist(head);
system("pause");
return 0;
}