LinkList練習
*請勿使用全域變數
給予一個初始Link List,請你進行插入節點、刪除節點的操作
請輸出進行操作後,最後的Link List
本題不會出現有重複數值的節點
輸入說明
第一行輸入 n,代表初始Link List有多少個節點
第二行輸入 n 個整數,依序分別為每個節點的值,第一個整數為起始節點,最後一個整數為結束節點
接下來有多行,總共有6種操作:
1 x,代表從尾端插入數值為 x 的新節點。
2 x y,代表搜尋數值為 x 的節點,並在其之後插入數值為 y 的新節點;
若 x 不在Link List中,則不必插入 y 。
3 x,代表刪除數值為 x 的節點;若 x 不在Link List中,則不必刪除任何節點。
4 代表 刪除起始節點,須將起始節點的下一個節點做為新的起始節點。
若 Link List 已無節點,則不必刪除任何節點。
5 代表 刪除結束節點,須將結束節點的前一個節點做為新的結束節點。
若 Link List 已無節點,則不必刪除任何節點。
6 代表 結束程式,並從起始節點到結束節點,依序輸出各個節點的值;
若Link List已無任何節點,則輸出"None any node"。
輸出說明
輸出共一行,從起始節點到結束節點,依序輸出各個節點的值;每個數字中間以空格間隔
若Link List已無任何節點,則輸出"None any node"
範例輸入1 說明
5
1 2 3 4 5 初始Link List: 1 2 3 4 5
1 6 從尾端插入數值為 6 的新節點(1 2 3 4 5 6)
2 3 7 搜尋數值為 3 的節點,並在其之後插入數值為 7 的新節點(1 2 3 7 4 5 6)
3 2 刪除數值為 2 的節點 (1 3 7 4 5 6)
4 刪除起始節點 (3 7 4 5 6)
5 刪除結束節點 (3 7 4 5)
6 結束程式
範例輸出1
3 7 4 5
Sample Input 1:
5
1 2 3 4 5
1 6
2 3 7
3 2
4
5
6
Sample Output 1:
3 7 4 5
Sample Input 2:
6
1 3 5 7 9 11
2 1 15
2 3 17
2 5 19
2 7 21
2 9 23
2 11 25
6
Sample Output 2:
1 15 3 17 5 19 7 21 9 23 11 25
Sample Input 3:
5
0 2 4 6 8
1 10
5
4
2 0 1
3 0
3 2
6
Sample Output 3:
4 6 8
Sample Input 4:
5
5 4 3 2 1
3 5
3 4
3 3
3 2
3 1
4
5
6
Sample Output 4:
None any node
Sample Input 5:
1
1
5
2 1 2
1 3
2 3 4
2 3 5
5
6
Sample Output 5:
3 5
你题目的解答代码如下:
#include <stdio.h>
#include <stdlib.h>
typedef struct Link{
int elem;
struct Link *next;
}link;
link * initLink(int a[],int n){
link * p=(link*)malloc(sizeof(link));//创建一个头结点
link * temp=p;//声明一个指针指向头结点,用于遍历链表
p->next=NULL;
//生成链表
for (int i=0; i<n; i++) {
link *q=(link*)malloc(sizeof(link));
q->elem=a[i];
q->next=NULL;
temp->next=q;
temp=temp->next;
}
return p;
}
link * insertElem(link * p,int add,int elem){
link * temp=p;//创建临时结点temp
//首先找到要插入位置的上一个结点
for (int i=0; i<add; i++) {
if (temp==NULL) {
printf("插入位置无效\n");
return p;
}
temp=temp->next;
}
//创建插入结点c
link * c=(link*)malloc(sizeof(link));
c->elem=elem;
//向链表中插入结点
c->next=temp->next;
temp->next=c;
return p;
}
link * delElem(link * p,int add){
link * temp=p;
//遍历到被删除结点的上一个结点
for (int i=0; i<add; i++) {
temp=temp->next;
}
link * del=temp->next;//单独设置一个指针指向被删除结点,以防丢失
temp->next=temp->next->next;//删除某个结点的方法就是更改前一个结点的指针域
free(del);//手动释放该结点,防止内存泄漏
return p;
}
int selectElem(link * p,int elem){
link * t=p;
int i=0;
while (t->next) {
t=t->next;
if (t->elem==elem) {
return i;
}
i++;
}
return -1;
}
void display(link *p){
link* temp=p;//将temp指针重新指向头结点
//只要temp指针指向的结点的next不是Null,就执行输出语句。
int i=0;
while (temp->next) {
temp=temp->next;
if (i>0)
printf(" ");
printf("%d",temp->elem);
i++;
}
printf("\n");
}
int main() {
int i,n,c,x,y,add;
scanf("%d", &n);
int a[n];
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
link *p=initLink(a,n);
while (1)
{
scanf("%d", &c);
if (c==6)
break;
switch (c)
{
case 1:
scanf("%d", &x);
p=insertElem(p, n, x);
n++;
break;
case 2:
scanf("%d", &x);
scanf("%d", &y);
add = selectElem(p, x);
if (add!=-1) {
p=insertElem(p, add+1, y);
n++;
}
break;
case 3:
scanf("%d", &x);
add = selectElem(p, x);
if (add!=-1) {
p=delElem(p, add);
n--;
}
break;
case 4:
if (p->next==NULL) break;
p=delElem(p, 0);
n--;
break;
case 5:
if (p->next==NULL) break;
p=delElem(p, n-1);
n--;
break;
}
}
if (p->next==NULL)
printf("None any node\n");
else
display(p);
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!