#include
#include
/* ***************==
定義鍊錶資料結構
***************==
*/ struct node
;typedef struct node node;
typedef struct node * link;
/* ***************
功能:設頭結點
返回:void
***************
*/
void creat_link(link * head)
(*head) -> next = *head;
(*head) -> prior = *head;
}/*
********************=
功能:從頭部插入結點
返回:void
********************=
*/
void insert_node_head(link * head,link new_node)
}/*
********************=
功能:從尾部插入結點
返回:void
********************=
*/ void insert_node_tail(link * head,link new_node)
// }
tmp -> next = new_node;
new_node -> next = *head;
new_node -> prior = tmp;
(*head) -> prior = new_node;
/* 更好的寫法 */
//head->prior->next = newnode;
//newnode->prior = head->prior;
//newnode->next = head;
//head->prior = newnode;
}void insert_node_mid(link * head,link new_node,int num) //插入節點
else
if(tmp -> num == num) //判斷這個位置是否為真的節點
else}}
void delete_node(link * head,int num_delete)
else
if(tmp -> num == num_delete) //找到這個位置
else if(tmp -> next == (*head))
else
*/tmp->prior->next = tmp->next;
tmp->next->prior = tmp->prior;
free(tmp); //釋放
tmp = null; //置空
}else}}
void revers_link(link * head)
else
p2 -> next = p3;
p2 -> prior = p1;
p1 -> next = p2;
p1 -> prior = (*head);
(*head) -> next -> next = (*head);
(*head) -> next = p1;}}
void free_node(link * head)
while( (*head)->next != (*head))
/*仍然需要構成環*/
(*head) -> next = (*head);
(*head) -> prior = (*head);
printf("link is freed\n");
}void display(link head)
while(tmp != head)
}int main()
new_node -> num = i; //節點賦值
insert_node_head(&head,new_node); //插入節點
// insert_node(&head,new_node,new_node->num);
// insert_node_tail(&head,new_node); //插入節點(尾插)
}display(head);
printf("請輸入要插入的序號:");
scanf("%d",&num_insert);
new_node = (link)malloc(sizeof(node)) ; //建立乙個新的節點,返回其位址
if(null == new_node)
new_node -> num = 321; //節點賦值
insert_node_mid(&head,new_node,num_insert); //插入節點(中間)
display(head);
printf("請輸入要刪除的序號:");
scanf("%d",&num_delete);
delete_node(&head,num_delete);
display(head);
printf("------------\n");
revers_link(&head);
display(head);
printf("------------\n");
free_node(&head);
printf("------------\n");
display(head);
printf("------------\n");
return 0;
}
帶表頭結點的雙向迴圈鍊錶
include include 定義鍊錶資料結構 struct node typedef struct node node typedef struct node link 功能 設頭結點 返回 void void creat link link head head next head head p...
帶表頭節點的迴圈雙向鍊錶基本操作
include include struct node typedef struct node node typedef node link 函式功能 釋放鍊錶 void release link head p head head null free p 函式功能 建立節點 int node cre...
鍊錶 帶頭結點的雙向迴圈鍊錶
還需改進 creat node這個函式應有返回型別,來判斷新建結點是否成功,不然主函式中不管成不成功都會訪問該節點成員。改了這個函式,在主函式中create node後要判斷是否成功,不成功就提示並退出函式,退出前別忘了還要釋放鍊錶!同時create link這個函式中也要判斷head是否申請成功,...