线性链表和顺序表的基本操作
一、实验目的:
线掌握线性表的逻辑特性以及在计算机内的两种存储结构,线性链表和顺序表存储结构下基本操作的实现,会灵活应用线性表结构解决某些实际问题。
二、实验内容:
1,线性表顺序存储结下的基本操作的实现(初始化,赋值、取值、插入、删除归并等)。
2,线性表链式存储结下的基本操作的实现(初始化,赋值、取值、插入、删除归并等)。
三、实验要求:
在本题下面提交源程序和实验运行结果截图。
上交纸质版实验报告和电子版实验报告。
四、源程序及注释
1,线性表顺序存储结下的基本操作的实现(初始化,赋值、取值、插入、删除归并等)。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define len sizeof(struct list)
struct list
{
int data;
struct list *next;
};struct list *la,*lb;
struct list * InitList()
{
struct list *head;
head=(struct list *)malloc(len);
return head;
}
struct list * ListInsert(struct list* &la)
{
struct list *p1,*head;
int n;
head=la;
printf("请输入数据的个数\n");
scanf("%d",&n);
printf("请输入数据,用空格分开\n");
while(n--)
{
p1=(struct list*)malloc(len);
scanf("%d",&p1->data);
p1->next=NULL;
la->next=p1;
la=p1;
}
return head;
}
void GetElem(struct list * la)
{
printf("请输入你要取得值的位置\n");
int n;
scanf("%d",&n);
while(la->next&&n)
{
n--;
la=la->next;
}
if(n)
printf("输入的位置超出链表范围\n");
else
printf("所处位置的元素为%d\n",la->data);
}
void InsertElem(struct list * la)
{
struct list* pos;
printf("请输入你要插入的位置和元素\n");
int n,x;
scanf("%d %d",&n,&x);
while(la->next&&n)
{
n--;
pos=la;
la=la->next;
}
if(n)
printf("输入的位置超出链表范围\n");
else
{
struct list * p1;
p1=(struct list*)malloc(len);
p1->data=x;
p1->next=pos->next;
pos->next=p1;
printf("插入成功\n");
}
}
bool UnionList(struct list *pa,struct list *pb)
{
struct list *head;
head=pa;
while(pa->next){
pa=pa