数据结构(CC++)--单链线性表

    技术2022-07-10  92

    #include<stdio.h> #include<stdlib.h> #define MaxSize 100 typedef int ElemType; typedef struct LNode { ElemType data; struct LNode * next; }LNode ,*LinkeList ; bool InitLIst(LinkeList& L) { L = (LNode*)malloc(sizeof(LNode)); if (L == NULL) return false; L->next = NULL; return true; } //头插法 bool HeadInsert(LinkeList& L) { int x; scanf("%d", &x); while (x != 9999) { LNode* s = (LNode*)malloc(sizeof(LNode)); s->data = x; s->next = L->next; L->next = s; scanf("%d", &x); } return true; } //前插法 在节点p之前插入e bool ListpreInsert(LNode * p,ElemType e) { if (p == NULL) return false; LNode* s = (LNode*)malloc(sizeof(LNode)); if (s == NULL) return false; s->next = p->next; p->next = s; s->data = p->data; p->data = e; return true;; } //按位插入 bool ListInsert(LinkeList &L, int i, ElemType e) { if (i < 1) return false; LNode* P; int j = 0; P = L; while (P != NULL && j < i - 1) { P = P->next; j++; } LNode* S = (LNode*)malloc(sizeof(LNode)); if (P == NULL) return false; S->data = e; S->next = P->next; P->next = S; return true; } //按位删除 bool ListDelet(LinkeList& L, int i, ElemType e) { if (i < 0) return false; LNode* p; p = L; int j = 0; while (p!=NULL&&j<i-1){ p = p->next; } if (p == NULL) return false; if (p->next == NULL) return false; LNode* s = p->next; e = s->data; p->next = s->next; free(s); return true; } //删除指定结点 bool DeletNode(LNode* p) { if (p == NULL) return false; if (p->next == NULL) { p = p->next; return true; } p->next = p->next->next; p->data = p->next->data; free(p->next); return true; } //按位查找 LNode* GetElem(LinkeList L, int i) { LNode* p; p = L; int j = 0; while (p != NULL && j < i ) { p = p->next; } return p; } //按值查找 LNode* LocateElem(LinkeList L, ElemType e) { LNode* p=L->next; while (p->next!=NULL&&p->data!=e){ p = p->next; } return p; } //打印线性表 bool ShowLNode(LinkeList L) { if (L == NULL) return false; LNode* p = L->next; while (p!= NULL) { printf("%d\t", p->data); p = p->next; } return true; } int main() { LNode* L; InitLIst(L); HeadInsert(L); ShowLNode(L); ListInsert(L, 2, 66); ShowLNode(L); return 1; }
    Processed: 0.010, SQL: 9