c语言节点的作用,C语言链表(基本功能函数)
創建鏈表
Node* CreateLinkList(const int n)
{
Node *head = NULL;
Node *previous = NULL;
Node *current = NULL;
int i = 0;
for (i = 0; i < n; i++){
current = (Node *)malloc(sizeof(Node));
if (current == NULL){
printf("內存分配失敗!\n");
exit(-1);
}
if (head == NULL) //頭結點的地址指向頭指針
head = current;
if (previous != NULL)
previous->next = current;
printf("請輸入數值: ");
scanf("%d", ¤t->data);
current->next = NULL;
previous = current;
}
return head;
}
2.遍歷鏈表
void TraverseLinkList(Node* const head)
{
Node *current = NULL; //臨時指針
current = head;
printf("鏈表遍歷結果為:");
while (current != NULL){
printf(" %d", current->data);
current = current->next;
}
printf("\n");
}
3.獲取鏈表長度
int GetListLength(Node* const head)
{
Node *current = NULL; //臨時指針
int len = 0;
current = head;
while (current != NULL){
len++;
current = current->next;
}
return len;
}
4.插入鏈表
int InsertList(Node **phead, int i, int val)
{
Node *head = NULL; //臨時指針
Node *previous = NULL;
Node *current = NULL;
Node *new_code = NULL;
int len = 0;
int j = 1;
head = *phead;
len = GetListLength(head);
if (i > len + 1 || i < 1){
printf("節點不存在...\n");
return FALSE;
}
new_code = (Node *)malloc(sizeof(Node)); //為新節點分配內存
//新元素插入到鏈表頭部,更新頭指針的值
if (i == 1){
new_code->data = val;
new_code->next = head;
*phead = new_code; //更新頭指針的值
return TRUE;
}
//新元素插入到鏈表非頭部的其他位置
current = head;
while (j < i){
previous = current;
current = current->next;
j++;
}
previous->next = new_code;
new_code->next = current;
new_code->data = val;
return TRUE;
}
5.刪除鏈表中節點
int DeleteElem(Node **phead, int i)
{
Node *head = NULL; //臨時指針
Node *previous = NULL;
Node *current = NULL;
int j = 1;
int len = 0;
head = *phead;
len = GetListLength(head);
if (i > len){
printf("不再該節點...\n");
return FALSE;
}
current = head;
if (i == 1){ //刪除頭節點
*phead = current->next; //更新頭指針
free(current);
current = NULL;
return TRUE;
}
while (j < i){
previous = current;
current = current->next;
j++;
}
previous->next = current->next;
free(current);
current = NULL;
return TRUE;
}
6.銷毀鏈表
void DestroyList(Node **phead)
{
Node *previous = NULL;
Node *current = NULL;
current = *phead;
while (current != NULL){
previous = current;
current = current->next;
free(previous);
previous = NULL;
}
*phead = NULL;
}
7.鏈表排序(冒泡排序法)
void SortList(Node *head)
{
Node *current = NULL;
Node *p = NULL;
int tmp = 0;
current = head;
if (current == NULL || current->next == NULL)
return;
for (current = head; current->next != NULL; current = current->next){
for (p = head; p->next != NULL; p = p->next){
if (p->data > p->next->data){
tmp = p->data;
p->data = p->next->data;
p->next->data = tmp;
}
}
}
}
8.主函數
#define TRUE 1
#define FALSE 0
/* 單鏈表存儲結構 */
typedef struct Node
{
int data;
struct Node *next;
}Node;
int main(void)
{
Node *head = NULL;
int val = 0;
int n = 0;
head = CreateLinkList(5); //創建鏈表
TraverseLinkList(head); //遍歷
printf("鏈表長度為: %d\n", GetListLength(head));
SortList(head); //排序
printf("排序之后");
TraverseLinkList(head); //遍歷
printf("\n");
/* 插入新節點 */
printf("新節點的位置: ");
scanf("%d", &n);
printf("新節點的值: ");
scanf("%d", &val);
InsertList(&head, n, val);
printf("插入新節點之后");
TraverseLinkList(head);
printf("鏈表長度為: %d\n", GetListLength(head));
printf("\n");
/* 刪除節點 */
printf("要刪除的節點的位置: ");
scanf("%d", &n);
DeleteElem(&head, n);
printf("刪除節點之后");
TraverseLinkList(head);
printf("鏈表長度為: %d\n", GetListLength(head));
printf("\n");
/* 銷毀鏈表 */
printf("銷毀鏈表之后");
DestroyList(&head);
printf("head = %p\n", head); //打印頭節點的值(head = NULL 頭節點釋放成功)
printf("鏈表長度為: %d\n", GetListLength(head));
printf("\n");
printf("hello...\n");
return 0;
}
9.測試結果
總結
以上是生活随笔為你收集整理的c语言节点的作用,C语言链表(基本功能函数)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大腿粗,抽脂大概总共需要花多少钱
- 下一篇: 输卵管造影手术无痛吗