C 實現線性表(鍊錶描述)

2021-09-26 19:20:17 字數 1609 閱讀 7359

本文使用c++實現了乙個線性表(陣列描述)。該程式由三個檔案構成,第一部分是標頭檔案,標頭檔案定義了乙個鍊錶的節點的結構體,同時在該結構體的基礎上定義了乙個線性表類,該抽象類中定義了絕大部分線性表的成員函式,其中包括:

確定線性表是否為空

確定線性表中資料的數目

新增乙個資料

按乙個給定索引查詢其對應的元素

按資料刪除乙個節點

遍歷輸出(反向輸出)該線性表

格式化該線性表

排序(反向排序)該線性表

第二部分是測試**。整個程式使用xcode編譯。其中,標頭檔案原始碼如下:

//

// node_list.hpp

// node_list

//// created by lq on 2019/9/2.

//#ifndef node_list_hpp

#define node_list_hpp

#include using namespace std;

struct node

;class node_chain

node_chain(const node_chain &s)

tail->next = nullptr;

}~node_chain()

void add_node(int data)

void remove_node(int data)

temp = temp->next;}}

}void remove_all()

head->next = tail;

tail->last = head;

length = 0;

}void show_list()

coutwhile(temp != head)

coutfor(int i=0;inext;

for(int j=i+1;jdata < temp2->data)

temp2=temp2->next;

}temp1 = temp1->next;}}

}int size()

bool empty_check()

else

}int find(int index)

coutfor(int i=0;inext;

for(int j=i+1;jdata > temp2->data)

temp2=temp2->next;

}temp1 = temp1->next;}}

}};#endif /* node_list_hpp */

測試**如下:

//

// main.cpp

// node_list

//// created by lq on 2019/9/2.

//#include "node_list.hpp"

int main()

執行結果如下:

5 4 3 2 1 

1 2 3 4 5

4 3 2 1

1 2 3 4

4 3 2 1

program ended with exit code: 0

Java實現鍊錶,線性表

package mylist public inte ce list linklist package mylist public class linklist implements list system.out.println 插入0到19 list.output system.out.prin...

線性表的鍊錶實現

include include define elemtype int typedef struct lnode list struct lnode struct lnode l list ptrl list makeempty list ptrl 建立空表 int length list ptrl...

線性表的鍊錶實現

include printf輸出函式和scanf輸入函式所在標頭檔案 include exit退出函式所在標頭檔案 include malloc動態記憶體分配函式 realloc函式所在的標頭檔案 includeusing namespace std 用 define巨集定義來定義符號常量 函式結果...