合并线性表(链式存储)
//合并线性表(链式存储)
#include <iostream>
using namespace std;
#include <malloc.h>typedef int Elemtype;typedef struct Node
{Elemtype data;struct Node * next;
}Node,*Ptr; //Ptr 一级指针typedef Ptr * SqListPtr; //SqListPtr 二级指针 typedef enum Status
{success,fail
}Status;//***********************************************************************************************
//调试菜单
void menu ();
//线性表的初始化
Status List_Init(SqListPtr L);
//获得线性表的长度
int List_Size(SqListPtr L);
//录入
Status List_Input(SqListPtr L,Elemtype elem);
//插入
Status List_Insert(SqListPtr L,int pos,Elemtype elem);
//定位
Status Locate (SqListPtr L,Elemtype elem,int *pos);
//提取
Status Retrival(SqListPtr L,int pos,Elemtype * elem);
//遍历
void List_Printf(SqListPtr L);
//**************************************************************************************************//合并两个线性表
Status List_Union(SqListPtr L1,SqListPtr L2);int main ()
{Ptr hander_1 = NULL;Ptr hander_2 = NULL;SqListPtr L1 = &hander_1;SqListPtr L2 = &hander_2;if(List_Init(L1)!=success || List_Init(L2)!=success){puts("初始化失败");exit(1); }List_Input(L1,1);List_Input(L2,3);if(List_Union(L1,L2) == success){puts("合并线性表成功"); List_Printf(L2); }else{puts("合并线性表失败");}/*******************************************************************************************//调试函数char Copy;Elemtype elem;int pos;Ptr hander = NULL;SqListPtr L = &hander;while(1){menu ();cin>>Copy;system("cls");switch(Copy){case 'A':case 'a':{if(List_Init(L) == success)puts("初始化成功");elseputs("初始化失败"); break;}case 'B':case 'b':{cout<<"线性表的长度为:"<<List_Size(L)<<endl; break;}case 'C':case 'c':{puts("请输入 -- 数据 -- 位置");cin>>elem>>pos; if(List_Insert(L,pos,elem) == success)puts("插入成功");elseputs("插入失败"); break; }case 'D':case 'd':{puts("请输入数据"); cin>>elem;if(List_Input(L,elem) == success)puts("录入成功");elseputs("录入失败"); break;}case 'E':case 'e':{List_Printf(L);break;}case 'F':case 'f':{puts("请输入你要定位的数据");cin>>elem; if(Locate (L,elem,&pos) == success)cout<<elem<<"是第"<<pos<<"个"<<endl;elsecout<<"该数据不存在"<<endl; break;}case 'G':case 'g':{puts("请输入你要提取数据的序号");cin>>pos; if(Retrival(L,pos,&elem) == success)cout<<"该数据为:"<<elem<<endl;elsecout<<"不存在"<<endl;break;} }}/*******************************************************************************/return 0;
}//合并两个线性表
Status List_Union(SqListPtr L1,SqListPtr L2)
{int Length_1 = List_Size(L1);Elemtype elem;int pos;Status s = success;for(int i = 0; i<Length_1; i++){Retrival(L1,i+1,&elem);s = Locate (L2,elem,&pos);if(s != success){s = List_Insert(L2,2,elem);if(s == fail){s = fail;return s;}} }return s;
} //************************************************************************************
//调试菜单
void menu ()
{puts("A 初始化线性表");puts("B 获得线性表的长度");puts("C 插入");puts("D 录入");puts("E 遍历");puts("F 定位");puts("G 提取");
}//线性表的初始化
Status List_Init(SqListPtr L)
{Status s = fail;if((*L)==NULL){Ptr p = (Ptr)malloc(sizeof(Node));if(p == NULL)return s;(*L) = p;(*L)->next = NULL;s = success;}return s;
}//获得线性表的长度
int List_Size(SqListPtr L)
{int i = 0;Ptr p = (*L)->next;while(p){p = p->next;i++;}return i;
}//录入
Status List_Input(SqListPtr L,Elemtype elem)
{Status s = fail;Ptr q = (Ptr)malloc(sizeof(Node));if(q == NULL)return s;q->data = elem;q->next = NULL;Ptr p = (*L)->next; if(p == NULL){(*L)->next = q;s = success;return s;}while(p->next){p = p->next;}p->next = q;s = success;return s;
}//插入
Status List_Insert(SqListPtr L,int pos,Elemtype elem)
{Status s = fail;Ptr p = (*L)->next;int i = 1;while(p && i<pos-1) //适用条件 : 线性表不为空 {p = p->next;i++;}if(p && i==pos-1){Ptr q = (Ptr)malloc(sizeof(Node));if(q == NULL)return s;q->next = p->next;p->next = q;q->data = elem;s = success;}return s;
}//定位
Status Locate (SqListPtr L,Elemtype elem,int *pos)
{Status s = fail;int i = 1;Ptr p = (*L)->next;while(p){if(p->data == elem)break;p = p->next;i++;}if(p && p->data==elem){*pos = i;s = success;}return s;
}//提取
Status Retrival(SqListPtr L,int pos,Elemtype * elem)
{Status s = fail;Ptr p = (*L)->next;int i = 1;while( p && i<pos){p = p->next;i++;}if(p && i == pos){*elem = p->data;s = success;}return s;
}//遍历
void List_Printf(SqListPtr L)
{Ptr p = (*L)->next;int i = 0;while(p){cout<<p->data<<" "; if(i % 3 == 0)cout<<endl;p = p->next;}
}
//************************************************************************************
发布评论