1、 线性表的存储及操作
背景知识:线性表的顺序存储结构和链式存储结构,及其操作的实现
目的要求:
- 掌握顺序存储结构和链式存储结构的特点;
- 掌握常见算法。
实验内容:
已知两个按元素值有序的线性表A和B,编程实现:将A和B有序归并成一个按元素值有序的线性表,然后删除值相同的元素。
实验步骤: - 从键盘输入两个按元素值有序的线性表A和B的值;
- 根据输入把数据元素分别以顺序存储结构和线性链表存储;
- 有序归并成一个新的按元素值有序的线性表C;
- 输出显示合并后的线性表C;
- 分别在顺序存储结构和线性链表存储结构上删除值相同的元素,并显示删除后的线性表。
测试数据:A=(3,5,8,11),B=(2,6,8,9,11,15,20)
代码
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
int read() {int x; cin >> x; return x;}
struct vector
{
int siz = 0;
int *a = 0;
void push_back(int x) {
siz++;
a = (int *)realloc(a,siz*sizeof(int));
a[siz-1] = x;
}
int size() {return siz;}
void resize(int x) {
a = (int *)realloc(a,x*sizeof(int));
siz = x;
}
int & operator [] (int idx){ return a[idx];}
void clear(){
free(a);
siz = 0;
}
};
void merge(vector & a, vector &b, vector &c)
{
int i,j,kase = 0;
i = j = 0;
while(i < a.size() && j < b.size()){
if(a[i] < b[j]) {
c.push_back(a[i]);
i++;
}
else {
c.push_back(b[j]);
j++;
}
}
while(i < a.size()) c.push_back(a[i++]);
while(j < b.size()) c.push_back(b[j++]);
}
void Delete(vector & c)
{
int i = 0,j = 1;
while(j < c.size()){
if(c[i] != c[j]) c[++i] = c[j];
j++;
}
c.resize(i+1);
}
#define newnode (node *) malloc (sizeof(node))
struct node
{
int data;
node *next;
};
void creat(node *&head,vector &v)
{
node *p = head = newnode;
for(int i = 0; i < v.size(); i++){
node *q = newnode;
q -> data = v[i]; q -> next = NULL;
p -> next = q; p = q;
}
}
node * merge(node *head1, node *head2)
{
if(head1 -> next == NULL) {free(head1); return head2;}
if(head2 -> next == NULL) {free(head2); return head1;}
node *head3,*p,*q,*r;
if(head1 -> next -> data < head2 -> next -> data){
head3 = head1;
p = head1 -> next -> next;
q = head2 -> next;
free(head2);
}
else {
head3 = head2;
p = head1 -> next;
q = head2 -> next -> next;
free(head1);
}
r = head3 -> next;
while(p && q) {
if(p -> data < q -> data){
r -> next = p;
r = p;
p = p -> next;
}
else {
r -> next = q;
r = q;
q = q -> next;
}
}
if(p) r -> next = p;
if(q) r -> next = q;
return head3;
}
void Delete(node * head)
{
if(head -> next==0) return;
node *p = head -> next, *q = p -> next;
while(q){
if(p -> data == q -> data) {
node *tmp = q;
q = q-> next;
free(tmp);
}
else {
p -> next = q;
p = q;
q = q -> next;
}
}
p -> next = 0;
}
void print(node *head)
{
node *p = head -> next;
while(p){
cout << p -> data << ' ';
p = p -> next;
if(p) printf(" -> ");
}
putchar('\n');
}
int main()
{
vector a,b,c;
printf("输入A元素个数: ");int n = read();
printf("\n按升序输入各元素: ");for(int i = 0; i < n; i++) a.push_back(read());
printf("\n输入B元素个数: ");n = read();
printf("\n按升序输入各元素: ");for(int i = 0; i < n; i++) b.push_back(read());
merge(a,b,c);
puts("\n\n\n顺序存储结构\n\n");
puts("合并后");printf("共 %d 个元素: ",c.size());
for(int i = 0; i < c.size(); i++) cout << c[i] << ' ';
cout << endl << endl;
Delete(c);
puts("删除相同元素后:"); printf("共 %d 个元素: ",c.size());
for(int i = 0; i < c.size(); i++) cout << c[i] << ' ';
puts("\n\n\n--------------------------------------\n\n");
puts("链式存储结构\n\n");
node *head1,*head2;
creat(head1,a);creat(head2,b);
node *head3 = merge(head1,head2);
puts("合并后: "); print(head3);
puts("删除后: "); Delete(head3); print(head3);
return 0;
}
测试: