顺序表
1.两个有序链表序列的合并
已知两个非降序链表序列S1与S2,设计函数构造出S1与S2合并后的新的非降序链表S3。
输入格式:
输入分两行,分别在每行给出由若干个正整数构成的非降序序列,用−1表示序列的结尾(−1不属于这个序列)。数字用空格间隔。
输出格式:
在一行中输出合并后新的非降序链表,数字间用空格分开,结尾不能有多余空格;若新链表为空,输出NULL。
样例:
1 3 5 -1
2 4 6 8 10 -1
结尾无空行
1 2 3 4 5 6 8 10
结尾无空行
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int data;
struct list* next;
}list;
list* Initlist()//初始化
{
list* head = (list*)malloc(sizeof(list));
head->next = NULL;
return head;
}
void Createlist(list* List)//建顺序表
{
list* L = List;
list* p;
int num, flag = 1;
while (flag)
{
scanf("%d", &num);
if (num >= 0)//判断‘-1’
{
p = (list*)malloc(sizeof(list));
p->data = num;
L->next = p;
L = L->next;
}
else
{
flag = 0;
L->next = 0;
}
}
}
void Resolve(list* l1, list* l2, list* l3)//合并
{
list* s1 = l1->next;
list* s2 = l2->next;
list* s3 = l3;
while ((s1 != NULL) && (s2 != NULL))
{
if (s1->data < s2->data)
{
s3->next = s1;
s3 = s3->next;
s1 = s1->next;
}
else
{
s3->next = s2;
s3 = s3->next;
s2 = s2->next;
}
}
if (s1 != NULL)
{
s3->next = s1;
}
if (s2 != NULL)
{
s3->next = s2;
}
}
void Print(list* l)//输出
{
list* p = l->next;
int flag = 1, num = 0;
while (p)
{
num++;
if (flag)
{
printf("%d", p->data);
flag = 0;
}
else
{
printf(" %d", p->data);
}
p = p->next;
}
if (num == 0)
{
printf("NULL");
}
}
int main()
{
list* l1, * l2, * l3;
l1 = Initlist();
l2 = Initlist();
l3 = Initlist();
Createlist(l1);
Createlist(l2);
Resolve(l1, l2, l3);
Print(l3);
return 0;
}
2.顺序表的建立及遍历
读入n值及n个整数,建立顺序表并遍历输出。
输入格式:
读入n及n个整数
输出格式:
输出n个整数,以空格分隔(最后一个数的后面没有空格)。
4
-3 10 20 78
结尾无空行
3 10 20 78
结尾无空行
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
int data;
struct node* next;
}node;
node* Create(int n)
{
node* list, * p, * q;
list = (node*)malloc(sizeof(node));
q = list;
for (int i = 0; i < n; i++)
{
p = (node*)malloc(sizeof(node));
scanf("%d", &p->data);
q->next = p;
q = p;
}
q = list->next;
return q;
}
void Print(node* list)
{
printf("%d", list->data);
list = list->next;
for (; list != NULL; list = list->next)
{
printf(" %d", list->data);
}
printf("\n");
}
int main()
{
int n;
node* list;
scanf("%d", &n);
if (n == 0)
{
return 0;
}
else
{
list = Create(n);
Print(list);
}
}
3.顺序表(删除)
(这题是元素删除,其实位置删除和这个类似,位置删除只不过是在给定位置往后进行移位)
已知一组数据,采用顺序存储结构存储,其中所有的元素为整数。设计一个算法,删除元素值在[x,y]之间的所有元素
输入格式:
输入包含三行数据,第一行是表中元素个数,第二行是顺序表的各个元素,第三行是区间x和y。
输出格式:
删除元素值在[x,y]之间的所有元素后,输出新的顺序表。(最后无空格)
10
55 11 9 15 67 12 18 33 6 22
10 20
结尾无空行
55 9 67 33 6 22
结尾无空行
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node* next;
}node;
void Initlist(node** list)
{
*list = (node*)malloc(sizeof(node));
(*list)->next = NULL;
}
void Create(int n, node* list)
{
node* p, * q;
q = list;
while (n--)
{
p = (node*)malloc(sizeof(node));
scanf("%d", &p->data);
p->next = NULL;
q->next = p;
q = p;
}
}
void Delete(int x, int y,node* list)
{
node* p = list;
for (; p->next != NULL;)
{
if (p->next->data <= y && p->next->data >= x)
{
p->next = p->next->next;
p = list;
continue;
}
p = p->next;
}
}
void Print(node* list)
{
node* p = list;
p = p->next;
printf("%d", p->data);
while (p->next != NULL)
{
printf(" %d", p->next->data);
p = p->next;
}
}
int main()
{
int m;
scanf("%d", &m);
node* list;
Initlist(&list);
Create(m, list);
int x, y;
scanf("%d%d", &x, &y);
Delete(x, y, list);
Print(list);
return 0;
}
4.最大子列和问题
给定K个整数组成的序列{ N1, N2, …, NK },“连续子列”被定义为{ Ni, Ni+1, …, Nj },其中 1≤i≤j≤K。“最大子列和”则被定义为所有连续子列元素的和中最大者。例如给定序列{ -2, 11, -4, 13, -5, -2 },其连续子列{ 11, -4, 13 }有最大的和20。现要求你编写程序,计算给定整数序列的最大子列和。
本题旨在测试各种不同的算法在各种数据情况下的表现。各组测试数据特点如下:
数据1:与样例等价,测试基本正确性;
数据2:102个随机整数;
数据3:103个随机整数;
数据4:104个随机整数;
数据5:105个随机整数;
输入格式:
输入第1行给出正整数K (≤100000);第2行给出K个整数,其间以空格分隔。
输出格式:
在一行中输出最大子列和。如果序列中所有整数皆为负数,则输出0。
6
-2 11 -4 13 -5 -2
结尾无空行
20
结尾无空行
//注意数组要开多大,条件有一个是“10的五次方”!!!!
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int main()
{
int k,s = 0;
scanf("%d", &k);
int a[100001];
for (int i = 0; i < k; i++)
{
scanf("%d", &a[i]);
if (a[i] < 0)
{
s++;
}
}
if (s == k)
{
return 0;
}
else
{
int Max = 0, Thissum = 0;
for (int i = 0; i < k; i++)
{
Thissum += a[i];
if (Thissum < 0)
{
Thissum = 0;
}
if (Thissum > Max)
{
Max = Thissum;
}
}
printf("%d", Max);
}
return 0;
}
5.递增有序顺序表的插入
实验目的:1、掌握线性表的基本知识 2、深入理解、掌握并灵活运用线性表。3、熟练掌握线性表的存储结构及主要运算的实现 已知顺序表L递增有序,将X插入到线性表的适当位置上,保证线性表有序。。
输入格式:
第1行输入顺序表长度,第2行输入递增有序的顺序表,第3行输入要插入的数据元素X。
输出格式:
对每一组输入,在一行中输出插入X后的递增的顺序表。
5
1 3 5 7 9
6
结尾无空行
1,3,5,6,7,9,
结尾无空行
#include<stdio.h>
#include<stdlib.h>
int main() {
int n, i,e,j,t;
int a[100];
scanf("%d", &n);
scanf("%d", &e);
for (i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
a[n]=e;
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for(i=0;i<=n;i++)
{
printf("%d,", a[i]);
}
return 0;
}
6.一元多项式的加法
设计程序求两个一元多项式的和。
输入格式:
输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数。数字间以空格分隔。
输出格式:
输出1行,以指数递降方式输出和多项式非零项的系数和指数(保证不超过整数的表示范围)。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0。
4 3 4 -5 2 6 1 -2 0
3 5 20 -7 4 3 1
结尾无空行
5 20 -4 4 -5 2 9 1 -2 0
结尾无空行
#include<stdio.h>
#include<stdlib.h>
struct node {
int coef;//系数
int exp;//指数
struct node* next;
};
struct node* Read() {
struct node* head, * p, * tail;
int i, n;
head = (struct node*)malloc(sizeof(struct node));
head->next = NULL;
tail = head;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
p = (struct node*)malloc(sizeof(struct node));
scanf("%d%d", &p->coef, &p->exp);
p->next = NULL;
tail->next = p;
tail = p;
}
tail->next = NULL;
return head;
}
struct node* addition(struct node* l1, struct node* l2) {
struct node* t1, * t2, * l3, * p, * head;
t1 = l1->next;
t2 = l2->next;
head = (struct node*)malloc(sizeof(struct node));
head->next = NULL;
l3 = head;
while (t1 && t2) {
p = (struct node*)malloc(sizeof(struct node));
if (t1->exp == t2->exp) {
p->coef = t1->coef + t2->coef;
p->exp = t1->exp;
t1 = t1->next;
t2 = t2->next;
}
else if (t1->exp < t2->exp) {
p->coef = t2->coef;
p->exp = t2->exp;
t2 = t2->next;
}
else if (t1->exp > t2->exp) {
p->coef = t1->coef;
p->exp = t1->exp;
t1 = t1->next;
}
l3->next = p;
l3 = l3->next;
}
if (t1) {
l3->next = t1;
}
else if (t2) {
l3->next = t2;
}
return head;
}
void Print(struct node* list) {
struct node* p = list->next;
int flag = 1;
for (; p; p = p->next) {
if (flag == 0 && p->coef) {
printf(" ");
}
if (p->coef) {
printf("%d %d", p->coef, p->exp);
flag = 0;
}
}
if (flag == 1) {
printf("0 0");
}
printf("\n");
}
int main() {
struct node* l1, * l2, * head;
l1 = Read();
l2 = Read();
head = addition(l1, l2);
Print(head);
return 0;
}