学期(2022-2023-1) 学号(20221320) 《计算机基础与程序设计》第十九周学习总结
作业信息
各项要求 | 具体内容 |
---|---|
<班级的链接> | 2022-2023-1-计算机基础与程序设计 |
<作业要求的链接> | 无 |
作业的目标 | 无 |
作业正文 | 见下方 |
代码调试中的问题和解决过程
-
问题1:
-
问题1解决方案:
#include <stdio.h>
#include <stdlib.h>
struct num
{
int a;
struct num *pNext;
};
#define SIZE sizeof(struct num)
struct num *CreatLiList(void)
{
struct num *spHead,*spPre,*spCur;
int n, count = 0;
spPre = (struct num *)malloc(SIZE);
spHead = spPre;
spHead -> pNext = NULL;
do
{
scanf("%d",&n);
if (count != 6)
{
count ++;
spCur = (struct num *)malloc(SIZE);
spCur -> a = n;
spCur -> pNext = NULL;
spPre -> pNext = spCur;
spPre = spCur;
}
} while (count != 6);
return spHead;
}
int AddNode(struct num *sp, int x)
{
struct num *spCur,*spNew;
spCur = sp;
int flag = 1;
while((spCur -> pNext)->a < x)
{
spCur = spCur -> pNext;
}
spNew = (struct num *)malloc(SIZE);
if (spNew == NULL) return 1;
spNew -> a = x;
spNew -> pNext = spCur -> pNext ;
spCur -> pNext = spNew;
return 1;
}
void TraverLiList(struct num *sp)
{
struct num *spCur;
spCur = sp -> pNext;
while (spCur != NULL)
{
printf("%d ", spCur->a);
spCur = spCur -> pNext;
}
}
int main()
{
printf("输入数组6个元素的值。\n");
struct num* head = CreatLiList();
printf("此链表各个结点的数据域为:");
TraverLiList(head);
printf("\n");
int x;
printf("输入要插入的数据x:");
scanf("%d", &x);
int i = AddNode(head, x);
printf("插入后链表各个结点的数据域为:");
TraverLiList(head);
return 0;
}