鱼C论坛

 找回密码
 立即注册
查看: 1177|回复: 1

[已解决]求助大佬 单链表按位置删除操作

[复制链接]
发表于 2022-1-1 12:38:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
单链表按位置删除操作,已知一个带头结点的单链表中包含6个结点,每个结点包含两个域,一个域为数据域(整型数据),另一个域为指针域,
5个结点的数据域分别为22,7,45,89,78,15,从键盘输入一个整数,该整数为被删除数据的位置。输出前5个数
每个输出的数之间用一个空格隔开,最前面没有空格,最后有一个空格)。
输入示例:2
输出示例:22 45 89 78 15
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. typedef struct LNode{
  4. int data;
  5. struct LNode* next;
  6. }LNode,*LinkList;
  7. int main(){
  8. int arr[]= {22,7,45,89,78};
  9. int n = 5,pos,x,j;
  10. LinkList rear,p,q;
  11. //尾插法创建链表 P是链表长度
  12. LinkList head =
  13. (LinkList)malloc(sizeof(LNode));
  14. head->next = NULL;
  15. rear = head;
  16. for(int i=0;i<n;i++){
  17. p = (LinkList)malloc(sizeof(LNode));
  18. p->data = arr[i];
  19. p->next = NULL;
  20. rear->next = p;
  21. rear = p;
  22. }
复制代码
最佳答案
2022-1-1 13:09:42
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Node{
  4.     int data;
  5.     struct Node* next;
  6. };

  7. int main(){
  8.     int arr[] = {22, 7, 45, 89, 78, 15};
  9.     struct Node *head = NULL, *tail = NULL;
  10.    
  11.     for(int i = sizeof(arr)/sizeof(int)-1; i > -1; i--){
  12.         tail = head;
  13.         head = (struct Node*)malloc(sizeof(struct Node));
  14.         head->data = arr[i];
  15.         head->next = tail;
  16.     }
  17.    
  18.     int a;
  19.     scanf("%d", &a);
  20.    
  21.     struct Node *p = head;
  22.     struct Node *t;
  23.     for(int i = 0; p; i++, p = p->next)
  24.     if(i+1 == a){
  25.         t = p->next;
  26.         p->data = t->data;
  27.         p->next = t->next;
  28.     }
  29.    
  30.     while(head){
  31.         printf("%d ", head->data);
  32.         head = head->next;
  33.     }
  34.     return 0;
  35. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-1-1 13:09:42 | 显示全部楼层    本楼为最佳答案   
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Node{
  4.     int data;
  5.     struct Node* next;
  6. };

  7. int main(){
  8.     int arr[] = {22, 7, 45, 89, 78, 15};
  9.     struct Node *head = NULL, *tail = NULL;
  10.    
  11.     for(int i = sizeof(arr)/sizeof(int)-1; i > -1; i--){
  12.         tail = head;
  13.         head = (struct Node*)malloc(sizeof(struct Node));
  14.         head->data = arr[i];
  15.         head->next = tail;
  16.     }
  17.    
  18.     int a;
  19.     scanf("%d", &a);
  20.    
  21.     struct Node *p = head;
  22.     struct Node *t;
  23.     for(int i = 0; p; i++, p = p->next)
  24.     if(i+1 == a){
  25.         t = p->next;
  26.         p->data = t->data;
  27.         p->next = t->next;
  28.     }
  29.    
  30.     while(head){
  31.         printf("%d ", head->data);
  32.         head = head->next;
  33.     }
  34.     return 0;
  35. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 03:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表