更多資料請點選:我的目錄
本篇僅用于記錄自己所學知識及應用,代碼仍可優化,僅供參考,如果發現有錯誤的地方,盡管留言于我,謝謝!
運作結果:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnLwQzMwQzMyIjM3EjMwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
#include <stdio.h>
#include <stdlib.h>
struct node//設計節點
{
int data;
struct node *next;
};
struct node *list()//初始化一個帶頭節點的空連結清單
{
struct node *head = malloc(sizeof(struct node));
if(head != NULL)
{
head->next = NULL;
}
return head;
}
struct node *new_list(int data)//建立一個新的節點
{
struct node *new = malloc(sizeof(struct node));
if(new != NULL)
{
new->data = data;
new->next = NULL;
}
return new;
}
void list_link(struct node *head, struct node *new);//節點連接配接
void show_list(struct node *head);//輸對外連結表所有節點
void over_turn(struct node *head);//連結清單翻轉
int main()
{
struct node *head;
head = list();
printf("請輸入将要插入的整數個數:");
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
struct node *new = new_list(i);
list_link(head , new);
}
printf("翻轉前:");
show_list(head);
over_turn(head);
printf("翻轉後:");
show_list(head);
return 0;
}
void list_link(struct node *head, struct node *new)//節點連接配接
{
if(head != NULL || new != NULL)
{
struct node *tail = head;
while(tail->next != NULL)
{
tail = tail->next;
}
tail->next = new;
}
}
void show_list(struct node *head)//輸對外連結表所有節點
{
while(head->next != NULL)
{
head = head->next;
printf("%d\t",head->data);
}
printf("\n");
}
void over_turn(struct node *head)//連結清單翻轉
{
struct node *p = head->next;
head->next = NULL;
while( p != NULL)
{
struct node *temp = p->next;
p->next = head->next;
head->next = p;
p = temp;
}
}
更多資料請點選:我的目錄