第一种:顺序表
typedef struct{
int data[MAXSIZE];
int len;
}SqList;
SqList * initSqList(){
SqList * L;
L = (SqList *)malloc(sizeof(SqList));
L->len = 0;
return L;
}
void createSqList(SqList * L){
int n;
printf("please input the length of the list!\n");
scanf("%d",&n);
printf("please input the values of the list!\n");
for(int i=1;i<=n;i++){
scanf("%d",&L->data[i]);
}
L->len = n;
}
int insertSqList(SqList * L,int i,int e){
if(i<1 || i>L->len+1){
printf("error!\n");
return -1;
}else{
for(int j=L->len;j>=i;j--){
L->data[j+1] = L->data[j];
}
L->data[i] = e;
L->len++;
return 0;
}
}
int deleteSqList(SqList * L,int i){
if(i<1 || i>L->len){
printf("error!\n");
return -1;
}else{
for(int j=i;j<=L->len;j++){
L->data[j] = L->data[j+1];
}
L->len--;
return 0;
}
}
void print(SqList * L){
for(int i=1;i<=L->len;i++){
printf("%4d",L->data[i]);
}
printf("\n");
}
int main(){
SqList * La;
SqList * Lb;
SqList * Lc;
int i=1;
int j=1;
int k=1;
// int tempa;
// int tempaa;
La = initSqList();
Lb = initSqList();
printf("create La!\n");
createSqList(La);
printf("\nLa:\n");
print(La);
printf("after insert:\n");
//插入和删除的验证代码
/* tempa = insertSqList(La,3,67);
if(tempa == -1){
printf("insert error!\n");
}else{
print(La);
}
printf("after delete:\n");
tempa = deleteSqList(La,2);
if(tempaa == -1){
printf("delete error!\n");
}else{
print(La);
}
*/
printf("create Lb!\n");
createSqList(Lb);
printf("\nLb:\n");
print(Lb);
if(La->len + Lb->len > MAXSIZE-1){
printf("error!\n");
}else{
Lc = initSqList();
while(i<=La->len && j<=Lb->len){
if(La->data[i] <= Lb->data[j]){
insertSqList(Lc,k,La->data[i]);
k++;
i++;
}else{
insertSqList(Lc,k,Lb->data[j]);
k++;
j++;
}
}
while(i<=La->len){
insertSqList(Lc,k,La->data[i]);
k++;
i++;
}
while(j<=Lb->len){
insertSqList(Lc,k,Lb->data[j]);
k++;
j++;
}
printf("after combination!\n");
printf("Lc:\n");
print(Lc);
}
return 0;
}
第二种:链表
typedef struct node{
char data;
struct node * next;
}Linklist;
//头插入法:
Linklist * createLinklist1(){
Linklist * head,* p;
char x;
head = (Linklist *)malloc(sizeof(Linklist));
head->next = NULL;
printf("please input data!\n");
scanf("%c",&x);
while(x != '\n'){
p = (Linklist *)malloc(sizeof(Linklist));
p->data = x;
p->next = head->next;
head->next = p;
scanf("%c",&x);
}
return head;
}
//尾插入法
Linklist * createLinklist2(){
char x;
Linklist * head,* p,* q;
head = (Linklist *)malloc(sizeof(Linklist));
head->next = NULL;
q = head;
printf("please input the values of the list!\n");
scanf("%c",&x);
while(x != '\n'){
p = (Linklist *)malloc(sizeof(Linklist));
p->data = x;
p->next = NULL;
q->next = p;
q = p;
scanf("%c",&x);
}
return head;
}
//插入
int insertLinklist(Linklist * head,int i,char e){
int j=0;
Linklist * p, * q;
p = head;
while(p != NULL && j<(i-1)){
p = p->next;
j++;
}
if(p == NULL){
printf("the location is not valid!\n");
return -1;
}else{
q = (Linklist *)malloc(sizeof(Linklist));
q->data = e;
q->next = p->next;
p->next = q;
return 0;
}
}
//删除
int deleteLinklist(Linklist * head,int i){
int j=0;
Linklist * p, * q;
p = head;
while(p != NULL && j<(i-1)){
p = p->next;
j++;
}
if(p == NULL){
printf("the location is not valid!\n");
return -1;
}else if(p->next == NULL){
printf("the node doesn't exist!\n");
return -1;
}else{
q = p->next;
p->next = q->next;
free(q);
return 0;
}
}
//打印
void print(Linklist * head){
Linklist * p;
p = head->next;
while(p != NULL){
printf("%2c",p->data);
p = p->next;
}
printf("\n");
}
int main(){
Linklist * La;
Linklist * Lb;
Linklist * Lc;
// int temp;
// int tempa;
int k = 1;
Linklist * pa;
Linklist * pb;
Linklist * pc;
int t1;
printf("create La!\n");
La = createLinklist2();
printf("\nLa:\n");
print(La);
printf("\n");
//插入和删除的验证代码
/* printf("after insert:\n");
temp = insertLinklist(La,1,'8');
if(temp == -1){
printf("error!\n");
}else{
printf("\nLa:\n");
print(La);
}
printf("\n");
printf("after delete:\n");
tempa = deleteLinklist(La,5);
if(tempa == -1){
printf("error!\n");
}else{
printf("\nLa:\n");
print(La);
}
*/
printf("create Lb!\n");
Lb = createLinklist2();
printf("\nLb:\n");
print(Lb);
Lc = (Linklist *)malloc(sizeof(Linklist));
Lc->next = NULL;
pa = La->next;
pb = Lb->next;
pc = Lc;
//合并
while(pa != NULL && pb != NULL){
if(pa->data <= pb->data){
t1 = insertLinklist(Lc,k,pa->data);
pc = pc->next;
pa = pa->next;
k++;
}else{
t1 = insertLinklist(Lc,k,pb->data);
pc = pc->next;
pb = pb->next;
k++;
}
}
while(pa != NULL){
t1 = insertLinklist(Lc,k,pa->data);
pc = pc->next;
pa = pa->next;
k++;
}
while(pb != NULL){
t1 = insertLinklist(Lc,k,pb->data);
pc = pc->next;
pb = pb->next;
k++;
}
printf("\nLc:\n");
print(Lc);
return 0;
}