#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
typedef int ElemType;
typedef struct DNode{
ElemType data;
struct DNode *prior,*next;
}DNode,*DLinklist;
void InitList(DLinklist &L){ //双链表初始化
L=(DLinklist)malloc(sizeof(DNode));
L->prior=NULL;
L->next=NULL;
}
DLinklist Insert(DLinklist &L){ //创建双链表
/*DNode *s=(DNode *)malloc(sizeof(DNode));
s->data=x; //传入数据
s->next=p->next; //1
p->next->prior=s; //2
p->next=s; //3
s->prior=p;*/ //4 //步骤1,2必须在3前 (不然p后继指针被覆盖)
InitList(L);
int x;
cin>>x; //相当于scanf函数
while(x!=9999){
DNode *s = (DNode *)malloc(sizeof(DNode));
s->data = x;
if(L->next == NULL){ //当表为空时,插入的第一个结点就是头节点
s->next = NULL; //非循环
//s->next=L; // 循环
s->prior = L;
L->next = s;
// L->prior=s;// 赋予首节点循环
}else{
s->next = L->next;
L->next->prior = s;
s->prior =L;
L->next = s;
}
cin>>x;
}
return L;
}
DNode *GetElem(DLinklist &L,int i){
int j=1;
DNode *p=L->next;
if(i==0) return L;
if (i<0) return NULL;
while (p && j<i)
{
p=p->next;
j++;
}
return p;
}
int Length(DLinklist &L){
DNode *p=L->next;
int len=0;
while (p)
{
p=p->next;
len++;
}
return len;
}
void Delete(DLinklist &L,int i){
if(i<1 || i>Length(L)){
cout<<"输入有误,删除失误\n";
}
DNode *p=GetElem(L,i-1);
DNode *q;
q=p->next;
p->next=q->next; //1 步骤顺序不影响代码实现
q->next->prior=p; //2
free(q);
}
void Print(DLinklist &L){
DNode *p=L->next;
while(p){ //非循环双链表判断
//while (p!=L){ //循环双链表的判断 (循环双链表尽可正常输出)
cout<<p->data<<" ";
p=p->next;
}
}
int main(){
int i;
cout<<"请输入表内数据\n";
DLinklist L=Insert(L); // 头插法建表
cout<<"L: ";
Print(L);
cout<<"输入删除第几个结点\n";
cin>>i;
Delete(L,i);
cout<<"删除后表内元素为:\n";
Print(L);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
typedef int ElemType;
typedef struct DNode{
ElemType data;
struct DNode *prior,*next;
}DNode,*DLinklist;
void InitList(DLinklist &L){ //双链表初始化
L=(DLinklist)malloc(sizeof(DNode));
L->prior=NULL;
L->next=NULL;
}
DLinklist Insert(DLinklist &L){ //创建双链表
/*DNode *s=(DNode *)malloc(sizeof(DNode));
s->data=x; //传入数据
s->next=p->next; //1
p->next->prior=s; //2
p->next=s; //3
s->prior=p;*/ //4 //步骤1,2必须在3前 (不然p后继指针被覆盖)
InitList(L);
int x;
cin>>x; //相当于scanf函数
while(x!=9999){
DNode *s = (DNode *)malloc(sizeof(DNode));
s->data = x;
if(L->next == NULL){ //当表为空时,插入的第一个结点就是头节点
s->next = NULL;
s->prior = L;
L->next = s;
}else{
s->next = L->next;
L->next->prior = s;
s->prior = L;
L->next = s;
}
cin>>x;
}
return L;
}
DNode *GetElem(DLinklist &L,int i){
int j=1;
DNode *p=L->next;
if(i==0) return L;
if (i<0) return NULL;
while (p && j<i)
{
p=p->next;
j++;
}
return p;
}
int Length(DLinklist &L){
DNode *p=L->next;
int len=0;
while (p)
{
p=p->next;
len++;
}
return len;
}
void Delete(DLinklist &L,int i){
if(i<1 || i>Length(L)){
cout<<"输入有误,删除失误\n";
}
DNode *p=GetElem(L,i-1);
DNode *q;
q=p->next;
p->next=q->next; //1 步骤顺序不影响代码实现
q->next->prior=p; //2
free(q);
}
void Print(DLinklist &L){
DNode *p=L->next;
while(p){
cout<<p->data<<" ";
p=p->next;
}
}
int main(){
int i;
cout<<"请输入表内数据\n";
DLinklist L=Insert(L); // 头插法建表
cout<<"L: ";
Print(L);
cout<<"输入删除第几个结点\n";
cin>>i;
Delete(L,i);
cout<<"删除后表内元素为:\n";
Print(L);
return 0;
}