선형 표 의 양 방향 링크 저장 구조

1715 단어 데이터 구조
#include 
#include 
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef int Status;
typedef struct LNode{
    ElemType data;
    struct LNode *next;
    struct LNode *prior;
}DNode,*DLinkList;

void CreateList_L(DLinkList *L,int n);
Status GetElem_L(DLinkList L,int i,ElemType *e);
Status ListInsert_L(DLinkList L,int i,ElemType e);

int main(int argc, const char * argv[]) {
    DLinkList L,p;
    CreateList_L(&L, 5);
    int e=3;
    ListInsert_L(L,2,e);
    p=L->next;
    while (p) {
        printf("%d ",p->data);
        p=p->next;
    }
    return 0;
}

void CreateList_L(DLinkList *L,int n){
    (*L)=(DLinkList)malloc(sizeof(DNode));
    (*L)->next=NULL;
    (*L)->prior=NULL;
    DLinkList p;
    for (int i=n; i>0; --i) {
        p=(DLinkList)malloc(sizeof(DNode));
        scanf("%d",&p->data);
        p->next=(*L)->next;
        if ((*L)->next != NULL)
            (*L)->next->prior=p;
        (*L)->next=p;
        p->prior=(*L);
    }
}

Status GetElem_L(DLinkList L,int i,ElemType *e){
    DLinkList p=L->next;
    int j=1;
    while (p&&jnext;++j;
    }
    if(!p||j>i)
        return ERROR;
    *e = p->data;
    return OK;
}

Status ListInsert_L(DLinkList L,int i,ElemType e){
    DLinkList p=L,s;
    int j=0;
    while (p&&jnext;
        ++j;
    }
    if(!p||j>i-1)
        return ERROR;
    s=(DLinkList)malloc(sizeof(DNode));
    s->data=e;
    s->next=p->next;
    if (p->next != NULL)
        p->next->prior=s;
    p->next=s;
    s->prior=p;
    return OK;
}

좋은 웹페이지 즐겨찾기