C++ 數據結構 線性鏈表

  • #pragma once 減少頭文件組合,降低編譯出錯的概率
  • 作用等效于?
#ifndef FUNC_H
#define FUNC_H代碼主體#endif

線性表的定義

  • 排隊問題?
  • 簡單的線性表 (物理 或者邏輯結構)
  • 1,數組
  • 2,鏈表

  • 線性表相關操作:
  • 1,線性表初始化
  • 2,線性表遍歷
  • 3,線性表插入元素
  • 4,線性表刪除元素

代碼

#include <stdio.h>
#include <stdlib.h>#define KSIZE 10
#define FALSE 0
#define TRUE 1/************************************************************************/
/* 線性表(linear list)
線性表是一個相當靈活的數據結構,它的長度可以根據需要增長和縮短,即對線性表的數據元素不僅可以進行訪問,還可以進行插入和刪除等。
抽象定義的線性表如下:
ADT:Abstract Data Type 抽象數據類型ADT LISTL:LIST簡稱,即線性表本身
i:索引
e:element簡稱,即元素
cur_:current簡稱,即當前元素
pre_:previous簡稱,即前一個元素
next_:next,即下一個元素
visit:對元素訪問的方式InitList(&L)                  :初始化線性表
DestroyList(&L)               :銷毀線性表
ClearList(&L)                 :清空線性表
ListEmpty(L)                  :線性表是否為空
ListLength(L)                 :線性表中元素個數
GetElem(L, i, &e)             :獲取線性表中指定的元素
LocateElem(L, e, compare())   :給定元素獲取第一次出現的索引位置
PriorElem(L, cur_ e, &pre_ e) :給定元素獲取其前一個元素
NextElem(L, cur_ e, &next_ e) :給定元素獲取其后一個元素
ListInsert(&L, i, e)          :將元素插入鏈表中指定位置
ListDelete(&L, i, &e)         :從鏈表中指定位置刪除元素
ListTraverse(L, visit())      :遍歷元素簡單線性表--C語言實現線性表組成類型:int數組*//************************************************************************/
/*---------------------------------------
InitList(&L);
DestroyList(&L);
ClearList(&L);
ListEmpty(L);
ListLength(L);
GetElem(L, i, &e);
LocateElem(L, e, compare());
ListInsert(&L, i, e);
ListDelete(&L, i, &e);
ListTraverse(L, visit());PriorElem(L, cur_ e, &pre_ e);
NextElem(L, cur_ e, &next_ e);
-----------------------------------------*/int count;void InitList(int *list);
void DestroyList(int *list);
void ClearList(int *list);
int ListEmpty(int *list);
int ListLength(int *list);
int GetElem(int *list, int i, int *e);
int LocateElem(int *list, int e);
int ListInsert(int *list, int i, int e);
int ListDelete(int *list, int i, int *e);
void ListTraverse(int *list);int main(void)
{int arr[KSIZE];int e = 0;InitList(arr);ListInsert(arr, 0, 5);ListInsert(arr, 0, 8);ListInsert(arr, 1, 7);ListTraverse(arr);ListDelete(arr, 0, NULL);ListTraverse(arr);GetElem(arr, 1, &e);printf("e = %d\n", e);printf("5的索引是%d\n", LocateElem(arr, 5));ClearList(arr);if(ListEmpty(arr)){printf("線性表為空\n");}else{printf("線性表不為空\n");}printf("線性表的長度為%d\n", ListLength(arr));DestroyList(arr);system("pause");return 0;
}void InitList(int *list)
{int i = 0;count = 0;for(i = 0; i < KSIZE; i++){list[i] = 0;}
}void DestroyList(int *list)
{}void ClearList(int *list)
{int i = 0;count = 0;for(i = 0; i < KSIZE; i++){list[i] = 0;}
}int ListEmpty(int *list)
{if(count == 0){return TRUE;}else{return FALSE;}
}int ListLength(int *list)
{return count;
}int GetElem(int *list, int i, int *e)
{if(i < count && i >= 0){*e = list[i];return TRUE;}else{return FALSE;}
}int LocateElem(int *list, int e)
{int i = 0;for(i = 0; i < count; i++){if(list[i] == e){return i;}}return -1;
}/************************************************************************/
/*-------------------------| 0 | 1 | 2 | 3 | 4 |   |-------------------------
*/
/************************************************************************/
int ListInsert(int *list, int i, int e)
{if(i <= count && i >= 0){/*int tempArr[KSIZE] = {0};int k = 0;int j = 0;int m = 0;for(k = 0; k < i; k++){tempArr[j++] = list[k];}tempArr[i] = e;j++;for(k = i; k < count ; k++){tempArr[j++] = list[k];}count++;for(m = 0; m < count; m++){list[m] = tempArr[m];}*/int k = 0;for(k = count-1; k >= i; k--){list[k+1] = list[k];}list[i] = e;count++;return TRUE;}else{return FALSE;}
}int ListDelete(int *list, int i, int *e)
{if(i < count && i >= 0){int k = 0;if(e != NULL){*e = list[i];}for(k = i; k < count - 1; k++){list[k] = list[k+1];}count--;return TRUE;}else{return FALSE;}
}void ListTraverse(int *list)
{int i = 0;for(i = 0; i < count; i++){printf("%d ", list[i]);}printf("\n");
}

堆申請內存

#include <stdio.h>
#include <stdlib.h>#define KSIZE 10
#define FALSE 0
#define TRUE 1/************************************************************************/
/* 線性表(linear list)
線性表是一個相當靈活的數據結構,它的長度可以根據需要增長和縮短,即對線性表的數據元素不僅可以進行訪問,還可以進行插入和刪除等。
抽象定義的線性表如下:
ADT:Abstract Data Type 抽象數據類型ADT LISTL:LIST簡稱,即線性表本身
i:索引
e:element簡稱,即元素
cur_:current簡稱,即當前元素
pre_:previous簡稱,即前一個元素
next_:next,即下一個元素
visit:對元素訪問的方式InitList(&L)                  :初始化線性表
DestroyList(&L)               :銷毀線性表
ClearList(&L)                 :清空線性表
ListEmpty(L)                  :線性表是否為空
ListLength(L)                 :線性表中元素個數
GetElem(L, i, &e)             :獲取線性表中指定的元素
LocateElem(L, e, compare())   :給定元素獲取第一次出現的索引位置
PriorElem(L, cur_ e, &pre_ e) :給定元素獲取其前一個元素
NextElem(L, cur_ e, &next_ e) :給定元素獲取其后一個元素
ListInsert(&L, i, e)          :將元素插入鏈表中指定位置
ListDelete(&L, i, &e)         :從鏈表中指定位置刪除元素
ListTraverse(L, visit())      :遍歷元素簡單線性表--C語言實現線性表組成類型:int數組*//************************************************************************/
/*---------------------------------------
InitList(&L);
DestroyList(&L);
ClearList(&L);
ListEmpty(L);
ListLength(L);
GetElem(L, i, &e);
LocateElem(L, e, compare());
ListInsert(&L, i, e);
ListDelete(&L, i, &e);
ListTraverse(L, visit());PriorElem(L, cur_ e, &pre_ e);
NextElem(L, cur_ e, &next_ e);
-----------------------------------------*/int count{};
int size{};//分配的內存空間void InitList(int *list);
void DestroyList(int *list);
void ClearList(int *list);
int ListEmpty(int *list);
int ListLength(int *list);
int GetElem(int *list, int i, int *e);
int LocateElem(int *list, int e);
int ListInsert(int *list, int i, int e);
int ListDelete(int *list, int i, int *e);
void ListTraverse(int *list);int main(void)
{int arr[KSIZE];int e = 0;InitList(arr);ListInsert(arr, 0, 5);ListInsert(arr, 0, 8);ListInsert(arr, 1, 7);ListTraverse(arr);ListDelete(arr, 0, NULL);ListTraverse(arr);GetElem(arr, 1, &e);printf("e = %d\n", e);printf("5的索引是%d\n", LocateElem(arr, 5));ClearList(arr);if(ListEmpty(arr)){printf("線性表為空\n");}else{printf("線性表不為空\n");}printf("線性表的長度為%d\n", ListLength(arr));DestroyList(arr);system("pause");return 0;
}void InitList(int *list)
{int i = 0;count = 0;size = KSIZE;//在堆上申請內存list = reinterpret_cast<int *>(malloc(sizeof (int) * KSIZE));if (list == nullptr){return ;}for(i = 0; i < KSIZE; i++){list[i] = 0;}
}void DestroyList(int *list)
{free(list);list = nullptr;
}void ClearList(int *list)
{int i = 0;count = 0;for(i = 0; i < KSIZE; i++){list[i] = 0;}
}int ListEmpty(int *list)
{if(count == 0){return TRUE;}else{return FALSE;}
}int ListLength(int *list)
{return count;
}int GetElem(int *list, int i, int *e)
{if(i < count && i >= 0){*e = list[i];return TRUE;}else{return FALSE;}
}int LocateElem(int *list, int e)
{int i = 0;for(i = 0; i < count; i++){if(list[i] == e){return i;}}return -1;
}/************************************************************************/
/*-------------------------| 0 | 1 | 2 | 3 | 4 |   |-------------------------
*/
/************************************************************************/
int ListInsert(int *list, int i, int e)
{if(i <= count && i >= 0){int k = 0;if (count == size){size = 2 * count;list = static_cast<int *>(realloc(list, size));}if (list == nullptr){return FALSE;}for(k = count-1; k >= i; k--){list[k+1] = list[k];}list[i] = e;count++;return TRUE;}else{return FALSE;}
}int ListDelete(int *list, int i, int *e)
{if(i < count && i >= 0){int k = 0;if(e != NULL){*e = list[i];}for(k = i; k < count - 1; k++){list[k] = list[k+1];}count--;return TRUE;}else{return FALSE;}
}void ListTraverse(int *list)
{int i = 0;for(i = 0; i < count; i++){printf("%d ", list[i]);}printf("\n");
}

單鏈表

代碼

#include <stdio.h>
#include <stdlib.h>/************************************************************************/
/* 要求:定義一個結構體Node,結構體由兩個整數組成,第一個是val,第二個是next定義一個Node類型的數組,數組有3個Node節點,3個Node節點的關系如下:nodeA(5, 2)  nodeB(7, -1)  nodeC(8, 1)數組首元素nodeA為頭結點,編寫函數ListTraverse遍歷此鏈表*/
/************************************************************************/typedef struct tagNode
{int val;int next;
}Node;void ListTraverse(Node *pList);int main(void)
{Node list[3] = {{5,2},{7,-1},{8,1}};ListTraverse(list);system("pause");return 0;
}void ListTraverse(Node *pList)
{int i = 0;Node *pCurrentNode = &pList[i];while(pCurrentNode->next != -1){printf("%d  ", pCurrentNode->val);i = pCurrentNode->next;pCurrentNode = &pList[i];}printf("%d  \n", pCurrentNode->val);
}

鏈表

/************************************************************************/
/* by 袁春旭                                                                  */
/************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>/************************************************************************/
/* 要求:編寫一個單鏈表,每個節點就是一條信息,每條信息包含的內容如下:姓名:name聯系方式:phone要求編寫的函數如下:InitList(Node *pHead)                               :初始化單鏈表*DestroyList(Node *pHead)                            :銷毀單鏈表*ClearList(Node *pHead)                              :清空單鏈表ListEmpty(Node *pHead)                              :判斷單鏈表是否為空ListLength(Node *pHead)                             :獲取單鏈表中節點個數GetElem(Node *pHead, int index, Node *pElem)        :獲取單鏈表中指定的節點LocateElem(Node *pHead, Node *pElem)                :給定節點獲取單鏈表中第一次出現的索引位置ListInsert(Node *pHead, int index, Node *pElem)     :將節點插入單鏈表中指定位置*ListDelete(Node *pHead, int index, Node *pElem)     :從單鏈表中指定位置刪除節點*ListTraverse(Node *pHead)                           :遍歷單鏈表中所有節點*作業:PriorElem(Node *pHead, Node *pCurElem, Node *pPreElem)  :獲取給定節點的前一個節點NextElem(Node *pHead, Node *pCurElem, Node *pNextElem)  :獲取給定節點的后一個節點打造自己的通訊錄1. 設計通訊錄菜單(如:插入信息,查找信息)2. 擴展節點信息3. 回顧文件讀寫知識提示:1. 使用記事本保存信息2. 編寫函數將信息從文件中逐一讀出并按條寫入鏈表節點3. 編寫函數將鏈表各個節點逐一寫入文件,達到保存的目的4. 注意讀寫函數的調用時機:在程序運行之初加載文件。在用戶執行退出菜單時保存文件。
*/
/************************************************************************/#define KLen 30int g_iCount = 0;typedef struct tagNode
{char name[KLen];char phone[KLen];struct tagNode *pNext;
}Node;int InitList(Node **pHead);
void ClearList(Node *pHead);
void DestroyList(Node *pHead);
int ListEmpty(Node *pHead);
int ListLength(Node *pHead);
void ListTraverse(Node *pHead);
int GetElem(Node *pHead, int index, Node *pElem);
int LocateElem(Node *pHead, Node *pElem);
int ListInsert(Node *pHead, int index, Node *pElem);
int ListDelete(Node *pHead, int index, Node *pElem);int main(void)
{//單元測試Node *pList = NULL;Node node1 = {"Jim", "1234", NULL};Node node2 = {"Merry", "4321", NULL};Node node3 = {"James", "3456", NULL};Node node4;InitList(&pList);ListInsert(pList, 0, &node1);printf("%d \n", ListLength(pList));ListInsert(pList, 1, &node2);ListInsert(pList, 1, &node3);printf("%d \n", ListEmpty(pList));printf("%d \n", ListLength(pList));ListTraverse(pList);ListDelete(pList, 2, NULL);printf("%d \n", ListLength(pList));ListTraverse(pList);DestroyList(pList);system("pause");return 0;
}int InitList(Node **pHead)
{*pHead = (Node *)malloc(sizeof(Node));if(*pHead == NULL){return 0;}(*pHead)->pNext = NULL;return 1;
}void ClearList(Node *pHead)
{Node *pCurrentNode = NULL;Node *pNextNode = NULL;if(pHead->pNext != NULL){pCurrentNode = pHead->pNext;while(pCurrentNode != NULL){pNextNode = pCurrentNode->pNext;free(pCurrentNode);pCurrentNode = pNextNode;}}
}void DestroyList(Node *pHead)
{ClearList(pHead);free(pHead);pHead = NULL;
}int ListEmpty(Node *pHead)
{/*if(pHead->pNext == NULL){return 1;}else{return 0;}*/if(g_iCount == 0){return 1;}return 0;
}int ListLength(Node *pHead)
{//int count = 0;//Node *pCurrentNode = pHead->pNext;//while(pCurrentNode != NULL)//{//	count++;//	//printf("姓名:%s   ", pCurrentNode->name);//	//printf("電話:%s   ", pCurrentNode->phone);//	pCurrentNode = pCurrentNode->pNext;//}//return count;return g_iCount;
}void ListTraverse(Node *pHead)
{Node *pCurrentNode = pHead->pNext;while(pCurrentNode != NULL){printf("姓名:%s   ", pCurrentNode->name);printf("電話:%s   ", pCurrentNode->phone);printf("\n");pCurrentNode = pCurrentNode->pNext;}printf("\n\n");
}int GetElem(Node *pHead, int index, Node *pElem)
{int count = 0;if(index < 0 || index > g_iCount){return 0;}Node *pCurrentNode = pHead;while(pCurrentNode != NULL){if(count == index){pElem = pCurrentNode;return 1;}count++;pCurrentNode = pCurrentNode->pNext;}return 1;
}int LocateElem(Node *pHead, Node *pElem)
{int index = 1;Node *pCurrentNode = pHead->pNext;while(pCurrentNode != NULL){if(!strcmp(pCurrentNode->name, pElem->name)&& !strcmp(pCurrentNode->phone, pElem->phone)){return index;}pCurrentNode = pCurrentNode->pNext;index++;}return -1;
}int ListInsert(Node *pHead, int index, Node *pElem)
{int count = 0;Node *pNode = NULL;Node *pCurrentNode = NULL;if(index < 0 || index > g_iCount){return 0;}pNode = (Node *)malloc(sizeof(Node));if(pNode == NULL){return 0;}strcpy(pNode->name, pElem->name);strcpy(pNode->phone, pElem->phone);pCurrentNode = pHead;while(pCurrentNode != NULL){if(count == index){//1. 將當前節點的next指針保存Node *pTemp = pCurrentNode->pNext;//2. 讓當前節點的next指針指向申請的內存pCurrentNode->pNext = pNode;//3. 將保存的next指針賦值給新節點的next指針pNode->pNext = pTemp;g_iCount++;return 1;}count++;pCurrentNode = pCurrentNode->pNext;}return 1;
}int ListDelete(Node *pHead, int index, Node *pElem)
{int count = 0;Node *pCurrentNode = pHead;Node *pPreNode = NULL;if(index <= 0 || index > g_iCount){return 0;}while(pCurrentNode != NULL){if(count == index){//1. 使currentNode的上一個節點指向currentNode的下一個節點pPreNode->pNext = pCurrentNode->pNext;if(pElem != NULL){//將要刪除的節點數據拷貝出來strcpy(pElem->name, pCurrentNode->name);strcpy(pElem->phone, pCurrentNode->phone);}//2. 刪除currentNode指向的節點free(pCurrentNode);g_iCount--;return 1;}count++;pPreNode = pCurrentNode;pCurrentNode = pCurrentNode->pNext;}return 1;
}

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/446617.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/446617.shtml
英文地址,請注明出處:http://en.pswp.cn/news/446617.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

英語口語 week12 Thursday

英語文章 As the pace of life quickens with technological advancements, people are occupied by all kinds of trivial matters. They seem forever on the go. There’s no difficulty in imagining that the hustle and bustle of everyday life can make us lose focus…

H.264/AVC視頻編解碼技術詳解 第一章 視頻信息與壓縮編碼

H.264/AVC視頻編解碼技術詳解系列筆記 是對 H.264/AVC視頻編解碼技術詳解 課程的學習 文章目錄人與世界的交互視頻信號的表示方法視頻壓縮編碼視頻信息為什么可以被壓縮&#xff1f;視頻壓縮編碼的分類&#xff1a;視頻壓縮編碼的基本技術人與世界的交互 從遠古時代開始&#…

英語口語 week13 Monday

英語文章 Competitions between businesses can be very aggressive in contemporary society. However, the competition for talented personnel is thought to be the key to business competition. Wise employers consider their employees as the company’s core asset…

文件系統的由來

啟蒙篇 文件的由來 磁盤上保存的是一對十六進制的數據&#xff0c;如何切分數據形成不同的文件&#xff0c;也就是如何確定一個文件的起始和終止位置&#xff1f;將相關數據打包在一起形成一個文件&#xff0c;比如從什么位置開始到什么位置結束&#xff0c;是一張圖片、一段…

c++面向對象高級編程 學習七 轉換函數

轉換函數&#xff1a;對象A和對象B之間的互相轉換。 class Fraction { public:Fraction(int num,int den1):m_numerator(num),m_denominator(den){}operator double()const{return (double)(m_numerator/m_denominator);} private:int m_numerator; //分子int m_denominator;/…

英語口語 week13 Wednesday

英語文章 Despite his extraordinary success in writing fairy tales,Hans Christian Andersen preferred to living in a way of simplicity and frugality. He often wore an old hat when he went out. One day, a well-dressed man stopped Andersen on the street, inte…

數據結構 隊列

隊列 代碼 #include <stdio.h> #include <stdlib.h>/************************************************************************/ /* 隊列結構要素&#xff1a;隊列容量 內存指針 元素個數 隊列頭 對列尾*/ /********************************************…

c++面向對象高級編程 學習八 non-explicit-one-argement-ctor

explicit&#xff08;顯式的&#xff09;&#xff1a;作用是"禁止單參數構造函數"被用于自動類型轉換 non-explicit: class Fraction { public:Fraction(int num,int den1):m_numerator(num),m_denominator(den){}Fraction operator (const Fraction& f){retur…

操作系統 IO管理

學什么&#xff1f; I/O input / output 輸入&#xff1a;鼠標 鍵盤 手柄 觸摸屏 攝像頭 MTC 掃描儀輸出&#xff1a;顯示器 打印機 耳機 音響 既是輸入也是輸出&#xff1a;光驅 網卡 磁盤 U盤硬件&#xff1a;設備如何把數據返回到PC機&#xff0c;但是不同種類的設…

英語口語小組PPT--袁隆平

文章中文版 大家好,我是第一組的xxx,現在由我來為大家分享在我眼中的袁隆平.在我看來,他值得外界對他的稱贊,因為他對中國,對世界的貢獻是有目共睹的:他研發的雜交水稻解決了人民的溫飽問題,讓無數人享受到吃飽的幸福&#xff0c;看到了生活的希望.這足以讓他青史留名. 并且他…

c++面向對象高級編程 學習九 pointer-like classes

c的class設計出來有兩種形式&#xff0c;一種像指針&#xff0c;一種像函數 智能指針里包含普通指針&#xff0c;要寫 * 和 -> 的函數 sp->method(); //sp-> 經 T* operator*() const 函數&#xff0c;得到px //由于 箭頭符號&#xff08;->&#xff09;作用下去…

const int *a和int*const a 的區別詳解

補充知識 “const int i”與“int const i”之間的區別對變量來說&#xff0c;const 關鍵字可以限定一個變量的值不允許改變&#xff0c;從而保護被修飾的東西&#xff0c;防止意外修改&#xff0c;在一定程度上可以提高程序的安全性和可靠性。 代碼 const int * int i1 10…

codeforces 133A-C語言解題報告

133A題目網址 題目解析 1.輸入字符串,如果里面包含H,Q,9,就輸出YES,否則輸出NO 代碼 #include<stdio.h> #include<stdlib.h> #include<string.h>int main() {char c[100]{0};int flag0;int i;scanf("%s",c);for(i0;i<strlen(c);i){if(c[i]H…

c++面向對象高級編程 學習十 function-like classes

本節是設計一個class&#xff0c;使它的行為像一個函數。 如果一個東西能接受小括號()操作符&#xff0c;那么這個東西就稱之為函數&#xff0c;或像函數的東西。 下圖為三個函數對()的重載&#xff0c;這三個類均為像函數的類&#xff0c;它們可接受()操作符&#xff0c; 標…

數據結構 棧

代碼 #include <stdio.h> #include <stdlib.h>/************************************************************************/ /*棧應用示例--數制轉換要求&#xff1a;輸入任意的正整數N(十進制)&#xff0c;分別輸出該整數的二進制、八進制、十六進制的結果算法…

英語口語 Week14 Monday

英語文章 Thailand, a country in Southeast Asia with an area of about 514,000 square kilometers, has been increasingly prosperous in its tourism industry in the past few decades. Its capital is Bangkok and its major languages are Thai, Chinese and English.…

c++面向對象高級編程 學習十一 類模板、函數模板、成員模板

namespace經驗談&#xff1a; 團隊中函數或類的名字可能會沖突&#xff0c;因此使用namespace進行區分。 類模板&#xff1a; template<typename T> 函數模板&#xff1a; template<class T>&#xff0c;此處class可改成typename 函數模板在使用的時候&#xff0…

操作系統面試 總結

以下文章來源于程序員cxuan &#xff0c;作者cxuan 原文鏈接什么是操作系統 操作系統是管理硬件和軟件的一種應用程序。操作系統是運行在計算機上最重要的一種軟件&#xff0c;它管理計算機的資源和進程以及所有的硬件和軟件。它為計算機硬件和軟件提供了一種中間層&#xff…

英語口語week 14 Thursday

英語文章 A couple decided to go out to celebrate their wedding anniversary, so they called a babysitter. When the babysitter arrived, the two children had already been asleep. The babysitter soon got bored and went to the kitchen where she blended some wh…

c++面向對象高級編程 學習十二 模板

模板特化&#xff1a; 模板是一種泛化的形式&#xff0c;特化是將參數類型進行指定&#xff0c;寫出特化的版本&#xff0c;當在調用下圖cout<<hash()(1000);的時候&#xff0c;由于特化中有struct hash{ }的版本&#xff0c;因此會直接調用特化部分。 模板偏特化&…