c單鏈表基本操作,本人已祥測

2021-07-11 09:40:39 字數 2745 閱讀 9001

#include #include #include using namespace std;

typedef int elemtype;

typedef struct node

node;

node *l;

//初始化  

void init_liat(node *i)

///@note建立鍊錶  

//p2屬於中間變數,用於不停地給p1賦值,l屬於head  

node *creat_list()

//memset(p1, 0, sizeof(struct node));

printf("請輸入鍊錶中的值");

cin >> p1->element;

p1->next = null;

while (p1->element > 0)

else

p2 = p1;

p1 = (node *)malloc(sizeof(struct node));

if (p1 == null || p2 == null)

//memset(p1, 0, sizeof(struct node));

printf("請輸入鍊錶中的值");

cin >> p1->element;

p1->next = null;

}printf("建立表成功\n");

return l;

}//列印鍊錶  

int print_list(node *i)

cout << "print list as flollows" << endl;

while (p != null)

return 1;

}//清空鍊錶  

node *clear_list(node *i)

while (p != null)

l = null;

if (l == null)

return l;

}//list 的長度  

int length_list(node *l)

while (l != null)

return cout;

}//獲取某個節點的資訊  

elemtype get_pos_element(node *k, int pos)

if (pos < 1)

while (k != null)

k = k->next;

++i;

}printf("no element");

exit(0);}//

node* get_pos_prev_address(node *l, int pos)

if (pos < 1)

while (l != null)

prev = l;

l = l->next;

++i;

}printf("get_pos_ress()執行成功,超出查詢範圍!\n");

system("pause");

return null;

}node * get_element_position(node *l, elemtype x)

while (l != null)

l = l->next;

}printf("get_element_position執行成功,該鍊錶不含有元素%d\n", x);

return null;

}node *get_element_prev_position(node *l, elemtype x)

while (l != null)

prev = l;

l = l->next;

}printf("get_element_prev_position執行成功,該鍊錶不含有元素%d\n", x);

return null;

}int modify_element(node *l, int pos, elemtype x)

if (pos < 1)

while (l != null)

l = l->next;

++i;

}printf("modify_element函式執行成功,超出查詢範圍!\n");

system("pause");

return 0;

}void insert_head_node(node **l, elemtype x)

void insert_end_node(node **l, elemtype x)

last->next = insert_node;

insert_node->element = x;

insert_node->next = null;

printf("insert_end_list執行成功,向末尾新增元素%d!\n", x);

}int insert_pos_list(node **l, int pos, elemtype x)

insert_node->element = x;

insert_node->next = pos_node->next;

pos_node->next = insert_node;

}//p為反轉後的頭,反轉link_list

node *reverselist(node *head)

head = p;

print_list(head);

return head;

}void main()

c 單鏈表基本操作

下午沒事,看資料結構。想到自畢業以後,都沒有寫過這些東西了。寫個鍊錶基本操作,還沒完全測試。includeusing namespace std node 節點 struct node int data node p 單鏈表 class link 通過陣列夠造 link int arr,int le...

C 單鏈表基本操作

include using namespace std struct node class list 建立帶頭結點的鍊錶,head next null 表示鍊錶為空,只有乙個頭結點 void creatlist1 int n 頭插入法建立鍊錶 void creatlist2 int n 尾插入法建立...

C 單鏈表基本操作

鍊錶一直是面試的高頻題,今天先總結一下單鏈表的使用,下節再總結雙向鍊錶的。本文主要有單鏈表的建立 插入 刪除節點等。1 概念 單鏈表是一種鏈式訪問的資料結構,用一組位址任意的儲存單元存放線性表中的資料元素。鍊錶中的資料是以結點來表示的,每個結點的構成 元素 指標,元素就是儲存資料的儲存單元,指標就是...