7-1 选民投票
编程统计候选人的得票数。有若干位候选人(n<=10),候选人姓名从键盘输入(候选人姓名不区分大小写,姓名最长为9个字节),若干位选民,选民每次输入一个得票的候选人的名字(姓名最长为9个字节),若选民输错候选人姓名,则按废票处理。程序自动统计各候选人的得票结果,并按照得票数由高到低的顺序排序。最后输出各选票人得票结果和废票信息。
输入格式:
先输入候选人人数n和n名候选人姓名,再输入选民人数m和m位选民的选票。
输出格式:
先根据选票结果由高到低输出各候选人得票结果,再根据废票情况输出废票信息(换行后,输出提示信息“invalid vote:”,再输出废票信息)。
输入样例1:
3
zhang
li
wang
9
Wang
Zhang
zhuang
LI
Liao
ZHANG
WANG
Wang
wang
输出样例1:
wang:4
zhang:2
li:1
invalid vote:
zhuang
Liao
输入样例2:
2
liu
yang
5
Liu
liu
YANG
yang
Liu
输出样例2:
liu:3
yang:2
代码:
#include <iostream>
#include <string>
using namespace std;
struct p
{
string name;
int num;
};
int main()
{
int n, i, j, k, m, cnt = 0, flag = 0;
string s, er[10];
cin >> n;
p a[n], temp;
for (i = 0; i < n; i++)
{
cin >> a[i].name;
a[i].num = 0;
}
cin >> m;
for (i = 0; i < m; i++)
{
cin >> s;
int l = s.length();
string ss = s;
for (j = 0; j < l; j++)
if (ss[j] >= 'A' && ss[j] <= 'Z')
ss[j] += 32;
for (k = 0; k < n; k++)
{
flag = 1;
if (ss == a[k].name)
{
a[k].num++;
flag = 0;
break;
}
}
if (flag)
{
er[cnt++] = s;
}
}
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - 1 - i; j++)
{
if (a[j].num < a[j + 1].num)
{
temp = a[j + 1];
a[j + 1] = a[j];
a[j] = temp;
}
}
for (i = 0; i < n; i++)
{
cout << a[i].name << ":" << a[i].num << endl;
}
if (cnt)
{
cout << endl ;
cout << "invalid vote:" << endl;
for (k = 0; k < cnt; k++)
{
cout << er[k] << endl;
}
}
return 0;
}
7-2 有理数均值
本题要求编写程序,计算N个有理数的平均值。
输入格式:
输入第一行给出正整数N(≤100);第二行中按照a1/b1 a2/b2 …
的格式给出N个分数形式的有理数,其中分子和分母全是整形范围内的整数;如果是负数,则负号一定出现在最前面。
输出格式:
在一行中按照a/b
的格式输出N个有理数的平均值。注意必须是该有理数的最简分数形式,若分母为1,则只输出分子。
输入样例1:
4
1/2 1/6 3/6 -5/10
输出样例1:
1/6
输入样例2:
2
4/3 2/3
输出样例2:
1
代码:
#include<iostream>
#include<cstdio>
#include<utility>
#define P pair<int,int>
using namespace std;
//int gcd(int a,int b)
//{
//return !b ? a:gcd(b,a%b);
//}
int gcd(int a,int b)
{
if (b == 0) return a;
int r = a % b;
return gcd(b,r);
}
int main()
{
int n,i,j;
cin>>n;
P p1,p2;
scanf("%d/%d",&p1.first,&p1.second);
for(i = 1;i < n;i++)
{
scanf("%d/%d",&p2.first,&p2.second);
p1.first = p1.first*p2.second + p2.first*p1.second;
p1.second=p1.second*p2.second;
}
if(p1.first % n == 0)
p1.first /= n;
else
p1.second *= n;
if(p1.first == 0)
{
cout<<0;
return 0;
}
int k = gcd(p1.first,p1.second);
p1.first /= k;
p1.second /= k;
if(p1.second == 1)
cout<<p1.first;
else
cout<<p1.first<<"/"<<p1.second;
return 0;
}
7-3 统计期末成绩
程序设计校选课期末计划安排在第十五周周五下午,有学生反映第十五周周末恰逢端午节假期,已请假提前回家。
经过协商,该课程期末考试共进行两次,分别在第十五周周五下午和第十六周周五下午。
如果学生两次考试都参加,则该课程期末成绩取两次成绩最高分。如果只参加了一次,则该次成绩即为期末考试成绩。
请你帮老师统计下每个学生该门课程的期末成绩。
输入格式:
输入在一行中给出正整数N(≤10)。
随后N行,每行给出一位学生的信息,格式为“学号 姓名 第一次成绩 第二次成绩”,中间以空格分隔。
其中学号为由15个数字组成的字符串,姓名为长度不超过20的不包含空白字符的非空字符串,
成绩为[0,100]区间内的整数,如果该次考试没参加,成绩为0。
输出格式:
按照学号递增次序,每行输出一个学生的学号、姓名和期末成绩,间隔一个空格。
输入样例:
3
201410300106 wanyun 70 88
201618050322 zhangyu 90 0
201509210118 lixiao 0 0
输出样例:
201410300106 wanyun 88
201509210118 lixiao 0
201618050322 zhangyu 90
代码:
#include<iostream>
#include<string>
using namespace std;
struct stu
{
string num;
string name;
int score;
};
int main()
{
int n,i,j,s1,s2;
struct stu s[n],temp;
cin>>n;
for(i = 0;i<n;i++)
{
cin>>s[i].num>>s[i].name>>s1>>s2;
if(s1==s2&&s1==0&&s2==0)
s[i].score = 0;
else
{
if(s1 > s2)
s[i].score = s1;
else
s[i].score = s2;
}
}
for(i = 0;i < n-1;i++)
for(j = 0;j < n-1-i;j++)
{
if(s[j].num > s[j+1].num)
{
temp = s[j+1];
s[j+1] = s[j];
s[j] = temp;
}
}
for(i = 0;i < n;i++)
cout<<s[i].num<<' '<<s[i].name<<' '<<s[i].score<<endl;
return 0;
}
7-4 课程设计排名统计
数据结构课程设计以小组为单位进行答辩,每个小组有一个唯一正整数表示的的编号和四个成员。
答辩成绩采用排名制,答辩结束后每个小组都提交一份答辩成绩表,该成绩表按照排名从高到低的顺序列出所有小组编号。
例如,小组编号最大值为5,2号小组提交的成绩表数据为{5 1 2 4 3 },则5号小组排名第一,1号小组排名第2,以此类推。
每个小组最终答辩成绩排名按照所有组所提交排名之和重新排序,和越小排名越高,名次从高到低分别为1,2,3,...。
请你帮老师统计下每个小组的最终排名。
输入格式:
输入在一行中给出正整数N(N≤10),为小组个数。
随后N行,按照小组编号i(1≤i≤N)递增顺序每行首先给出第i个小组四个成员的姓名,中间以空格分隔,
然后给出一个小组提交的答辩成绩表,由N个正整数组成,中间以空格分隔,分别对应排名从高到低的小组编号。
其中姓名为长度不超过20的不包含空白字符的非空字符串。
输出格式:
按照最终排名非递增次序,每行先给出名次,然后按照输入顺序输出对应小组四个成员的姓名,中间以一个空格分隔。
如果小组最终排名有相同,则再按照小组编号递增顺序输出。
输入样例:
5
xiaozhao zhangwuji zhaomin zhouzhiruo 2 3 1 5 4
huangrong guojing guofu guoxiang 2 3 5 1 4
yangguo xiaolongyu limochou laowantong 3 2 1 4 5
yangkang monianci ouyangxiu zhoubotong 2 3 1 5 4
yuanchengzhi qingqing wenyi gongzhu 3 2 1 4 5
输出样例:
1 huangrong guojing guofu guoxiang
2 yangguo xiaolongyu limochou laowantong
3 xiaozhao zhangwuji zhaomin zhouzhiruo
4 yuanchengzhi qingqing wenyi gongzhu
5 yangkang monianci ouyangxiu zhoubotong
代码:
#include<iostream>
#include<string>
using namespace std;
int n;
struct stu
{
string name[4];
int pm[11];
int flag = 0;
}a[11];
int main()
{
int i,j,k;
cin>>n;
struct stu temp;
for(i = 1;i <= n;i++)
{
for(j = 0;j < 4;j++)
{
cin>>a[i].name[j];
}
for(j = 1;j <= n;j++)
{
cin>>a[i].pm[j];
}
}
for(i = 1;i <= n;i++)
{
for(j = 1;j <= n;j++)
for(k = 1;k <= n;k++)
{
if(a[j].pm[k] == i)
a[i].flag += k;
}
}
for (i = 1; i < n; i++)
{
for (j = 1; j < n ; j++)
{
if (a[j].flag > a[j + 1].flag)
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
for(i = 1;i <= n;i++)
{
cout<<i<<' ';
for(j = 0;j < 4;j++)
{
if(j!=3)
cout<<a[i].name[j]<<' ';
else
cout<<a[i].name[3];
}
cout<<endl;
}
return 0;
}
7-5 分配补题题集2
期末考试前C语言组织了一次测试,统计出了每个学生的成绩(整数),并计算了全班的平均分AVE(整数)。创建了4个补题题集,编号分别为1,2,3,4,题目数依次递减。根据测试成绩分配每个题集学生名单,测试成绩位于区间[AVE+10,100],[AVE,AVE+10),[AVE-10,AVE),[0,AVE-10)分别对应题集1,2,3,4。请你编程帮助老师实现题集分配。
输入格式:
输入在一行中给出正整数N(≤10)。
随后N行,每行给出一位学生的信息,格式为“学号 姓名 测试成绩”,中间以空格分隔。其中学号由12个数字组成的字符串、姓名为长度小于20的不包含空白字符的非空字符串、测试成绩为[0,100]区间内的整数。
输出格式:
首先在一行中输出全部学生测试成绩的平均分(整数),接下来一行输出测试成绩低于平均分的人数,然后按照输入顺序,每行输出一位测试成绩低于平均分的学生的学号、姓名、测试成绩、分配的题集编号,间隔一个空格。
输入样例:
3
201410300106 wanyun 70
201618050322 zhangyu 59
201509210118 lixiao 80
输出样例:
69
1
201618050322 zhangyu 59 3
代码:
#include<iostream>
#include<string>
using namespace std;
typedef struct Stu
{
string num;
string name;
int score;
int grade = 0;
int flag = 0;
}stu;
int main()
{
int n,i,sum = 0,cnt = 0;
cin>>n;
stu s[n];
for(i = 0;i < n;i++)
{
cin>>s[i].num>>s[i].name>>s[i].score;
sum += s[i].score;
}
int ave = sum/n;
for(i = 0;i < n;i++)
{
if(s[i].score < ave-10 && s[i].score >= 0)
{
s[i].grade =4;
s[i].flag = 1;
cnt++;
}
else if(s[i].score >= ave-10&&s[i].score < ave)
{
s[i].grade =3;
s[i].flag = 1;
cnt++;
}
}
cout<<ave<<endl<<cnt<<endl;
if(cnt)
{
for(i = 0;i < n;i++)
{
if(s[i].flag == 1)
cout<<s[i].num<<' '<<s[i].name<<' '<<s[i].score<<' '<<s[i].grade<<endl;
}
}
return 0;
}
7-6 成绩大于等于某值的学生信息输出
输入若干个学生信息(包括学号、姓名和成绩),输入学号为0时输入结束,建立一个单向链表,再输入一个成绩值,将成绩大于等于该值的学生信息输出。
提示:
定义函数struct stud_node *Creat_Stu_Doc()完成创建链表
定义函数struct stud_node *DeleteDoc(struct stud_node *head,int min_score)将分数低于min_score的结点删除
定义函数void Ptrint_Stu_Doc(struct stud_node *head)打印链表
输入输出示例:括号内为说明,无需输入输出
输入样例:
1 zhang 78
2 wang 80
3 li 75
4 zhao 85
0
80
输出样例:
2 wang 80
4 zhao 85
代码:
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<string>
using namespace std;
struct stud_node
{
int num;
char name[20];
int score;
struct stud_node *next;
};
struct stud_node *Creat_Stu_Doc()
{
struct stud_node *p, *q, *head=NULL;
while(1)
{
p = (struct stud_node *)malloc(sizeof(struct stud_node));
cin>>p->num;
if(p->num == 0)
break;
//scanf("%s %d", p->name, &p->score);
cin>>p->name>>p->score;
p->next = NULL;
if(head == NULL)
head = p;
else
q->next = p;
q = p;
}
return head;
}
struct stud_node *DeleteDoc(struct stud_node *head,int min_score)//链表的删除
{
struct stud_node *p,*q;
while(head!=NULL && head->score < min_score)//先删除头结点满足的情况
{
p = head;
head = head->next;
free(p);
}
if(head == NULL)//判断头结点是否为空,如果为空则链表为空
return NULL;
p = head;
q = head->next;
while(q != NULL)//删除除过头结点之外的可删的结点
{
if(q->score < min_score)
{
p->next=q->next;
free(q);
}
else
p = q;
q = p->next;
}
return head;
}
void Ptrint_Stu_Doc(struct stud_node *head)
{
while(head != NULL)
{
printf("%d %s %d\n", head->num, head->name, head->score);
head = head->next;
}
}
int main()
{
struct stud_node *head;
int number;
head = Creat_Stu_Doc();
scanf("%d", &number);
head = DeleteDoc(head, number);
Ptrint_Stu_Doc(head);
return 0;
}
7-7 单向链表1
链表节点定义为:
struct Node{
int data;
struct Node *next;
}
编程实现:输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算:
输入若干个正整数(输入-1为结束标志),建立一个单向链表,将其中的奇数值结点删除后输出
输入输出示例:括号内为说明
输入样例:
2 (repeat=2)
1 2 3 4 5 6 7 -1
1 3 5 -1
输出样例:
2 4 6
代码:
#include <iostream>
#include <cstdlib>
using namespace std;
struct num
{
int data;
struct num *next;
};
struct num *creat()
{
int dt;
struct num *head = NULL, *p, *q;
cin >> dt;
while (dt != -1)
{
p = (struct num *)malloc(sizeof(struct num));
p->data = dt;
p->next = NULL;
if (head == NULL)
head = p;
else
q->next = p;
q = p;
cin >> dt;
}
return head;
}
struct num *Delete (struct num *head)
{
struct num *p, *q;
while (head != NULL && head->data % 2 == 1) //先删除满足删除条件的头节点
{
p = head;
head = head->next;
free(p);
}
if (head == NULL)
return NULL;
p = head;
q = head->next;
while (q != NULL)
{
if (q->data % 2 == 1)
{
p->next = q->next;
free(q);
}
else
p = q;
q = p->next;
}
return head;
}
int main()
{
int r, i, j;
cin >> r;
for (i = 0; i < r; i++)
{
struct num *head = NULL, *p;
head = creat();
head = Delete (head);
for (p = head; p != NULL; p = p->next)
{
if (p->next != NULL)
cout << p->data << ' ';
else
cout << p->data << endl;
}
}
}
7-8 单向链表3
编程实现:输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算:
输入一个正整数 n(0<n<=9)和一组(n个)升序的整数,建立单向链表,再输入一个整数 x,把 x 插入到这组数据中,使该组数据仍然有序。
输入输出示例:括号内为说明
输入样例:
4 (repeat=4)
5 (数据的个数n=5)
1 2 4 5 7 (5个有序整数)
3 (待插入整数x=3)
4 (数据的个数n=4)
1 2 5 7 (4个有序整数)
-10 (待插入整数x=-10)
3 (数据的个数n=3)
1 2 4 (3个有序整数)
100 (待插入整数x=100)
5 (数据的个数n=5)
1 2 4 5 7 (5个有序整数)
4 (待插入整数x=4)
输出样例:
size=6:1 2 3 4 5 7
size=5:-10 1 2 5 7
size=4:1 2 4 100
size=6:1 2 4 4 5 7
代码:
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
node* next;
};
struct node *create(int n)
{
struct node *p,*head = NULL,*q;
while(n--)
{
p = (struct node *)malloc(sizeof(struct node));
cin>>p->data;
p->next = NULL;
if(head == NULL)
head = p;
else
q->next = p;
q = p;
}
return head;
}
struct node *add(struct node *head,int num)
{
struct node *p,*q;
q = (struct node*)malloc(sizeof(struct node));
if(head == NULL)
return NULL;
for(p = head;p != NULL;p = p->next)
{
if(num <= head->data)//头节点插入
{
q->next = head;
q->data = num;
head = q;
break;
}
if(p->next == NULL && p->data <= num)
{
p->next = q;
q->data = num;
break;
}
if(p->data <= num && p->next->data >= num)
{
q->next = p->next;
p->next = q;
q->data = num;
break;
}
}
return head;
}
int main()
{
int r,i;
cin>>r;
for(i = 0;i < r;i++)
{
int n,num;
struct node *head,*p;
cin>>n;
head = create(n);
cin>>num;
head = add(head,num);
cout<<"size="<<n+1<<':';
for(p = head;p != NULL;p = p->next)
{
if(p->next != NULL)
cout<<p->data<<' ';
else
{
if(i!=r-1)
cout<<p->data<<endl;
else
cout<<p->data;
}
}
}
return 0;
}
7-9 单向链表5
定义单向链表struct Node并实现:输入若干个学生信息(包括学号、姓名和成绩),输入学号为0时输入结束,再输入一个成绩值,将成绩小于该值的学生信息删除,并将成绩大于等于该值的学生信息输出。
输入输出示例:括号内为说明
输入样例:
1 zhang 78
2 wang 80
3 li 75
4 zhao 85
0
80
输出样例:
2 wang 80
4 zhao 85
代码:
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<string>
using namespace std;
struct stud_node
{
int num;
char name[20];
int score;
struct stud_node *next;
};
struct stud_node *Creat_Stu_Doc()
{
struct stud_node *p, *q, *head=NULL;
while(1)
{
p = (struct stud_node *)malloc(sizeof(struct stud_node));
cin>>p->num;
if(p->num == 0)
break;
//scanf("%s %d", p->name, &p->score);
cin>>p->name>>p->score;
p->next = NULL;
if(head == NULL)
head = p;
else
q->next = p;
q = p;
}
return head;
}
struct stud_node *DeleteDoc(struct stud_node *head,int min_score)//链表的删除
{
struct stud_node *p,*q;
while(head!=NULL && head->score < min_score)//先删除头结点满足的情况
{
p = head;
head = head->next;
free(p);
}
if(head == NULL)//判断头结点是否为空,如果为空则链表为空
return NULL;
p = head;
q = head->next;
while(q != NULL)//删除除过头结点之外的可删的结点
{
if(q->score < min_score)
{
p->next=q->next;
free(q);
}
else
p = q;
q = p->next;
}
return head;
}
void Ptrint_Stu_Doc(struct stud_node *head)
{
while(head != NULL)
{
printf("%d %s %d\n", head->num, head->name, head->score);
head = head->next;
}
}
int main()
{
struct stud_node *head;
int number;
head = Creat_Stu_Doc();
scanf("%d", &number);
head = DeleteDoc(head, number);
Ptrint_Stu_Doc(head);
return 0;
}
7-10 链表也简单final
将学生成绩绩点组成一个链表。链表结构如下:
struct student {
string name; //学生姓名
double gpa; //绩点
student *next;
};
输入是一组学生的姓名和绩点,以链表形式存储。 删除绩点小于平均绩点的学生结点,成为一个新链表。 后按照输入的顺序,依序输出新链表的学生信息。平均绩点是输入的所有学生绩点取算术平均值。
输入格式:
输入包括若干行。 每行是一个学生的 姓名和绩点,以空格隔开。
最后一行是-1。
输出格式:
输出包括学生姓名。 每个学生姓名一行。
输入样例:
zhang 3.5
liu 2.1
tie 1.9
-1
输出样例:
zhang
代码:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
struct stu
{
char name[20]; //学生姓名
double gpa; //绩点
stu *next;
};
struct stu *create()
{
struct stu *head = NULL, *p, *q;
while (1)
{
p = (struct stu *)malloc(sizeof(struct stu));
cin >> p->name;
if (strcmp(p->name, "-1") == 0)
break;
cin >> p->gpa;
p->next = NULL;
if (head == NULL)
head = p;
else
q->next = p;
q = p;
}
return head;
}
struct stu *Delete(struct stu *head, int ave)
{
struct stu *p, *q;
while (head != NULL && head->gpa < ave) //先删除头结点满足的情况
{
p = head;
head = head->next;
free(p);
}
if (head == NULL)
return NULL;
p = head;
q = head->next;
while (q != NULL)
{
if (q->gpa < ave)
{
p->next = q->next;
free(q);
}
else
p = q;
q = p->next;
}
return head;
}
int main()
{
struct stu *head = NULL, *p;
head = create();
double x = 0, n = 0;
for (p = head; p != NULL; p = p->next, n++)
{
x += p->gpa;
}
x = x / n;
head = Delete(head, x);
for (p = head; p != NULL; p = p->next)
{
if (p->next != NULL)
cout << p->name << endl;
else
cout << p->name;
}
return 0;
}
7-11 单链表基本操作
请编写程序实现单链表插入、删除结点等基本算法。给定一个单链表和一系列插入、删除结点的操作序列,输出实施上述操作后的链表。单链表数据域值为整数。
输入格式:
输入第1行为1个正整数n,表示当前单链表长度;第2行为n个空格间隔的整数,为该链表n个元素的数据域值。第3行为1个正整数m,表示对该链表施加的操作数量;接下来m行,每行表示一个操作,为2个或3个整数,格式为0 k d或1 k。0 k d表示在链表第k个结点后插入一个数据域值为d的结点,若k=0则表示表头插入。1 k表示删除链表中第k个结点,此时k不能为0。注:操作序列中若含有不合法的操作(如在长度为5的链表中删除第8个结点、删除第0个结点等),则忽略该操作。n和m不超过100000。
输出格式:
输出为一行整数,表示实施上述m个操作后的链表,每个整数后一个空格。输入数据保证结果链表不空。
输入样例:
5
1 2 3 4 5
5
0 2 8
0 9 6
0 0 7
1 0
1 6
输出样例:
7 1 2 8 3 5
代码:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
struct s
{
int num;
s *next;
};
struct s *create(int n)
{
struct s *head = NULL, *p, *q;
while (n--)
{
p = (struct s *)malloc(sizeof(struct s));
cin >> p->num;
p->next = NULL;
if (head == NULL)
head = p;
else
q->next = p;
q = p;
}
return head;
}
struct s *add(struct s *head, int k, int d)
{
struct s *temp = (struct s *)malloc(sizeof(struct s));
if (k == 0)
{
temp->num = d;
temp->next = head;
head = temp;
}
else
{
struct s *p = head;
while (k - 1 > 0)
{
p = p->next;
k--;
}
temp->next = p->next;
p->next = temp;
temp->num = d;
}
return head;
}
struct s *Delete(struct s *head, int k)
{
struct s *p = head, *q;
int bj = 1;
while (p != NULL && bj < k - 1)
{
p = p->next;
bj++;
}
q = p;
p = p->next;
if (p->next != NULL)
{
q->next = p->next;
free(p);
}
else
{
q->next = NULL;
free(p);
}
return head;
}
int main()
{
struct s *head = NULL, *p;
int n, m, i;
cin >> n;
head = create(n);
cin >> m;
for (i = 0; i < m; i++)
{
int f, k, d;
cin >> f;
if (f == 0)
{
cin >> k >> d;
if (k >= 0 && k <= n)
{
head = add(head, k, d);
n++;
}
continue;
}
else if (f == 1)
{
cin >> k;
if (k > 0 && k <= n)
{
head = Delete(head, k);
n--;
}
continue;
}
}
for (p = head; p != NULL; p = p->next)
cout << p->num << ' ';
return 0;
}
7-12 链表操作-插入、查找和删除
请编写创建链表和输出链表的函数。对于以下数据结点的结构定义,
针对带头结点的链表,请编程完成以下功能。
struct LNode{
int data; //数据域
struct LNode *next; //指针域
};
struct LNode *head; //头指针
输入数据包含若干组命令和数据,一组数据中的第1个字符代表命令,
接下来的是该命令需要的数据。
(1)如果命令是I,功能为创建空链表,对应函数:void List_Init(head);
(2)如果命令是A,后跟一个整数data,功能为向链表尾部追加一个数据data,
对应函数:void List_Append(head,data);
(3)如果命令是C,后跟一个整数N,再跟N个整数,功能为向链表尾部追加N个
数据,可通过调用List_Append()函数实现;
(4)如果命令是P,功能遍历输出链表中所有数据,数据间用一个空格分隔,对应
函数:void List_print(head),如果链表未建立,输出“List not defined!”,
如果链表为空输出:“List is empty!”。
以上为上一题目的内容,在此基础上,增加设计如下功能:
(5)如果命令是N,后跟一个整数n和d,功能为向链表的第n个位置插入数据d,
可通过调用List_Insert(head,n,d)函数实现;
(6)如果命令是F,后跟一个整数d,功能为在链表查找数据d,返回其位序,若
找不到返回-1。可通过调用List_Find(head,d)函数实现;
(7)如果命令是D,后跟一个整数n,功能为删除链表第n个位置的数据,可通过
调用List_Delete(head,n)函数实现。
输入格式:
若干组命令和数据,很多命令和数据写在一起请注意识别。
输出格式:
根据输入命令输出相应内容,详见输出样例。
输入样例:
I C 5 100 200 300 400 500 P
F 500 N 3 23 N 5 31 P
D 4 P F 99
A 6 P
输出样例:
100 200 300 400 500
index:5
100 200 23 300 31 400 500
100 200 23 31 400 500
Not Found!
100 200 23 31 400 500 6
输入样例:
I A 100 A 200 A 300 C 4 400 500 600 700 P
F 800 F 500
N 4 4 N 5 5 P
D 2 D 2 D 2 P
D 2 D 2 D 2 P
D 1 D 1 P
D 1 D 1 P
输出样例:
100 200 300 400 500 600 700
Not Found!
index:5
100 200 300 4 5 400 500 600 700
100 5 400 500 600 700
100 600 700
700
List is empty!
代码:
#include <iostream>
#include <cstdio>
using namespace std;
struct LNode
{
int data;
struct LNode *next;
};
struct LNode *head = NULL;
struct LNode *List_Init(struct LNode *head)//新建一个空链表
{
head = new LNode;
head->next = nullptr;
head->data = 0;
return head;
}
void List_Append(struct LNode *head, int data) //向链表的尾部追加数据data
{
struct LNode *p = head, *q;
q = new LNode;
q->data = data;
q->next = nullptr;
while (p->next != NULL)
p = p->next;
p->next = q;
}
void List_print(struct LNode *head)
{
struct LNode *p ;
if (head == NULL)
{
cout << "List not defined!" << endl;
return ;
}
if (head->next == NULL)
{
cout << "List is empty!" << endl;
return ;
}
for (p = head->next; p != NULL; p = p->next) //head没有data
{
if (p->next != NULL)
cout << p->data << ' ';
else
cout << p->data<<endl;
}
}
void List_Insert(struct LNode *head, int n, int d)
{
struct LNode *p = head, *q;
q = new struct LNode;
q->data = d;
q->next = nullptr;
for (int i = 1; i < n; i++)
p = p->next;
q->next = p->next;
p->next = q;
}
int List_Find(struct LNode *head, int d)
{
int bj = 1;
struct LNode *p = head->next;
while (p != NULL)
{
if (p->data == d)
return bj;
p = p->next;
bj++;
}
return -1;
}
void List_Delete(struct LNode *head, int n)
{
struct LNode *p = head, *q;
int bj = 1;
for(bj = 1;bj < n;bj++)
if(p != NULL)
p=p->next;
if(p==NULL || p->next==NULL)
return ;
/*q = p;
p = p->next;
if (p->next != NULL)
{
q->next = p->next;
free(p);
}
else
{
q->next = NULL;
free(p);
}*/
q=p->next;
p->next=p->next->next;
free(q);
}
int main()
{
char c;
int i, n, N, data;
while (scanf("%c", &c) != EOF)
{
if (c == 'I')
head = List_Init(head);
if (c == 'A')
{
cin >> data;
List_Append(head, data);
}
if (c == 'C')
{
cin >> N;
for (i = 0; i < N; i++)
{
cin >> data;
List_Append(head, data);
}
}
if (c == 'P')
List_print(head);
if (c == 'N')
{
cin >> n >> data;
List_Insert(head, n, data);
}
if (c == 'F')
{
cin >> data;
if (List_Find(head, data) == -1)
cout << "Not Found!" << endl;
else
cout << "index:" << List_Find(head, data)<<endl;
}
if (c == 'D')
{
cin >> n;
List_Delete(head, n);
}
}
return 0;
}