数据结构课程设计(C语言实现)
生活随笔
收集整理的這篇文章主要介紹了
数据结构课程设计(C语言实现)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
僅供自己學習使用
一、設計任務
??設計一個應用程序(C/C++),利用多級菜單實現單鏈表、棧、隊列、二叉樹及圖五種結構的基本操作及應用。具體內容包括:
①創建
②插入
③刪除
④查找
⑤應用
注:利用基本操作(可擴展)實現單鏈表的應用,如一元多項式運算、通訊錄設計等。
①進棧
②出棧
③取棧頂元素
④應用
注:利用基本操作(可擴展)實現棧的應用,如表達式求值、深度優先遍歷等。
①入列
②出列
③取隊頭元素
④取隊尾元素
⑤應用
注:利用基本操作(可擴展)實現隊列的應用,如酒店客房分配、廣度優先遍歷等。
①創建
②遍歷(先序、中序、后序)
③求結點個數
④求樹的深度
⑤查找雙親
⑥查找兄弟(左/右)
⑦查找孩子(左/右)
⑧應用
注:利用基本操作(可擴展)實現二叉樹的應用,如二叉排序樹、Huffman編碼等。
①創建(鄰接矩陣/鄰接表)
②遍歷(深度/廣度)
③定位
④找第一個鄰接點
⑤找下一個鄰接點
⑥插入(點/邊)
⑦刪除(點/邊)
⑧應用
注:利用圖的基本操作(可擴展)實現圖的應用,如拓撲排序、關鍵路徑等。
二、運行效果圖
三、部分源代碼
1. bitree.h
#include<stdio.h> #include<stdlib.h> #include"user.h"//二叉樹結構體 typedef struct BiTNode {char data;struct BiTNode* lchild, * rchild; } BiTNode, * BiTree;//初始化 Status InitBiTree(BiTree& T) {T = (BiTree)malloc(sizeof(BiTNode));if (!T) {printf("內存分配失敗。\a\n\n");}T->lchild = T->rchild = NULL;return OK; }//先序創建 void CreateBiTree(BiTree& T) {char ch;printf("請輸入結點數據(先序):");scanf_s("%c", &ch);getchar();if (ch == '*')T = NULL;else {T = new BiTNode;T->data = ch;CreateBiTree(T->lchild);CreateBiTree(T->rchild);} } //先序遍歷 void PreOrderTraverse1(BiTree T) {if (T) {printf("%c", T->data);PreOrderTraverse1(T->lchild);PreOrderTraverse1(T->rchild);} } //中序遍歷 void InOrderTraverse2(BiTree T) {if (T) {InOrderTraverse2(T->lchild);printf("%c", T->data);InOrderTraverse2(T->rchild);} } //后序遍歷 void PostOrderTraverse3(BiTree T) {if (T) {PostOrderTraverse3(T->lchild);PostOrderTraverse3(T->rchild);printf("%c", T->data);} } //二叉樹深度 int Depth(BiTree T) {int n, m;if (T == NULL)return 0;else {m = Depth(T->lchild);n = Depth(T->rchild);if (m > n)return(m + 1);elsereturn(n + 1);} } //葉子結點個數 int LeafCount(BiTree T) {if (T == NULL){return 0;}if ((T->lchild == NULL) && (T->rchild == NULL)){return 1;}return LeafCount(T->lchild) + LeafCount(T->rchild); } //查找雙親 Status Parent(BiTree T, char e)// 初始條件:二叉樹T存在,e是T中某個結點 // 操作結果:若e是T的非根結點,則返回它的雙親;否則返回“空” {Status m;if (T == NULL) // 空樹return 0;if ((T->lchild && T->lchild->data == e) || (T->rchild && T->rchild->data == e)) {printf("結點的雙親數據是:%c\n\n",T->data);return OK;}m = Parent(T->lchild, e);if (m == 0) m = Parent(T->rchild, e);return m; } //查找兄弟 Status Brother(BiTree T, char e) {Status m;if (T == NULL) // 空樹return 0;if (T->lchild && T->lchild->data == e) {printf("結點存在右兄弟,且右兄弟數據是:%c\n\n", T->rchild->data);return OK;}if (T->rchild && T->rchild->data == e) {printf("結點存在左兄弟,且左兄弟數據是:%c\n\n", T->lchild->data);return OK;}m = Brother(T->lchild, e);if (m == 0) m = Brother(T->rchild, e);return m; }// 查找孩子 Status Child(BiTree T, char e) {Status m;if (T == NULL) {return 0;}if (T->data == e) {if (T->lchild && !T->rchild) {printf("存在左孩子,且左孩子數據是:%c\n\n",T->lchild->data);return OK;}if (T->rchild && !T->lchild) {printf("存在左孩子,且左孩子數據是:%c\n\n", T->rchild->data);return OK;}if (T->rchild && T->lchild) {printf("左孩子數據是:%c,右孩子數據是:%c\n\n",T->lchild->data,T->rchild->data);return OK;}}m = Child(T->lchild, e);if (m == 0) m = Child(T->rchild, e);return m; }// 二叉樹的應用///*********** 測試 *************/ //int main(void) { // BiTree root; // CreateBiTree(root); // printf("\n"); // // //printf("先序遍歷結果是:"); // // //PreOrderTraverse1(root); // // //printf("\n"); // // //printf("中序遍歷結果是:"); // // //InOrderTraverse2(root); // // //printf("\n"); // // //printf("后序遍歷結果是:"); // // //PostOrderTraverse3(root); // // //printf("\n"); // // printf("您所創建的二叉樹深度是:%d\n\n", Depth(root)); // // printf("您所創建的二叉樹的葉子結點個數是:%d\n\n", LeafCount(root)); // // char data; // // printf("請您輸入要查找雙親的結點信息:"); // // scanf_s("%c", &data); // // if (!Parent(root, data)) { // // printf("不存在該結點或該結點是根結點。\a\n\n"); // // } // char data; // printf("請您輸入要查找兄弟的結點信息:"); // scanf_s("%c", &data); // if (!Child(root, data)) { // printf("不存在該結點或該結點是葉子結點。\a\n\n"); // } // return 0; //} }2. LinkList.h
#include<stdio.h> #include<stdlib.h> #include"user.h"typedef struct LNode {int data;struct LNode* next; }LNode, *Linklist;Status ListInit(Linklist& L) { // 初始化L = (LNode*)malloc(sizeof(LNode));if (!L) {printf("內存分配失敗!\a\n");exit(ERROR);}L->next = NULL;L->data = 0; //頭節點的數據存放結點個數printf("單鏈表初始化成功!\n\n");return OK; }Status ListCreate(Linklist& L) { // 創建(尾插法)int i; // i是循環變量int n;//n用于記錄元素個數printf("請輸入元素個數:");scanf_s("%d", &n);for (i = 0; i < n; i++) {LNode* newNode = (LNode*)malloc(sizeof(LNode));if (!newNode) {printf("內存分配失敗!\a\n");exit(ERROR);}printf("\t請輸入第%d個元素的數據:", n - i);scanf_s("%d", &newNode->data);newNode->next = L->next;L->next = newNode;L->data++;}printf("單鏈表創建成功!\n\n");return OK; }Status ListInsert(Linklist L) { // 插入LNode* sign = L;int j = 0;int location;printf("請輸入要插入的位置:");scanf_s("%d", &location);while (sign && j < location - 1) {sign = sign->next;++j;}if (!sign || j > location - 1) {printf("位置不合法!\a\n");exit(ERROR);}LNode* newNode = (LNode*)malloc(sizeof(LNode));if (!newNode) {printf("內存分配失敗!\a\n");exit(ERROR);}printf("請輸入要插入的數據:");scanf_s("%d", &newNode->data);newNode->next = sign->next;sign->next = newNode;L->data++;printf("插入成功!\n\n");return OK; }int ListDelete(Linklist L) { //刪除int location;printf("請輸入要刪除元素所在的位置:");scanf_s("%d", &location);int j = 0;LNode* sign = L;while (sign->next && j < location - 1) {sign = sign->next;++j;}if (!sign || j > location - 1) {printf("位置不合法!\a\n");exit(ERROR);}LNode* temp = sign->next;sign->next = temp->next;int data = temp->data;free(temp);printf("刪除成功!\n\n");return data; }Status ListShow(Linklist& L) { //打印LNode* sign = L->next;int i = 1;printf("現在開始打印單鏈表中的數據:\n");while (sign) {printf("\t第%d個數據是:%d\n", i++, sign->data);sign = sign->next;}printf("數據打印完畢!\n\n");return OK; }Status ListLocate(Linklist& L) { //查找int data;int location = 1; //記錄元素位置printf("請輸入您要查找的元素:");scanf_s("%d", &data);LNode* sign = L->next;while (sign && sign->data != data) {sign = sign->next;location++;}if (!sign) {printf("當前鏈表中不存在您要查找的元素。\n\n");return FALSE;}else {printf("元素所在位置是第%d個。\n\n", location);return OK;} }// 單鏈表的應用,多項式相加減 typedef struct pnode {int coef; //系數int exp; // 指數struct pnode* next; // 后繼指針 }PolyNode;PolyNode* aHead, * bHead;void DispPoly(PolyNode* head) {PolyNode* p;p = head;while (p != NULL){if (p->coef > 0 && p != head){printf("+");}if (p->exp == 0){printf("%d", p->coef);}else if (p->exp == 1){printf("%dx", p->coef);}else{printf("%dx^%d", p->coef, p->exp);}p = p->next;}printf("\n"); }void createList(PolyNode*& head) {int m; // m代表多項式的系數while (1){printf("\n\t\t請輸入多項式的項數:");scanf_s("%d", &m);if (m != 0){break;}else{printf("\n\t\t不允許輸入 0 項!請重新輸入!\n");}}PolyNode* p = NULL, * s = NULL;for (int i = 0; i < m; i++){p = (PolyNode*)malloc(sizeof(PolyNode));printf("\n\t\t第%2d項 系數:", i + 1);scanf_s("%d", &p->coef);printf("\t\t\t指數:");scanf_s("%d", &p->exp);if (head == NULL){head = p;}else{s->next = p;}s = p;}s->next = NULL; }void Add(PolyNode*& ahead, PolyNode*& bhead) {PolyNode* p = NULL, * q = NULL, * s = NULL;q = aHead;while (q != NULL){p = q;q = q->next;}p->next = bHead;printf("\n\n\t\t連接:");DispPoly(aHead);q = aHead;while (q != NULL){p = q;s = q;p = p->next;while (p != NULL){if (p->exp == q->exp){q->coef = q->coef + p->coef;s->next = p->next;free(p);p = s;}s = p;p = p->next;}q = q->next;}printf("\n\t\t相加得多項式:");DispPoly(aHead); }void Sub(PolyNode*& ahead, PolyNode*& bhead) {PolyNode* p = NULL, * q = NULL, * s = NULL;q = aHead;while (q != NULL){p = q;if (q->coef > 0) q->coef = -(q->coef);q = q->next;}p->next = bHead;printf("\n\n\t\t連接:");DispPoly(aHead);q = aHead;while (q != NULL){p = q;s = q;p = p->next;while (p != NULL){if (p->exp == q->exp){q->coef = q->coef + p->coef;s->next = p->next;free(p);p = s;}s = p; p = p->next;}q = q->next;}printf("\n\t\t相減得多項式:");DispPoly(aHead); }/*********** 測試 *************/ //int main(void) { // Linklist L = NULL; // ListInit(L); //初始化單鏈表 // ListCreate(L); //創建單鏈表 // // ListShow(L); //打印單鏈表 // //ListInsert(L); //插入到單鏈表 // //printf("此時單鏈表中一共有%d個元素.\n", L->data); // //ListShow(L); //打印單鏈表 // //ListDelete(L); // //ListShow(L); //打印單鏈表 // ListLocate(L); //}3. map.h
#include <iostream> using namespace std; #define MAX 100 //最大頂點數 bool visited[MAX]; //標志數組,用于標記頂點是否被訪問過//邊的存儲結構 typedef struct BNode {int pointPosite; //該邊所指向頂點的位置struct BNode* nextB; //指向下一條邊的指針int info; //和邊相關的信息 }BNode;//頂點的存儲結構 typedef struct DNode {string data; //存放頂點信息BNode* firstB; //指針指向第一條依附該頂點的邊 }DNode, AdjList[MAX]; //AdjList表領接表類型//鄰接表的存儲結構 typedef struct {AdjList vertices; //頂點向量int DNum, BNum; //圖的當前頂點數和邊數 }TGraph;//順序隊列的存儲表示 typedef struct {string* base; //存儲空間的基地址int front; //頭指針int rear; //尾指針 }SqQueue;//隊列的初始化 void InitQueue(SqQueue& Q) {//構造一個空隊列Q.base = new string[MAX];if (!Q.base){cout << "內存分配失敗" << endl;}Q.front = Q.rear = 0; }// 入隊 void EnQueue(SqQueue& Q, string e) {if ((Q.rear + 1) % MAX == Q.front){cout << "隊列已滿" << endl;}Q.base[Q.rear] = e;Q.rear = (Q.rear + 1) % MAX; }// 出隊 string DeQueue(SqQueue& Q, string& e) {if (Q.front == Q.rear){cout << "隊列為空" << endl;}e = Q.base[Q.front];Q.front = (Q.front + 1) % MAX;return e; }// 判斷隊列是否尾空 bool Empty(SqQueue Q) {if (Q.front == Q.rear){return true;}return false; }// 找所給頂點在圖中的序號 int LocateVex(TGraph G, string v) {for (int i = 0; i < G.DNum; ++i){if (v == G.vertices[i].data){return i;}}return -1; }// 鄰接表創建無向圖 void CreateUDG(TGraph& G) //無向圖UDG {string v1, v2;cout << "請輸入所要創建圖的總頂點數和總邊數:";cin >> G.DNum >> G.BNum; //輸入總頂點數和總邊數for (int i = 0; i < G.DNum; ++i) //輸入各點,構造表頭結點表{cout << "請輸入第" << i + 1 << "個頂點的值:";cin >> G.vertices[i].data; //輸入頂點值G.vertices[i].firstB = NULL;//初始化表頭結點指針域為空}for (int k = 0; k < G.BNum; ++k) //輸入各邊,構造領接表{BNode* p1, * p2;cout << "請輸入第" << k + 1 << "條邊的信息:";cin >> v1 >> v2; //輸入一條邊依附的兩個頂點int m = LocateVex(G, v1); int n = LocateVex(G, v2); //得到v1 v2在G.vertices中的序號p1 = new BNode; //生成一個新的邊結點*p1p1->pointPosite = n; //鄰接點序號為np1->nextB = G.vertices[m].firstB; //頭插法G.vertices[m].firstB = p1;p2 = new BNode;p2->pointPosite = m;p2->nextB = G.vertices[n].firstB;G.vertices[n].firstB = p2;}cout << "無向圖創建成功。\n\n"; }// 打印圖的鄰接表 void UDGprint(TGraph G) {BNode* p;for (int i = 0; i < G.DNum; ++i){cout << G.vertices[i].data;p = G.vertices[i].firstB;while (p){cout << ": ";cout << p->pointPosite;p = p->nextB;}cout << endl;} }// 深度優先遍歷 void deepTravel(TGraph G, string v) {int b, w;BNode* p;cout << v << " ";b = LocateVex(G, v);visited[b] = true; //訪問第b個頂點,標志為truep = G.vertices[b].firstB; //p指向v的邊鏈表的第一個邊結點while (p != NULL) //如果p不為空{w = p->pointPosite; //w為v的第一個邊結點if (!visited[w]) //若w未被訪問 則繼續遞歸{string a;for (int i = 0; i < G.DNum; ++i){if (w == i){a = G.vertices[i].data;}}deepTravel(G, a);}p = p->nextB; //否則p指向下一個v的邊結點} }// 廣度優先遍歷 void breadthTravel(TGraph G, string v) {BNode* p;int l;string e;int b = LocateVex(G, v);SqQueue Q;InitQueue(Q);for (int i = 0; i < G.DNum; i++)visited[i] = false;if (!visited[b]){visited[b] = true;cout << v << " ";EnQueue(Q, v);while (!Empty(Q)){string s = DeQueue(Q, e);int u = LocateVex(G, s);p = G.vertices[u].firstB;while (p){l = p->pointPosite;if (!visited[l]){visited[l] = true;cout << G.vertices[l].data << " ";EnQueue(Q, G.vertices[l].data);}p = p->nextB;}}} }// 查找第一個鄰接點 int FirstAdjVex(TGraph G, string v) {BNode* p;int v1;v1 = LocateVex(G, v);//v1為頂點v在圖G中的序號p = G.vertices[v1].firstB;if (p){return p->pointPosite;}else{return -1;} }// 查找下一個鄰接點 int NextAdjVex(TGraph G, string v, string w) {BNode* p;int v1, v2;v1 = LocateVex(G, v);//v1為頂點v在圖G中的序號v2 = LocateVex(G, w);//v2為頂點v在圖G中的序號p = G.vertices[v1].firstB;for (; p->nextB && p->pointPosite != v2; p = p->nextB);if (p->nextB && p->pointPosite == v2){return p->nextB->pointPosite;}else{return -1;} }// 插入結點 void InsertNode(TGraph& G, string data) {G.DNum++;G.vertices[G.DNum - 1].data = data;G.vertices[G.DNum - 1].firstB = NULL;printf("插入成功。\n\n"); }// 刪除結點 void DeleteNode(TGraph& G, string data) {int c = LocateVex(G, data);BNode* p, * q;for (int i = 0; i < G.DNum; ++i){p = G.vertices[i].firstB;q = p;if (G.vertices[i].firstB->pointPosite)if (c > G.vertices[i].firstB->pointPosite){q = p;p = p->nextB;}else if (G.vertices[i].firstB->pointPosite == c){G.vertices[i].firstB = p->nextB;q = G.vertices[i].firstB;p = p->nextB;while (p){if (c > p->pointPosite){q = p;p = p->nextB;}else{p->pointPosite -= 1;q = p;p = p->nextB;}}continue;}else{G.vertices[i].firstB->pointPosite -= 1;q = p;p = p->nextB;}while (p){if (c > p->pointPosite){p = p->nextB;}else if (c == p->pointPosite){q->nextB = p->nextB;p = p->nextB;while (p){if (c > p->pointPosite){p = p->nextB;}else{p->pointPosite -= 1;p = p->nextB;}}continue;}else{p->pointPosite -= 1;q = p;p = p->nextB;}}}for (int j = 0; j < G.DNum; j++){if (G.vertices[j].data == data){for (; j < G.DNum; j++){G.vertices[j] = G.vertices[j + 1];}G.DNum -= 1;break;}} }/**********應用**********/ // 判斷圖的聯通性 bool Judge(TGraph G) {for (int v = 0; v < G.DNum; v++){visited[v] = false;}deepTravel(G, G.vertices[0].data); //從任意一點遍歷,這里從下標為0的點開始for (int v = 0; v < G.DNum; v++){if (!visited[v]){return false;}}return true; }/**************測試***************/ //int main() //{ // string ch; // TGraph G; // CreateUDG(G); // cout << "建立的鄰接表如下: " << endl; // UDGprint(G); // cout << "請輸入遍歷開始的結點:"; // cin >> ch; // cout << "深度遍歷序列為: "; // deepTravel(G, ch); // cout << "\n廣度遍歷序列為: "; // breadthTravel(G, ch); // return 0; //}完整下載鏈接
數據結構課程設計
總結
以上是生活随笔為你收集整理的数据结构课程设计(C语言实现)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Keil5 C51版(51单片机编程软件
- 下一篇: LeetCode 106~110