目錄
- 🚀前言
- 🦜任務目標
- 🌟順序表實現
- 🐍鏈表實現
🚀前言
大家好!我是 EnigmaCoder。
本文介紹線性表的實驗,使用順序表和鏈表實現通訊錄管理,包含初始化、插入、刪除、查詢、輸出。
🦜任務目標
-
線性表數據結構應用:利用順序表和鏈表實現動態存儲通訊錄信息,包括初始化、插入、刪除、查詢和遍歷功能。
-
核心算法實現:
-
動態擴容機制(
realloc
實現順序表空間擴展); -
按學號/姓名的精準查詢(基于自定義比較函數);
-
元素的插入與刪除(涉及數據移位操作)。
- 學生數據樣例:
學號(num) | 姓名(name) | 性別(sex) | 電話號碼(tel) | QQ號碼(qq) | 備注 |
---|---|---|---|---|---|
1001 | 張三 | 男 | 13812345678 | 1234567890 | 常規數據(男) |
1002 | 李四 | 女 | 13987654321 | 9876543210 | 常規數據(女) |
1003 | 王芳 | 女 | 15800001111 | 456789123 | QQ號為9位(合法) |
1010 | 劉暢 | 男 | 18666668888 | 123456789012 | QQ號為12位(最大值) |
🌟順序表實現
#include<stdio.h>
#include<stdlib.h>
#include<string.h>#define LIST_INIT_SIZE 10
#define LISTIHCREAHENT 5typedef struct {int num;char name[20];char sex[3];char tel[14];char qq[12];
}ElemType;typedef struct {ElemType* elem;int length;int listsize;
}SqList;int InitList_Sq(SqList& L) {L.elem = (ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));if (!L.elem)exit(-1);L.length = 0;L.listsize = LIST_INIT_SIZE;
}int ListInsert_Sq(SqList& L, int i, ElemType e) {ElemType* newbase;if (i<1 || i>L.length + 1) {return 0;}if (L.length == L.listsize) {newbase = (ElemType*)realloc(L.elem, (LIST_INIT_SIZE + LISTIHCREAHENT) * sizeof(ElemType));if (!L.elem)exit(-1);L.elem = newbase;L.listsize += LISTIHCREAHENT;}for (int j = L.length - 1; j >= i - 1; j--) {L.elem[j + 1] = L.elem[j];}L.elem[i - 1] = e;L.length++;return 1;
}int ListDelete_Sq(SqList& L, int i, ElemType& e) {if (i<0 || i>L.length)return 0;for (int j = i ; j <= L.length-1; j++) {L.elem[j-1] = L.elem[j];}L.length--;return 1;
}int LocateElem_Sq(SqList L, ElemType e, int(*equal)(ElemType, ElemType)) {int i = 1;while (i <= L.length && !equal(e, L.elem[i - 1])) i++;if (i <= L.length) return 1;else return 0;}int comparebynum(ElemType x, ElemType y) {return x.num == y.num ? 1 : 0;
}int comparebyname(ElemType x, ElemType y) {if (strcmp(x.name, y.name) == 0)return 1;else return 0;
}void inputOne(ElemType& x) {scanf("%d%s%s%s%s", &x.num, x.name, x.sex, x.tel, x.qq);
}void outputOne(ElemType x) {printf("%d\t%s\t%s\t%s\t%s\n", x.num, x.name, x.sex, x.tel, x.qq);
}void Output(SqList L) {for (int i = 0; i <= L.length-1; i++) {outputOne(L.elem[i]);}
}int main() {SqList L;int choice,i,y,m;ElemType e,t,x;do{printf(" 通訊錄管理系統\n");printf("====================================\n");printf(" 0:退出\n");printf(" 1:建立通訊錄\n");printf(" 2:插入\n");printf(" 3:刪除\n");printf(" 4:查詢\n");printf(" 5:輸出\n");printf("====================================\n");printf("請選擇0-5\n");scanf("%d",&choice);switch(choice){case 0: exit(1);case 1: InitList_Sq(L);break;case 2: printf("input i:");scanf("%d",&i);printf("input one record:");inputOne(e);ListInsert_Sq(L,i,e);break;case 3: printf("input i:");scanf("%d",&i);ListDelete_Sq(L,i,t);break;case 4: printf("41.姓名查詢\n42.學號查詢\n");printf("輸入選項: ");scanf("%d",&y);if(y!=41&&y!=42) {printf("請重新選擇。\n");break; }if(y==41){printf("需查詢的姓名:");scanf("%s",t.name);m=LocateElem_Sq(L,t,comparebyname); }else {printf("需查詢的學號:");scanf("%d",&t.num);m=LocateElem_Sq(L,t,comparebynum);} if(m) outputOne(L.elem[m-1]);else printf("查詢失敗。\n");break;case 5: Output(L);break; }}while(choice);free(L.elem);return 0;
}
🐍鏈表實現
#include <stdio.h>
#include <stdlib.h>
#include <string.h>typedef struct {int num;char name[20];char sex[3];char tel[14];char qq[12];
} ElemType;typedef struct LNode {ElemType data;struct LNode* next;
} LNode, * LinkList;void InitList_L(LinkList& L) {L = (LinkList)malloc(sizeof(LNode));L->next = NULL;
}int ListInsert_L(LinkList& L, int i, ElemType e) {LinkList p = L, s;int j = 0;while (p && j < i - 1) {p = p->next;j++;}if (!p || j > i - 1) return -1;s = (LinkList)malloc(sizeof(LNode));s->data = e;s->next = p->next;p->next = s;return 1;
}int ListDelete_L(LinkList& L, int i, ElemType& e) {LinkList p = L, q;int j = 0;while (p->next && j < i - 1) {p = p->next;j++;}if (!(p->next) || j > i - 1) return -1;q = p->next;p->next = q->next;e = q->data;free(q);return 1;
}LinkList LocateElem_L(LinkList L, ElemType e, int(*op)(ElemType a, ElemType b)) {LinkList p = L->next;while (p && !op(p->data, e)) {p = p->next;}return p;
}int comparebynum(ElemType x, ElemType y) {return x.num == y.num ? 1 : 0;
}int comparebyname(ElemType x, ElemType y) {return strcmp(x.name, y.name) == 0 ? 1 : 0;
}void inputOne(ElemType& x) {printf("請輸入學號、姓名、性別、電話、QQ:");scanf("%d%s%s%s%s", &x.num, x.name, x.sex, x.tel, x.qq);
}void outputOne(ElemType x) {printf("%d\t%s\t%s\t%s\t%s\n", x.num, x.name, x.sex, x.tel, x.qq);
}void outputList(LinkList L) {LinkList p = L->next;while (p) {outputOne(p->data);p = p->next;}
}int main() {LinkList L;int choice, i, y;ElemType e, t;do {printf(" 通訊錄管理系統\n");printf("====================================\n");printf(" 0:退出\n");printf(" 1:建立通訊錄\n");printf(" 2:插入\n");printf(" 3:刪除\n");printf(" 4:查詢\n");printf(" 5:輸出\n");printf("====================================\n");printf("請選擇0 - 5\n");scanf("%d", &choice);switch (choice) {case 0:exit(1);case 1:InitList_L(L);break;case 2:printf("input i:");scanf("%d", &i);inputOne(e);if (ListInsert_L(L, i, e) == -1) {printf("插入失敗\n");}break;case 3:printf("input i:");scanf("%d", &i);if (ListDelete_L(L, i, e) == -1) {printf("刪除失敗\n");}else {printf("刪除的記錄為:");outputOne(e);}break;case 4:printf("41.姓名查詢\n42.學號查詢\n");printf("輸入選項: ");scanf("%d", &y);if (y != 41 && y != 42) {printf("請重新選擇。\n");break;}if (y == 41) {printf("需查詢的姓名:");scanf("%s", t.name);LinkList result = LocateElem_L(L, t, comparebyname);if (result) {outputOne(result->data);}else {printf("查詢失敗。\n");}}else {printf("需查詢的學號:");scanf("%d", &t.num);LinkList result = LocateElem_L(L, t, comparebynum);if (result) {outputOne(result->data);}else {printf("查詢失敗。\n");}}break;case 5:outputList(L);break;}} while (choice);LinkList p = L;while (p) {LinkList temp = p;p = p->next;free(temp);}return 0;
}