資料結構基礎4_雙鏈表的實現
阿新 • • 發佈:2019-01-25
typedef int ElemType;
typedef int Status;
#define true 1
#define false 0
typedef struct Dnode{
ElemType data;
struct Dnode *pre;
struct Dnode *next;
}Dnode,*linklist;
Status Initlist(linklist &L)
{
Dnode *p;
p=(linklist)malloc(sizeof(Dnode));
if(!p)
{
return false;
}
p->pre=p->next=p;
L=p;
for(int i=5;i>0;i--)
{ int k;
scanf("%d",&k);
Dnode *q;
q=(linklist)malloc(sizeof(Dnode));
q->data=k;
q->next=L->next;
L->next->pre=q;
L->next=q;
q->pre=L;
}
return true;
}
Status Destroylist(linklist &L)
{
free(L);
L->pre=L->next=NULL;
}
Status Clearlist(linklist &L)
{
L->next=L->pre;
L->next=NULL;
}
Status Listempty(linklist L)
{
Dnode *p=L;
if(p->next==NULL)
{
return true;
}
else
return false;
}
Status Listlength(linklist L)
{
Dnode *p=L->next;
int i=1;
while(p->next!=L)
{
i++;
p=p->next;
}
return i;
}
Dnode *Getelem(linklist L,int i)
{
Dnode *q=L;
int j=1;
while(q->next!=L&&j<i)
{
j++;
q=q->next;
}
if(!q||j>i)
{
return false;
}
return q;
}
Status Listinsert(linklist &L,int i,ElemType e)
{
Dnode *p,*s;
p=Getelem(L,i);
/*
if(!(p=Getelem(L,i)))
{
return false;
}*/
s=(linklist)malloc(sizeof(Dnode));
if(!s)
{
return false;
}
s->data=e;
s->next=p->next;
p->next->pre=s;
p->next=s;
s->pre=p;
return true;
}
Status Listdelete(linklist &L,int i,ElemType &e)
{
Dnode *p;
p=Getelem(L,i);
if(!(p=Getelem(L,i)))
{
return false;
}
e=p->data;
p->pre->next=p->next;
p->next->pre=p->pre;
free(p);
return true;
}
int Listtraverse(linklist L)
{
Dnode *p=L->next;
while(p!=L)
{
printf("%d",p->data);
p=p->next;
}
}
typedef int Status;
#define true 1
#define false 0
typedef struct Dnode{
ElemType data;
struct Dnode *pre;
struct Dnode *next;
}Dnode,*linklist;
Status Initlist(linklist &L)
{
Dnode *p;
p=(linklist)malloc(sizeof(Dnode));
if(!p)
{
return false;
}
p->pre=p->next=p;
L=p;
for(int i=5;i>0;i--)
{ int k;
scanf("%d",&k);
Dnode *q;
q=(linklist)malloc(sizeof(Dnode));
q->data=k;
q->next=L->next;
L->next->pre=q;
L->next=q;
q->pre=L;
}
return true;
}
Status Destroylist(linklist &L)
{
free(L);
L->pre=L->next=NULL;
}
Status Clearlist(linklist &L)
{
L->next=L->pre;
L->next=NULL;
}
Status Listempty(linklist L)
{
Dnode *p=L;
if(p->next==NULL)
{
return true;
}
else
return false;
}
Status Listlength(linklist L)
{
Dnode *p=L->next;
int i=1;
while(p->next!=L)
{
i++;
p=p->next;
}
return i;
}
Dnode *Getelem(linklist L,int i)
{
Dnode *q=L;
int j=1;
while(q->next!=L&&j<i)
{
j++;
q=q->next;
}
if(!q||j>i)
{
return false;
}
return q;
}
Status Listinsert(linklist &L,int i,ElemType e)
{
Dnode *p,*s;
p=Getelem(L,i);
/*
if(!(p=Getelem(L,i)))
{
return false;
}*/
s=(linklist)malloc(sizeof(Dnode));
if(!s)
{
return false;
}
s->data=e;
s->next=p->next;
p->next->pre=s;
p->next=s;
s->pre=p;
return true;
}
Status Listdelete(linklist &L,int i,ElemType &e)
{
Dnode *p;
p=Getelem(L,i);
if(!(p=Getelem(L,i)))
{
return false;
}
e=p->data;
p->pre->next=p->next;
p->next->pre=p->pre;
free(p);
return true;
}
int Listtraverse(linklist L)
{
Dnode *p=L->next;
while(p!=L)
{
printf("%d",p->data);
p=p->next;
}
}
#include<iostream> #include<linklist3.h> #include<stdlib.h> #include<stdio.h> using namespace std; int main() { linklist L; Initlist(L); Listinsert(L,1,1); Listinsert(L,2,2); Listinsert(L,3,3); int n; Listdelete(L,2,n); Listtraverse(L); system("pause"); }