线性表-链式存储(双链表)

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
#define MAXSIZE 10000
#define SUCCESS 0
#define OVERFLOW 0
#define ERROR -1
typedef int ElemType;
typedef int Status;
typedef struct DuLNode
{
ElemType data;
struct DuLNode *prior,*next;
}DuLNode,*DuLinkList;
Status InitList(DuLinkList &L);
Status PrintElem(DuLinkList L);
Status InitElem(DuLinkList &L,ElemType e[],int len);
DuLNode *GetElem_DuL(DuLinkList L,int i);
Status ListInsert(DuLinkList &L,int i,ElemType e);
Status ListDelete(DuLinkList &L,int i);
int main(){
DuLinkList L;
ElemType elem[]={4,5,2,1,25,3,6,5},e=1;
InitList(L);
InitElem(L,elem,8);
printf("\nGetElem_DuL(L,%d)=%d",e,GetElem_DuL(L,e)->data);
ListInsert(L,3,7);
ListDelete(L,3);
return 0;
}
Status InitList(DuLinkList &L)
{
L=new DuLNode;
L->next=L;
L->prior=L;
printf("init\n");
return SUCCESS;
}
Status PrintElem(DuLinkList L)
{
DuLinkList p=L->next;
printf("\n");
while(p!=L){
printf("%d ",p->data);
p=p->next;
}
return SUCCESS;
}
Status PrintElem_p(DuLinkList L)
{
DuLinkList p=L->prior;
printf("\n");
while(p!=L){
printf("%d ",p->data);
p=p->prior;
}
return SUCCESS;
}
Status InitElem(DuLinkList &L,ElemType e[],int len){
DuLNode *p=L,*s;
int i=0;
while(len>i){
s=new DuLNode();
s->data=e[i];
s->prior=p->prior;
p->prior->next=s;
s->next=p;
p->prior=s;
i++;
}
PrintElem(L);
PrintElem_p(L);
return SUCCESS;
}
DuLNode *GetElem_DuL(DuLinkList L,int i)
{
DuLNode *p=L->next;int j=1;
while(p&&j<i){
p=p->next;
++j;
}
if(!p||j>i) exit(ERROR);
return p;
}
Status ListInsert(DuLinkList &L,int i,ElemType e)
{
DuLNode *p,*s;
if(!(p=GetElem_DuL(L,i))) exit(ERROR);
s=new DuLNode();
s->data=e;
s->prior=p->prior;
p->prior->next=s;
s->next=p;
p->prior=s;
PrintElem(L);
return SUCCESS;
}
Status ListDelete(DuLinkList &L,int i)
{
DuLNode *p;
if(!(p=GetElem_DuL(L,i))) exit(ERROR);
p->prior->next=p->next;
p->next->prior=p->prior;
delete p;
PrintElem(L);
return SUCCESS;
}

 

posted @ 2021-03-04 14:16  红藕  阅读(96)  评论(0编辑  收藏  举报