C++ 双向链表插入操作详解及代码实现
#include
typedef int Status; typedef int ElemType;
typedef struct DuLNode { ElemType data; struct DuLNode *prior; struct DuLNode *next; } DuLNode, *DuLinkList;
DuLinkList GetElemP_DuL(DuLinkList L, int i) { int j; DuLinkList p; p = L->next; j = 1; while (j < i && p) { p = p->next; ++j; } if (!p || j > i) return NULL; return p; }
Status ListInsert_DuL(DuLinkList &L, int i, ElemType e) { DuLinkList s, p; if(!(p=GetElemP_DuL(L,i))) return 0; s = new DuLNode; s->data = e; s->next = p; s->prior = p->prior; p->prior->next = s; p->prior = s; return 1; }
void CreateDuList(DuLinkList &L,int n); //该函数未给出细节
void print(DuLinkList &L) { DuLinkList p; int flag=1; p = L->next; while (p) { if(flag) cout << p->data; else cout << ' '<< p->data; flag=0; p = p->next; } }
int main() {
int a;
ElemType e;
int n;
DuLinkList L, p;
cin >>n;
CreateDuList(L,n);
cin >> a;
cin >> e;
ListInsert_DuL(L, a, e);
print(L);
return 0;
}
原文地址: https://www.cveoy.top/t/topic/jUiQ 著作权归作者所有。请勿转载和采集!