C语言链表问题,求解答!
发布网友
发布时间:2022-05-12 02:56
我来回答
共2个回答
热心网友
时间:2023-08-03 04:35
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct node {
unsigned id;
char name[30];
double scores[3];
struct node *next;
}*LIST,*pNode;
pNode ReadInfor() {
pNode newnode = (pNode)malloc(sizeof(struct node));
printf("学号:");
scanf("%u",&newnode->id);
fflush(stdin);
printf("姓名:");
scanf("%s",newnode->name);
printf("三门课程成绩(空格隔开):");
scanf("%lf%lf%lf",&newnode->scores[0],&newnode->scores[1],&newnode->scores[2]);
return newnode;
}
LIST Create(int n) {
int i;
LIST head;
pNode p;
head = p = (pNode)malloc(sizeof(struct node));
for(i = 0; i < n; ++i) {
p->next = ReadInfor();
p = p->next;
}
p->next = NULL;
return head;
}
pNode Seek(LIST head,char name[]) {
pNode p = head;
while(p->next) {
if(strcmp(p->next->name,name) == 0) {
return p;
}
p = p->next;
}
return NULL;
}
int InsertFront(LIST head,char name[]) { // 插在姓名为name的结点前面
pNode newnode,p = Seek(head,name);
if(p) {
newnode = ReadInfor();
newnode->next = p->next;
p->next = newnode;
return 1;
}
else printf("没有找到姓名是:%s的学生!\n",name);
return 0;
}
int InsertRear(LIST head,char name[]) { // 插在姓名为name的结点后面
pNode newnode,p = Seek(head,name);
if(p) {
newnode = ReadInfor();
newnode->next = p->next->next;
p->next->next = newnode;
return 1;
}
else printf("没有找到姓名是:%s的学生!\n",name);
return 0;
}
int Erase(LIST head,char name[]) {
pNode q,p = Seek(head,name);
if(p) {
q = p->next;
p->next = q->next;
free(q);
printf("删除成功。\n");
return 1;
}
else printf("没有找到姓名是:%s的学生!\n",name);
return 0;
}
void Show(LIST head) {
pNode p = head->next;
int i;
while(p) {
printf("%u ",p->id);
printf("%s ",p->name);
for(i = 0; i < 3; ++i)
printf("%g ",p->scores[i]);
printf("\n");
p = p->next;
}
}
int main() {
LIST head = Create(3);
Show(head);
InsertFront(head,"刘灿");
InsertRear(head,"宋立柱");
Erase(head,"赵培龙");
Show(head);
return 0;
}
热心网友
时间:2023-08-03 04:36
给你一个大致的答案,如果看不懂,或者还要修改的地方在跟我说下,我这个只是简单的做个链表的操作。
#include<stdio.h>
#include<stdlib.h>
typedef struct student
{
int num;
float score;
struct student* next;
}stu;
stu* creat(stu* head)
{
stu* p1,* p2;
p1=p2=(stu*)malloc(sizeof(stu));
scanf("%d,%f",&p1->num,&p1->score);
while(p1->num!=0)
{
if(head==NULL)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(stu*)malloc(sizeof(stu));
scanf("%d,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return head;
}
stu* find(stu* head,int m)
{
int n;
stu* p1;
p1=head;
if(head==NULL)
{
printf("该链表为空\n");
exit(-1);
}
if(m==0)
printf("请输入要插入在什么结点之后\n");
else
printf("请输入要删除的结点\n");
scanf("%d",&n);
while(p1->num!=n)
p1=p1->next;
return p1;
}
stu* insert(stu* head)
{
stu* p1,* p2;
p1=(stu*)malloc(sizeof(stu));
printf("请输入要插入的数据\n");
scanf("%d,%f",&p1->num,&p1->score);
p2=find(head,0);
p1->next=p2->next;
p2->next=p1;
return head;
}
stu* delete(stu* head)
{
stu* p1,* p2;
p2=head;
p1=find(head,1);
if(p1==head)
head=head->next;
else
{
while(p2->next!=p1)
p2=p2->next;
p2->next=p1->next;
}
return head;
}
stu* nixu(stu* head)
{
stu* p1,* p2,* p3;
p3=(stu*)malloc(sizeof(stu));
p3=NULL;
p2=p1=head;
if(head==NULL)
{
printf("该链表为空\n");
exit(-1);
}
else
{
while(p1!=NULL)
{
p2=p1->next;
p1->next=p3;
p3=p1;
p1=p2;
}
}
return p3;
}
print(stu* head)
{
printf("打印链表:\n");
while(head!=NULL)
{
printf("%d,%f\n",head->num,head->score);
head=head->next;
}
}
int main()
{
stu* head;
head=NULL;
head=creat(head);
head=insert(head);
head=delete(head);
// head=nixu(head);
print(head);
return 0;
}