// 2018-06-21 // 线性表的链式存储实现 #include<stdio.h> #include<stdlib.h> #include<time.h> #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 typedef int ElemType; typedef struct node{ ElemType data; struct node *next; }Node; // 函数说明 int InitList(Node **L); // 初始化线性表 int ListLength(Node *L); // 返回L中数据元素的个数 int ListTraverse(Node *L); // 输出L中每个数据元素 int ListInsert(Node *L,int i,ElemType e); // 在L中第i个位置插入新的元素e,L的长度加 1 int ListEmpty(Node *L); // 表为空返回TRUE,非空FALSE int ClearList(Node *L); // 清空线性表 int GetElem(Node *L,int i,ElemType *e); // 用e返回第i个数据元素的值 int LocateElem(Node *L,ElemType e); // 返回L中第一个与e满足关系的数据元素的位序 int ListDelete(Node *L,int i,ElemType *e); // 删除L中第i个数据元素,并用e返回其值,长度减 1 void CreateListHead(Node **L,int n); // 建立带表头节点的单链线性表 L(头插法) void CreateListTail(Node **L,int n); // 建立带表头节点的单链线性表 L (尾插法) // 函数现实 int InitList(Node **L) { *L = (Node *)malloc(sizeof(Node)); if(!(*L)) return ERROR; (*L)->next = NULL; return OK; } int ListLength(Node *L) { int i = 0; Node *p = L->next; while(p) { p = p->next; i++; } return i; } int ListTraverse(Node *L) { Node *p = L->next; while(p) { printf("%d ",p->data); p = p->next; } printf(" "); return OK; } int ListInsert(Node *L,int i,ElemType e) { int j; Node *p,*s; if(i < 1) return ERROR; p = L; j = 1; while(p && j < i) { p = p->next; j++; } if(!p) return ERROR; s = (Node *)malloc(sizeof(Node)); s->data = e; s->next = p->next; p->next = s; return OK; } int ListEmpty(Node *L) { if(L->next) return FALSE; return TRUE; } int ClearList(Node *L) { Node *p,*q; p = L->next; while(p) { q = p->next; free(p); p = q; } L->next = NULL; return OK; } int GetElem(Node *L,int i,ElemType *e) { int j; Node *p; p = L->next; j = 1; if(i < 1) return ERROR; while(p && j<i) { p = p->next; j++; } if(!p) return ERROR; *e = p->data; return OK; } int LocateElem(Node *L,ElemType e) { int i = 0; Node *p = L->next; while(p) { i++; if(p->data == e) return i; else p = p->next; } return 0; } int ListDelete(Node *L,int i,ElemType *e) { int j; Node *p,*q; if(i < 1) return ERROR; p = L; j = 1; while(p->next && j < i) { p = p->next; j++; } if(!(p->next)) return ERROR; q = p->next; *e = q->data; p->next = q->next; free(q); return OK; } void CreateListHead(Node **L,int n) { Node *p; int i; *L = (Node *)malloc(sizeof(Node)); (*L)->next = NULL; for(i = 0;i <= n;i++) { p = (Node *)malloc(sizeof(Node)); p->data = i; p->next = (*L)->next; (*L)->next = p; } } void CreateListTail(Node **L,int n) { Node *p,*r; int i; *L = (Node *)malloc(sizeof(Node)); r = *L; for(i = 0;i <= n;i++) { p = (Node *)malloc(sizeof(Node)); p->data = i; r->next = p; r = p; } r->next = NULL; } int main() { Node *L; int i,j,k; ElemType e; InitList(&L); printf("初始化L后:ListLength(L) = %d ",ListLength(L)); for(j = 1;j <= 5;j++) i = ListInsert(L,1,j); printf("在L的表头依次插入1~5后:L.data = "); ListTraverse(L); printf("ListLength(L) = %d ",ListLength(L)); i = ListEmpty(L); printf("L是否空: i = %d (1:是 0:否) ",i); i = ClearList(L); printf("清空L后:ListLength = %d ",ListLength(L)); i = ListEmpty(L); printf("L是否空: i = %d (1:是 0:否) ",i); for(j = 1;j <=10;j++) ListInsert(L,j,j); printf("在L表尾依次插入1~10后,L.data = "); ListTraverse(L); printf("L.length = %d ",ListLength(L)); ListInsert(L,1,0); printf("在L的表头插入0后,L.data = "); ListTraverse(L); printf("L.length = %d ",ListLength(L)); GetElem(L,7,&e); printf("第七个元素的值为:%d ",e); for(j = 3;j <= 4;j++) { k = LocateElem(L,j); if(k) printf("值为%d的元素的位序为%d ",j,k); else printf("没有值为%d的元素 ",j); } printf(" "); k = ListLength(L); for(j = k+1;j >= k;j--) { i = ListDelete(L,j,&e); if(i == ERROR) printf("删除第%d个元素失败 ",j); else printf("删除第%d个元素的值为%d ",j,e); } printf("依次输出L的元素:"); ListTraverse(L); printf("L.Length = %d ",ListLength(L)); j = 5; ListDelete(L,j,&e); printf("删除的第5个元素的值为:%d ",e); printf("依次输出L元素:"); ListTraverse(L); printf(" "); i = ClearList(L); printf("清空元素后L.length = %d ",ListLength(L)); CreateListHead(&L,10); printf("整体创建L的元素(头插法):L.Data = "); ListTraverse(L); printf(" "); i = ClearList(L); printf("清除后的L.length = %d ",ListLength(L)); CreateListTail(&L,10); printf("整体创建L的元素(尾插法):L.Data = "); ListTraverse(L); return 0; }