#include
#include
struct node //定義結點的結構體
;typedef struct node node; //將struct node簡單定義為node
typedef struct node *link; //將stcuct node型別定義為*link
void insert_node_head(link head, link new_node) //頭插
void insert_node_tail (link head, link new_node) //尾插
new_node->next = head;
new_node->prior = p;
p->next = new_node;
new_node->next->prior = new_node;
void display_link_next (link head) //後繼輸出
while(p != head)
}void display_link_prior (link head) //前驅輸出
}void release_link (link head) //釋放鍊錶
// head->next = head; //使用p、q,在釋放完p後,head後就沒有指向,則必須加指向null,否則會段錯誤
void delete_node (link head, int number) //刪除其中乙個結點
else
else
if(p == head) //未找到
else //刪除的結點在中間}}
}int number_link (link head) //計算鍊錶長度並返回
return num;
link find_link_next (link head, int n) //查詢對應學號所在結點內容,後繼
if(p->num == n)
else
}link find_link_prior (link head, int n) //查詢對應學好所在結點內容,前驅
if(p->num == n)
else
}void enter_link_next (link head, link enter_node,int number) //插入新的結點,插在所找結點的前面,後繼
/*p->prior->next = enter_node;
enter_node->prior = p->prior;
enter_node->next = p;
p->prior = enter_node;*/
insert_node_head(p, enter_node);
}void enter_link_prior (link head, link enter_node, int number) //插入新的結點,插在所找結點的後面,前驅
insert_node_tail(p, enter_node);
void search_change_node (link head, link change_node, int num) //查詢並修改
if(p == head)
else
}void insert_rank_node (link head, link new_node) //插入的時候排序
else if(p->next == head) //乙個結點
else
}else //多個結點
else
if((p->num) < (new_node->num)) //比較最後乙個結點的學號是否小於new_node的學號,小於則new_node插最後
else
new_node->next = p->next;
p->next = new_node;}}
}}link write_node(link *new_node) //寫入
}void create_link(link * head) //建立頭指標,並定義為空指標
int main()
display_link_next(head); //輸出
/*num = number_link(head); //返回結點數
printf("number = %d\n", num);
printf("please input the num, find the name:\n");
scanf("%d",&n);
// printf("%s\n",find_link_next(head, n)->name); // 輸出查詢號對應的名字
printf("%s\n",find_link_prior(head, n)->name);
printf("\n");
printf("please input the number of delete:\n");
scanf("%d",&number);
delete_node (head, number); //刪除其中乙個結點
*/// flashback_link(head); //倒敘輸出
/*// enter_node = (link)malloc(sizeof(node));
printf("please input the name and number:\n");
write_node(&enter_node);
scanf("%d",&(enter_node->num));
scanf("%s",enter_node->name);
printf("please input the number of enter:\n");
scanf("%d",&number);
// enter_link_next(head, enter_node, number); //在已經輸入的結點裡再插入乙個新的結點
enter_link_prior(head, enter_node, number);
*/printf("please input the number of search and change:");
scanf("%d",&num); //查詢的學號
write_node(&change_node);
printf("please input the number and name:");
scanf("%d",&(change_node->num)); //修改的學號
scanf("%s",change_node->name); //修改的名字
search_change_node(head, change_node, num); //查詢並修改
// display_link_next(head); //輸出
display_link_prior(head); //輸出
release_link (head); //釋放
display_link_next(head);
return 0;
}
建立單項鍊表,然後實現單項鍊表逆序
建立乙個任意數目的單項鍊表,每項的位置作為自己的初始資料 返回鏈頭 node initlink int num return head 輸出單項鍊表的全部資料 void display node head node curnode head while curnode.next null syste...
單項鍊表反轉
遍歷,將當前節點的下乙個節點快取後更改當前節點指標 public static node reverse node head node pre head node cur head.getnextnode node next while null cur 將原鍊錶的頭節點的下乙個節點置為null,再...
建立單項鍊表
鍊錶是動態分配儲存空間的鏈式儲存結構,其中包括乙個 頭指標 變數,頭指標中存放乙個位址,該位址指向乙個元素。鍊錶中每乙個元素稱為 節點 每個節點都由兩部分組成,即儲存資料元素的資料域和儲存直接後繼儲存位置的指標域。指標域中儲存的即是鍊錶的下乙個節點的位置,是乙個指標。多個節點構成乙個鍊錶。inclu...