线性表的实现及其基本操作
生活随笔
收集整理的這篇文章主要介紹了
线性表的实现及其基本操作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 一、順序表
- 二、鏈表
一、順序表
初始化、賦值、插入、刪除、輸出、有序合并
#include <iostream> #include <string>using namespace std;typedef int ElemType;#define LIST_INIT_SIZE 100 #define LISTINCREMENT 10/*數組定義*/ typedef struct {ElemType* elem;//首地址int length;//當前長度int listsize;//容量 }SqList;/*初始化*/ SqList InitList_Sq(SqList& L) {L.elem = (ElemType*)malloc(LIST_INIT_SIZE * sizeof(ElemType));//存儲分配失敗if (!L.elem) {exit(-2);}//空表L.length = 0;//初始化存儲容量L.listsize = LIST_INIT_SIZE;return L;//cout << "初始化成功!"; }//創建順序表 賦值 SqList setList_Sq(SqList& L) {int length;cout << "請輸入此順序表長度:";cin >> length;L.length = length;cout << "請以此輸入順序表中元素的值:";for (int i = 0; i < L.length; i++) {cin >> L.elem[i];}return L; }/*插入*/ int ListInsert_Sq(SqList& L, int i, ElemType e) {//插入的i不合法if (i<1 || i>L.length + 1) {return -1;//cout << "插入位置不合法";}//容量不夠,增加分配if (L.length >= L.listsize) {ElemType* newbase = (ElemType*)(realloc(L.elem, (L.listsize + LISTINCREMENT) * sizeof(ElemType)));if (!newbase) {exit(-2);//cout << "存儲分配失敗";}L.elem = newbase;//新基址L.listsize += LISTINCREMENT;//增加容量}int* q = &(L.elem[i - 1]);//插入位置及之后的元素向后移for (int* p = &(L.elem[L.length - 1]); p >= q; p--) {*(p + 1) = *(p);}//插入新的元素*q = e;//表長更新++L.length;return 1;//cout << "插入成功"; }/*刪除*/ int ListDelete_Sq(SqList& L, int i, ElemType& e) {if ((i < 1) || (i > L.length)) {//cout << "刪除位置不合法";return 0;}int* p = &(L.elem[i - 1]);e = *p;int* q = L.elem + L.length - 1;for (++p; p < q; ++p) {*(p - 1) = *p;}--L.length;return 1; }//輸出順序表 void printLisq_Sq(SqList L) {for (int i = 0; i < L.length; i++) {cout << L.elem[i] << " ";} }/*順序表的合并*/ void MergeList_Sq(SqList La, SqList Lb, SqList& Lc) {ElemType* pa = La.elem;ElemType* pb = Lb.elem;Lc.listsize = Lc.length = La.length + Lb.length;ElemType* pc = Lc.elem = (ElemType*)malloc(Lc.listsize * sizeof(ElemType));if (!Lc.elem) exit(OVERFLOW);ElemType* pa_last = La.elem + La.length - 1;ElemType* pb_last = Lb.elem + Lb.length - 1;while ((pa <= pa_last) && (pb <= pb_last)){if (*pa <= *pb) *pc++ = *pa++;else *pc++ = *pb++;}while (pa <= pa_last) *pc++ = *pa++;while (pb <= pb_last) *pc++ = *pb++; }int main() {SqList l;l = InitList_Sq(l);ElemType e1, e2;cout << "原始順序表為:";printList(l);cout << "請輸入要添加的元素的值:" << endl;cin >> e1;int i, j;cout << "請輸入要添加的元素的位置:" << endl;cin >> i;if (ListInsert_Sq(l, i, e1)) {cout << "插入成功!" << endl;cout << "新的順序表為:" << endl;printList(l);}cout << "請輸入你要刪除的元素的位置:" << endl;cin >> e2;cout << "請輸入你要刪除的元素的值:" << endl;cin >> j;if (ListDelete_Sq(l, j, e2)) {cout << "刪除成功!" << endl;cout << "新的順序表為:" << endl;printList(l);}/*順序表的合并*/SqList la, lb, lc;la = setList_Sq(la);lb = setList_Sq(lb);MergeList_Sq(la, lb, lc);printLisq_Sq(lc);return 0;}二、鏈表
#include <iostream> using namespace std;#define ERROR 0 #define OK 1typedef int ElemType; typedef int Status;/*鏈表數據結構*/ typedef struct LNode {ElemType data; // Data Fieldstruct LNode* next; // Pointer (Link) Field }LNode, * LinkList;/*創建*/ LinkList CreatList_L(int n) //尾插法 {LinkList L, r, p;r = L = (LinkList)malloc(sizeof(LNode));for (int i = 1; i <= n; i++){p = (LinkList)malloc(sizeof(LNode));cout << "請輸入鏈表第" << i << "個元素的值:" << endl;cin >> p->data;r->next = p;r = p;}r->next = NULL;return L; } /*增 插入*/ Status ListInsert_L(LinkList& L, int i, ElemType e) { //在帶頭結點單鏈表第 i 個結點前插入新元素x LinkList p = L;int j = 0;while (p && j < i - 1){p = p->next;j++;} //找第 i-1個結點if (!p || j > i - 1)return ERROR;LinkList s = (LinkList)malloc(sizeof(LNode));//創建新結點,其數據為x s->data = e;s->next = p->next;p->next = s;return OK; } /*刪*/ Status ListDelete_L(LinkList& L, int i, ElemType& e) { //在單鏈表中刪除第 i 個結點LinkList p = L;int j = 0;while (p->next && j < i - 1){p = p->next; ++j;} //找第 i-1 個結點if (!(p->next) || j > i - 1)return ERROR;LinkList q = p->next;p->next = q->next; //重新鏈接e = q->data;free(q); //釋放q結點return OK; }/*改*//*查*/ int LocateElem_L(LinkList L, ElemType e) /*在單鏈表L中查找值為x的結點,找到后返回其指針,否 則返回空*/ {int i = 0;LinkList p;p = L->next;while (p != NULL && p->data != e){p = p->next; // 向后查找i++;}return i + 1; }/*輸出鏈表*/ void put(LinkList L) {while (L->next!=NULL){cout << L->next->data;L = L->next;}cout << endl;}int main() {int n;cout << "輸入要建立的鏈表中元素的個數:";cin >> n;cout << endl;LinkList L = CreatList_L(n);cout << "新建的鏈表如下: " ;put(L);int choose = 1;while (choose){ cout << "=================================" << endl;cout << "| 1.查找 |" << endl;cout << "| 2.刪除 |" << endl;cout << "| 3.插入 |" << endl;cout << "| 0.退出 |" << endl;cout << "=================================" << endl;cout << "請輸入你的選擇:";cin >> choose;switch (choose){case 1:ElemType e;cout << "請輸入要查找的元素的值為:";cin >> e;cout << "所查找元素的位置為: " << LocateElem_L(L, e) << endl;break;case 2:int i;cout << "請輸入要刪除的元素的位置: ";cin >> i;ElemType e2;cout << "請輸入要刪除的元素的值: ";cin >> e2;if (ListDelete_L(L, i, e2)) {cout << "刪除成功!" << endl;cout << "鏈表更新為:";put(L);}else{cout << "刪除失敗!" << endl;};break;case 3:int i3;cout << "請輸入要插入的位置:";cin >> i3;int e3;cout << "請輸入要插入的元素的值:";cin >> e3;if (ListInsert_L(L, i3, e3)) {cout << "插入成功!" << endl;}else {cout << "插入失敗!" << endl;}cout << "鏈表更新為:";put(L);break;case 0:break;}}return 0; } 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的线性表的实现及其基本操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 硬盘选取指南
- 下一篇: ecplise ee下载安装教程+Spr