供参考:
#include <stdio.h>
#include <stdlib.h>
typedef struct node_s {
int data;
struct node_s* next;
} node_t;
void create_List(node_t** L,int n)// N個資料節點加入
{
int i;
node_t* pt = NULL, * pL = NULL;
(*L) = (node_t*)malloc(sizeof(node_t));
(*L)->next = NULL;
pL = (*L);
for (i = 0; i < n; i++) {
pt = (node_t*)malloc(sizeof(node_t));
pt->next = NULL;
scanf("%d", &pt->data);
pL->next = pt;
pL = pt;
}
}
void insert_end(node_t* L, int x)//從最後面加入資料 x
{
node_t* pL = L, * pt = NULL;
while (pL->next) pL = pL->next;
pt = (node_t*)malloc(sizeof(node_t));
pt->next = NULL;
pt->data = x;
pL->next = pt;
}
void delete_first(node_t* L)//刪除最前面節點,若 List 無節點,則不必刪除
{
node_t* pL = NULL;
if (!L->next) return;
pL = L->next;
L->next = pL->next;
free(pL);
}
void delete_end(node_t* L)//刪除最後面節點,若 List 已無節點,則不必刪除
{
node_t* pL = L, * pre = NULL;
if (!L->next) return;
while (pL->next) { pre = pL; pL = pL->next; }
pre->next = pL->next;
free(pL);
}
void deletd_x(node_t* L, int x)//刪除 List 內資料為 x 的節點;
{ //若 x 不存在則不刪除任何節點
if (!L->next) return;
node_t* pL = L->next, * pre = L;
while (pL) {
if (pL->data == x) {
pre->next = pL->next;
free(pL);
pL = pre;
}
else {
pre = pL;
pL = pL->next;
}
}
}
void search_x_insert_y(node_t* L, int x, int y)//搜尋 List 內資料為 x 的節點,並在其後加入
{ //數值 y 的新節點。若 x 不在 List 中,則不必加入y
if (!L->next) return;
node_t* pL = L->next, * pt = NULL;
while (pL && pL->data != x) pL = pL->next;
if (pL != NULL) {
pt = (node_t*)malloc(sizeof(node_t));
pt->next = NULL;
pt->data = y;
pt->next = pL->next;
pL->next = pt;
}
}
void convert_List(node_t* L) //反轉 Link List
{
if (!L->next) return;
node_t* pL = L->next, * pt = NULL;
L->next = NULL;
while (pL) {
pt = pL;
pL = pL->next;
pt->next = L->next;
L->next = pt;
}
}
void swap_x_y(node_t* L, int x, int y)//交換數值為 x和 y 節點的位置,
//若 x 或 y 不在 List中,則不必交換
{
if (!L->next) return;
node_t* pL = L->next, * prL = L,
* px = NULL, * py = NULL,
* prx = NULL, *pry = NULL;
while (pL) {
if (pL->data == x) {
prx = prL;
px = pL;
}
else if (pL->data == y) {
pry = prL;
py = pL;
}
if (px && py) break;
prL = pL;
pL = pL->next;
}
if (px && py) {
if (pry == px) {
prx->next = px->next;
px->next = py->next;
py->next = px;
}
else {
prL = py->next;
py->next = px->next;
px->next = prL;
prx->next = py;
pry->next = px;
}
}
}
void print_List(node_t* L)//從最前面節點開始依序輸出各節點的值,每個數字
{ //以中間空白間隔,若 List 為空,則輸出"None"
node_t* pL = L;
if (L == NULL || L->next == NULL)
printf("None");
else {
while (pL->next) {
printf(pL->next == L->next ? "%d" : " %d", pL->next->data);
pL = pL->next;
}
//printf("\n");
}
}
int main()
{
int N, C, s, x, y;
node_t* List = NULL;
scanf("%d", &N);
create_List(&List, N);
scanf("%d", &C);
while (C--) {
scanf("%d", &s);
switch (s)
{
case 1:scanf("%d", &x); insert_end(List, x); break;
case 2:delete_first(List); break;
case 3:delete_end(List); break;
case 4:scanf("%d", &x); deletd_x(List, x); break;
case 5:scanf("%d%d", &x, &y); search_x_insert_y(List, x, y); break;
case 6:convert_List(List); break;
case 7:scanf("%d%d", &x, &y); swap_x_y(List, x, y); break;
default:
break;
}
}
print_List(List);
return 0;
}