![]() |
#2
wufuzhang2019-08-02 13:20
回复 楼主 F3268724562
![]() #include <stdio.h> #include <stdlib.h> typedef struct student { char name[20]; int score; struct student *next; }stu; //创建链表 struct student *creat(int n) { if (n == 0) { return NULL; } int i; stu *head; stu *note; stu *end; head = end = NULL; for(i = 0; i < n; i++) { note=(stu*)malloc(sizeof(stu)); printf("请输入学生姓名\n"); scanf("%s", note->name); printf("请输入学生分数\n"); scanf("%d", ¬e->score); if(head == NULL) { head = note; } else { end->next = note; } end = note; } end->next = NULL; return head; } //链表基本操作之插入节点 struct student* addnote(stu* head, int m) { int i; stu* p, *r; stu* q; p = r = head; for(i = 0; i < m; i++) { if (i < m -1) p = p->next; else r = p->next; } q=(stu*)malloc(sizeof(stu)); printf("请输入插入节点的学生姓名\n"); scanf("%s", q->name); printf("请输入插入节点的学生成绩\n"); scanf("%d", &q->score); if (m == 0) { head = q; q->next = p; } else { p->next = q; q->next = r; } return head; } //链表基本操作之删除节点 struct student * deletenote(stu * head, int k) { int i; stu * p, * q; p = head; q = NULL; if (k == 0) { head = head->next; return head; } for(i = 0; i < k; i++) { if (i != k - 1) p = p->next; else p->next = p->next->next; } return head; } //链表操作之遍历链表 void outputlist(stu* head) { stu* p; p=head; while(p!=NULL) { printf("姓名:%s\n",p->name); printf("成绩:%d\n",p->score); p=p->next; } return; } int main(int argc, char * argv[]) { int n, m, k; stu * head; printf("请输入要输入学生的个数\n"); scanf("%d", &n); head = creat(n); printf("请输入要增加节点的位置\n"); scanf("%d", &m); while (m > n) { printf("输入节点位置超范围,请重新输入\n"); scanf("%d", &m); } head = addnote(head, m); printf("请输入要删除节点的位置\n"); scanf("%d", &k); while (k > n + 1) { printf("输入节点位置超范围,请重新输入\n"); scanf("%d", &k); } head = deletenote(head, k); outputlist(head); return 0; } |
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef struct student
{
char name[20];
int score;
struct student* next;
}stu;
//创建链表
struct student* creat(int n)
{
int i;
stu* head;
stu* note;
stu* end;
(stu*)malloc(sizeof(stu));
head=end=NULL;
for(i=0;i<n;i++)
{
note=(stu*)malloc(sizeof(stu));
printf("请输入学生姓名\n");
scanf("%s",note->name);
printf("请输入学生分数\n");
scanf("%d",¬e->score);
if(head==NULL)
{
head=note;
}
else
{
end->next=note;
}
end=note;
}
end->next=NULL;
return head;
}
//链表基本操作之插入节点
struct student* addnote(stu* head,int m)
{
int i;
stu* p;
stu* q;
p=head;
for(i=0;i<m;i++)
{
p=p->next;
}
q=(stu*)malloc(sizeof(stu));
printf("请输入插入节点的学生姓名\n");
scanf("%s",p->name);
printf("请输入插入节点的学生成绩\n");
scanf("%d",&p->score);
q->next=p->next;
p->next=q;
return head;
}
//链表基本操作之删除节点
struct student* deletenote(stu* head,int k)
{
int i;
stu* p;
stu* q;
p=q=head;
for(i=1;i<=k;i++)
{
p=p->next;
}
for(i=1;i<=k-1;i++)
{
q=q->next;
}
q->next=p->next;
free(p);
return head;
}
//链表操作之遍历链表
void outputlist(stu* head)
{
stu* p;
p=head;
while(p!=NULL)
{
printf("姓名:%s\n",p->name);
printf("成绩:%d\n",p->score);
p=p->next;
}
return;
}
int main(int argc, char *argv[]) {
int n,m,k;
stu* head;
printf("请输入要输入学生的个数\n");
scanf("%d",&n);
head=creat(n);
printf("请输入要增加节点的位置\n");
scanf("%d",&m);
addnote(head,m);
printf("请输入要删除节点的位置\n");
scanf("%d",&k);
deletenote(head,k);
outputlist(head);
return 0;
}
//求大神执教,错在哪里了