2021-07-15 20:21

# 大一c语言链表不会了不会了

• 写回答
• 好问题 提建议
• 关注问题
• 收藏
• 邀请回答

#### 3条回答默认 最新

• qfl_sdu 2021-07-15 20:24
已采纳

参考：https://blog.csdn.net/qfl_sdu/article/details/118326835
链表的增删改查操作都有

已采纳该答案
评论
解决 无用
打赏 举报
• 浪客 2021-07-15 21:14
``````#include <stdio.h>
#include <stdlib.h>
typedef struct LNode
{
int value;
struct LNode *next;

{
int value;
LNode *head = L, *rear = L;
printf("请输入链表的值，输入-1结束：\n");
scanf("%d", &value);
while (value != -1)
{
s->value = value;
s->next = NULL;
rear->next = s;
rear = s;
scanf("%d", &value);
}
return L;
}
{
LNode *p = L->next;
while (p != NULL)
{
printf("%d ", p->value);
p = p->next;
}
printf("\n");
}

int list_Query(Linklist L, int val)
{
Linklist p = L->next;
while (p != NULL)
{
if (p->value == val)
{
printf("已找到%d！\n", val);
return 1;
}
p = p->next;
}
printf("找不到查询的数！\n");
return -1;
}

void list_Del(Linklist L, int val)
{
Linklist p = L->next, pre;
pre = p;
while (p != NULL)
{
if (p->value == val)
{
pre->next = p->next;
free(p);
p = pre->next;
printf("已删除%d!\n", val);
return;
}
pre = p;
p = p->next;
}
printf("%d不存在!\n", val);
}

int main()
{
Linklist L1 = NULL;
int s = 0, val;
do
{
printf("\n\t1.建立链表\n");
printf("\t2.查询\n");
printf("\t3.删除\n");
printf("\t4.显示链表\n");
printf("\t5.退出\n");
printf("\t输入选择：");
scanf("%d", &s);
switch (s)
{
case 1:
L1 = list_Creat();
break;
case 2:
printf("输入要查询的数：");
scanf("%d", &val);
list_Query(L1, val);
break;
case 3:
printf("输入要删除的数：");
scanf("%d", &val);
list_Del(L1, val);
break;
case 4:
list_Display(L1);
break;
case 5:
exit(0);
break;
}
} while (1);

return 0;
}
``````
评论
解决 1 无用
打赏 举报
• qfl_sdu 2021-07-15 23:10

修改后的代码：

``````#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NMBSCORE 3
struct StNode
{
int num;        //学号
char name[20];  //姓名
StNode* next;
};

//录入信息
struct StNode* Input(struct StNode* head)
{
struct StNode* p,*node;
int i;
system("cls");
node = (struct StNode*)malloc(sizeof(struct StNode));
//录入信息
printf("请输入学号:");
scanf("%d",&node->num);
printf("请输入姓名:");
scanf("%s",node->name);

node->next = 0;

if (head == 0)
{
//
}else
{
while(p->next)
p = p->next;
//新建节点
p->next = node;
}
system("pause");
}
//显示所有学生信息
void showAll(struct StNode* head)
{
struct StNode* p;
int i=1,j;
system("cls");
while(p)
{
printf("学生%d学号:%d\n",i,p->num);
printf("学生%d姓名:%s\n",i,p->name);
printf("\n");
i++;
p = p->next;
}
system("pause");
}

//查询信息
void Find(struct StNode* head)
{
int opt;
struct StNode* p = head;
int id,j;
char name[20]={0};
system("cls");
printf("1.按学号查询\n");
printf("2.按姓名查询\n");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("请输入学号:");
scanf("%d",&id);
while(p)
{
if(p->num == id)
{
printf("学生学号:%d\n",p->num);
printf("学生姓名:%s\n",p->name);
printf("\n");
break;
}
p = p->next;
}
if(p==0)
printf("查无此人\n");
break;
case 2:
printf("请输入姓名:");
scanf("%s",name);
while(p)
{
if(strcmp(p->name,name) ==0)
{
printf("学生学号:%d\n",p->num);
printf("学生姓名:%s\n",p->name);

printf("\n");
break;
}
p = p->next;
}
if(p==0)
printf("查无此人\n");
break;
}
system("pause");
}

//删除学生信息
void DeleteInfo(struct StNode* head)
{
struct StNode* node,*pre;
int id,i;
system("cls");
printf("请输入需要删除信息的学生学号:");
scanf("%d",&id);

if (head->num == id)
{
printf("该学生信息已经删除\n");
system("cls");
return;
}else
{
while(node)
{
if (node->num == id)
{
pre->next = node->next;
free(node);
node = 0;
printf("该学生信息已经删除\n");
system("pause");
return;
}else
{
pre = node;
node = node->next;
}
}
printf("查无此人\n");
system("pause");
}
}

int main()
{
struct StNode* head = 0;
int opt = 0;
int bgo = 1;

while(bgo)
{
system("cls");
printf("--------------学生信息管理系统---------------|\n");
printf("|   1.录入学生信息                           |\n");
printf("|   2.显示所有学生信息                       |\n");
printf("|   3.学生信息查询                           |\n");
printf("|   4.学生信息删除                           |\n");
printf("|   0.退出系统                               |\n");
printf("----------------------------------------------\n");
scanf("%d",&opt);
switch(opt)
{
case 0:
bgo = 0;
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4: