您的位置:首页 > 其它

线性表顺序表模板 纯本人手工创造

2015-12-08 20:40 232 查看
/* ***********************************************
Author        :mubaixu
Created Time  :2015-12-08 20:45:05
File Name     :线性表顺序存储操作
************************************************ */

1 #include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <windows.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define status int
#define elemtype int
typedef struct {
elemtype *elem;

int lenght;
int listsize;
}sqlist;
status initlist(sqlist &l){//构造线性表
l.elem=(elemtype *)malloc(LIST_INIT_SIZE*sizeof(elemtype));
if(!l.elem)
exit(OVERFLOW);
l.lenght=0;//运行过程中的动态长度
l.listsize=LISTINCREMENT;//初始化的静态长度。
return OK;
}

status listinsert(sqlist &l,int i,elemtype e){//在线性表第i个位置插入元素e
if(i<1||i>l.lenght+1)
return ERROR;
elemtype *p=NULL;
elemtype *q=NULL;
elemtype *newbase=NULL;

if(l.lenght>=l.listsize){
newbase=(elemtype *)realloc(l.elem,(LISTINCREMENT+LIST_INIT_SIZE)*sizeof(elemtype));
if(!newbase)
return OVERFLOW;
l.elem=newbase;
l.listsize+=LISTINCREMENT;
}

q=&(l.elem[i-1]);
for(p=&(l.elem[l.lenght-1]);p>=q;p--){
*(p+1)=*p;
}
*q=e;
++l.lenght;
return OK;
}

status listdelete(sqlist &l,int i,elemtype &e){//删除线性表中的第i个元素, 并且返回删除的值为e

if(i<1||i>l.lenght)
return ERROR;

elemtype *p=NULL;
elemtype  *q=NULL;
q=&(l.elem[i-1]);
e=*q;
p=l.elem+l.lenght-1;
for(++q;q<=p;q++){
*(q-1)=*q;
}
--l.lenght;
return OK;
}
void mergelist(sqlist la,sqlist lb,sqlist &lc){
elemtype *pa,*pb,*pc,*pb_last,*pa_last;
//将la和lb按非递减顺序排序后赋值于lc
//eg:  la:3 5 8 11
//      lb:2 6 8 9 11 15 20
//       lc:2 3 5 6 8 8 9 11 11 15 20
pa=la.elem;
pb=lb.elem;
pc=lc.elem;
printf("------\n");
lc.listsize=lc.lenght=la.lenght+lb.lenght;
pc=lc.elem=(elemtype *)malloc(lc.listsize*sizeof(elemtype));
pa_last=la.elem+la.lenght-1;
pb_last=lb.elem+lb.lenght-1;
//   printf("--------->%d  %d %d\n",la.lenght,lb.lenght,lc.lenght);
while(pa<=pa_last&&pb<=pb_last){
if(*pa<=*pb){
*pc++=*pa++;
}
else
*pc++=*pb++;
}

while(pa<=pa_last)
*pc++=*pa++;
while(pb<=pb_last)
*pc++=*pb++;

}
status compare(elemtype x, elemtype y)
{
return x == y;
}
int locateelem(sqlist l,elemtype e,status(*compare)(elemtype,elemtype)){
//在线性表中查找元素e,如果查到了返回其位序,位序值从1开始
//如果没有查到返回0
elemtype i=1;
elemtype *p=l.elem;
while(i<=l.lenght&&!(*compare)(*p++,e))
++i;
if(i<=l.lenght)
return i;
else
return 0;
}

void Union(sqlist &la,sqlist lb){
// 将所有在线性表lb中但是不在la中的元素插入到线性表la的后面
elemtype la_len,lb_len;
la_len=la.lenght;
lb_len=lb.lenght;
for(int i=1;i<=lb_len;i++){
elemtype tmp=lb.elem[i-1];

if(!locateelem(la,tmp,compare))
listinsert(la,++la_len,tmp);
}
la.lenght=la_len;
}

int main(){
//构造基本顺序表并按照逆序顺序输出主函数
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
sqlist l;
initlist(l);
elemtype e,i;
i=0;
while(~scanf("%d",&e)){
i++;
listinsert(l,i,e);
}
printf("%d\n",l.lenght);
for( i=l.lenght-1;i>=0;i--)//逆序输出
printf("%d",l.elem[i]);
for( i=0;i<l.lenght;i++)//正序输出
printf("%d",l.elem[i]);
//删除元素
//如果想要删除第pos个元素
elemtype pos;
scanf("%d",&pos);
listdelete(l,pos,e);
printf("%d\n",l.lenght);
for(i=0;i<l.lenght;i++)
printf("%d ",l.elem[i]);
puts("");
//查找某个元素是否在线性表l中
//如果在返回其相对位序,如果不在,返回值为0
elemtype x;
scanf("%d",&x);
elemtype ans=locateelem(l,x,compare);
printf("%d\n",ans);
return 0;
}

int main(){
//分别输入两个线性表的序列,构造两个线性表
//后将两个表合并按顺序输出
//    page20算法2.2实现  &&   page26算法2.7
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
sqlist l1,l2,l3;
initlist(l1),initlist(l2),initlist(l3);
elemtype e,i,cnt;
cnt=0;
elemtype n1,n2;
scanf("%d",&n1);
for( i=0;i<n1;i++){
scanf("%d",&e);
cnt++;
listinsert(l1,cnt,e);
}
scanf("%d",&n2);
cnt=0;
for( i=0;i<n2;i++){
scanf("%d",&e);
cnt++;
listinsert(l2,cnt,e);
}
//printf("--------->%d  %d %d\n",l1.lenght,l2.lenght,l3.lenght);
mergelist(l1,l2,l3);
printf("%d\n",l3.lenght);//输出合并后的线性表长度
for( i=0;i<l3.lenght;i++){
printf("%d ",l3.elem[i]);//按顺序输出合并后的表的序列
}
puts("");
return 0;
}

int main(){
//构造两个线性表,将两个表合并至第一个表中,要求将第二个表中不在第一个表中的元素
//放置在第一个表中的后面
//page20   此主函数和Union函数相对应
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
sqlist l1,l2;
initlist(l1),initlist(l2);
elemtype e,i,cnt;
cnt=0;
elemtype n1,n2;
scanf("%d",&n1);
for( i=0;i<n1;i++){
scanf("%d",&e);
cnt++;
listinsert(l1,cnt,e);
}
scanf("%d",&n2);
cnt=0;
for( i=0;i<n2;i++){
scanf("%d",&e);
cnt++;
listinsert(l2,cnt,e);
}

Union(l1,l2);
printf("%d\n",l1.lenght);
for(int i=0;i<l1.lenght;i++)
printf("%d ",l1.elem[i]);
puts("");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: