用c语言输出个人的学号和姓名,急啊!!!求救了 C语言编一个链表,输出姓名和学号就好...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

#include

#include

#include

#include

typedef struct Node

{

int data;

struct Node *next;

}AN;

int data;

AN *CreList(AN *head);

AN *InsList(AN *head,int data);

AN *DelList(AN *head,int data);

void find(int value,AN *head);

void Des(AN *head);

void display(AN *head);

void main()

{

char j='y';int x;AN *head=NULL;

printf("请创建一个链表,以0结束\n");head=CreList(head);

while(j=='y')

{

j=0;

printf("选择操作:\n1.重新创建链表\n2.插入数据\n3.删除数据\n4.查找某个数\n5.输出链表\n6.删除链表\n");

scanf("%d",&x);

switch(x)

{

case 1:

printf("请输入链表,以0结束\n");head=CreList(head);break;

case 2:

printf("请插入一个数字\n");scanf("%d",&data);head=InsList(head,data);break;

case 3:

printf("删除哪个数?\n");scanf("%d",&data);head=DelList(head,data);break;

case 4:

printf("查找那个数?\n");scanf("%d",&data);find(data,head);break;

case 5:

display(head);break;

case 6:

Des(head);break;

default :0;

}

printf("continue?(y or n)\n");

while(j!='y'&&j!='n')

{

j=getch();

}

system("cls");

}

}

AN *CreList(AN *head)

{

AN *p;

AN *p1=NULL;

p=(AN *)malloc(sizeof(AN));

int x;

scanf("%d",&x);

p->data=x;

p->next=NULL;

head=p;p1=p;

while(x!=0)

{

p=(AN*)malloc(sizeof(AN));

scanf("%d",&x);

if(x==0){free(p);p=NULL;break;}

p->data=x;

p->next=NULL;

p1->next=p;

p1=p;

}

return head;

}

void display(AN *head)

{

AN *p=head;

while(p!=NULL)

{

printf("%d\n",p->data);

p=p->next;

}

}

AN *InsList(AN *head,int data)

{

AN *w,*p=head,*p1=head;

if((w=(AN *)malloc(sizeof(AN)))==NULL)

printf("内存空间申请失败\n");

else

{

w->data=data;

w->next=NULL;

if(w->data<=p->data)

{w->next=head;return w;}

else

{

while(w->data>p->data&&p->next!=NULL)

{

p1=p;

p=p->next;

}

if(w->data>p->data)

{

p1=p;

p=p->next;

}

p1->next=w;

w->next=p;

}

}

return head;

}

AN *DelList(AN *head,int data)

{

AN *p=head,*p1=head;

while(p->data!=data)

{

p1=p;

p=p->next;

if(p==NULL)break;

}

if(p==NULL) printf("表中无此数\n ");

else

if(head->data==p->data)

{

head=head->next;

free(p);

}

else

p1->next=p->next;

p=NULL;

free(p);

return head;

}

void find(int value,AN *head)

{

AN *p=head;

int count=0;

while(p->data!=value&&p->next!=NULL)

{

p=p->next;count++;

}

if(p->next==NULL&&p->data!=value)

printf("链表里无此数\n");

else

printf("是第%d个数\n",count+1);

}

void Des(AN *head)

{

AN *p;

while(head->next!=NULL)

{

p=head;

head=head->next;

free(p);

}

free(head);

printf("链表成功删除");

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的C语言链表示例,用于输出学生的学号姓名、性别、年龄和成绩: ```c #include <stdio.h> #include <stdlib.h> // 定义学生结构体 struct Student { int id; char name[50]; char gender; int age; float score; struct Student* next; }; // 创建链表节点 struct Student* createNode(int id, char name[], char gender, int age, float score) { struct Student* newNode = (struct Student*)malloc(sizeof(struct Student)); newNode->id = id; strcpy(newNode->name, name); newNode->gender = gender; newNode->age = age; newNode->score = score; newNode->next = NULL; return newNode; } // 插入节点到链表尾部 struct Student* insertNode(struct Student* head, int id, char name[], char gender, int age, float score) { struct Student* newNode = createNode(id, name, gender, age, score); if (head == NULL) { head = newNode; } else { struct Student* temp = head; while (temp->next != NULL) { temp = temp->next; } temp->next = newNode; } return head; } // 输出链表中的学生信息 void printStudents(struct Student* head) { struct Student* temp = head; while (temp != NULL) { printf("学号:%d\n", temp->id); printf("姓名:%s\n", temp->name); printf("性别:%c\n", temp->gender); printf("年龄:%d\n", temp->age); printf("成绩:%.2f\n", temp->score); printf("\n"); temp = temp->next; } } int main() { struct Student* head = NULL; int id; char name[50]; char gender; int age; float score; // 插入学生信息 printf("请输入学生信息(输入-1结束):\n"); while (1) { printf("学号:"); scanf("%d", &id); if (id == -1) { break; } printf("姓名:"); scanf("%s", name); printf("性别:"); scanf(" %c", &gender); printf("年龄:"); scanf("%d", &age); printf("成绩:"); scanf("%f", &score); head = insertNode(head, id, name, gender, age, score); } // 输出学生信息 printf("\n学生信息如下:\n"); printStudents(head); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值