SCAU程序设计在线实训平台_实验_数据结构_实验1

8576 顺序线性表的基本操作

Description

编写算法,创建初始化容量为LIST_INIT_SIZE的顺序表T,并实现插入、删除、遍历操作。本题目给出部分代码,请补全内容。

#include<stdio.h>
#include<malloc.h>
#define OK 1 
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int

typedef struct
{
	int *elem;
	int length;
	int listsize;
}SqList;

int InitList_Sq(SqList &L)
{
// 算法2.3,构造一个空的线性表L,该线性表预定义大小为LIST_INIT_SIZE
// 请补全代码

}

int Load_Sq(SqList &L)
{
// 输出顺序表中的所有元素
	int i;
	if(_________________________) printf("The List is empty!");  // 请填空
	else
	{
		printf("The List is: ");
		for(_________________________) printf("%d ",_________________________);  // 请填空
	}
	printf("\n");
	return OK;
}

int ListInsert_Sq(SqList &L,int i,int e)
{
// 算法2.4,在顺序线性表L中第i个位置之前插入新的元素e
// i的合法值为1≤i≤L.length +1
// 请补全代码

}

int ListDelete_Sq(SqList &L,int i, int &e)
{
// 算法2.5,在顺序线性表L中删除第i个位置的元素,并用e返回其值
// i的合法值为1≤i≤L.length
// 请补全代码

}

int main()
{
	SqList T;
	int a, i;
	ElemType e, x;
	if(_________________________)    // 判断顺序表是否创建成功
	{
		printf("A Sequence List Has Created.\n");
	}
	while(1)
	{
		printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n");
		scanf("%d",&a);
		switch(a)
		{
			case 1: scanf("%d%d",&i,&x);
					if(_________________________) printf("Insert Error!\n"); // 判断i值是否合法,请填空
					else printf("The Element %d is Successfully Inserted!\n", x); 
					break;
			case 2: scanf("%d",&i);
					if(_________________________) printf("Delete Error!\n"); // 判断i值是否合法,请填空
					else printf("The Element %d is Successfully Deleted!\n", e);
					break;
			case 3: Load_Sq(T);
					break;
			case 0: return 1;
		}
	}
}
输入格式

测试样例格式说明:
根据菜单操作:
1、输入1,表示要实现插入操作,紧跟着要输入插入的位置和元素,用空格分开
2、输入2,表示要实现删除操作,紧跟着要输入删除的位置
3、输入3,表示要输出顺序表的所有元素
4、输入0,表示程序结束

输入样例

1
1 2
1
1 3
2
1
3
0

输出样例

A Sequence List Has Created.
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The Element 2 is Successfully Inserted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The Element 3 is Successfully Inserted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The Element 3 is Successfully Deleted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The List is: 2
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:

代码实现
#include<stdio.h>
#include<malloc.h>
#define OK 1 
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int

typedef struct
{
	int *elem;
	int length;
	int listsize;
}SqList;

int InitList_Sq(SqList &L)
{
	L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));\
	L.listsize=LIST_INIT_SIZE;
    L.length=0;
    return 1;
}

int Load_Sq(SqList &L)
{
// 输出顺序表中的所有元素
	int i;
	if(L.length==0) printf("The List is empty!");  // 请填空
	else
	{
		printf("The List is: ");
		for(i=0;i<L.length;i++) printf("%d ",L.elem[i]);  // 请填空
	}
	printf("\n");
	return OK;
}

int ListInsert_Sq(SqList &L,int i,int e)
{
// 算法2.4,在顺序线性表L中第i个位置之前插入新的元素e
// i的合法值为1≤i≤L.length +1
// 请补全代码
	if(i<1||i>L.length+1) return 0;
	for(int j=L.length-1;j>=i-1;j--)
	{
		L.elem[j+1]=L.elem[j];
	}
	L.elem[i-1]=e;
	L.length++;
	return 1;
}

int ListDelete_Sq(SqList &L,int i, int &e)
{
// 算法2.5,在顺序线性表L中删除第i个位置的元素,并用e返回其值
// i的合法值为1≤i≤L.length
// 请补全代码
	if(i<1||i>L.length) return 0;
	e=L.elem[i-1];
	for(int j=i-1;j<=L.length-1;j++)
	{
		L.elem[j]=L.elem[j+1];
	}
	L.length--;
	return 1;

}

int main()
{
	SqList T;
	int a, i;
	ElemType e, x;
	if(InitList_Sq(T))    // 判断顺序表是否创建成功
	{
		printf("A Sequence List Has Created.\n");
	}
	while(1)
	{
		printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n");
		scanf("%d",&a);
		switch(a)
		{
			case 1: scanf("%d%d",&i,&x);
					if(!ListInsert_Sq(T,i,x)) printf("Insert Error!\n"); // 判断i值是否合法,请填空
					else printf("The Element %d is Successfully Inserted!\n", x); 
					break;
			case 2: scanf("%d",&i);
					if(!ListDelete_Sq(T,i,e)) printf("Delete Error!\n"); // 判断i值是否合法,请填空
					else printf("The Element %d is Successfully Deleted!\n", e);
					break;
			case 3: Load_Sq(T);
					break;
			case 0: return 1;
		}
	}
}

8577 合并顺序表

Description

若线性表中数据元素相互之间可以比较,且数据元素在表中按值递增或递减,则称该表为有序表。

编写算法,将两个非递减有序顺序表A和B合并成一个新的非递减有序顺序表C。

输入格式

第一行:顺序表A的元素个数
第二行:顺序表A的各元素(非递减),用空格分开
第三行:顺序表B的元素个数
第四行:顺序表B的各元素(非递减),用空格分开

输出格式

第一行:顺序表A的元素列表
第二行:顺序表B的元素列表
第三行:合并后顺序表C的元素列表

输入样例

5
1 3 5 7 9
5
2 4 6 8 10

输出样例

List A:1 3 5 7 9
List B:2 4 6 8 10
List C:1 2 3 4 5 6 7 8 9 10

提示

输出时注意大小写和标点。

代码实现①(改进自上题代码)
#include<stdio.h>
#include<malloc.h>
#define OK 1 
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int

typedef struct
{
	int *elem;
	int length;
	int listsize;
}SqList;

int InitList_Sq(SqList &L)
{
	L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));\
	L.listsize=LIST_INIT_SIZE;
    L.length=0;
    return 1;
}

int Load_Sq(SqList &L)
{
// 输出顺序表中的所有元素
	int i;
	if(L.length==0) printf("The List is empty!");  // 请填空
	else
	{
		//printf("The List is: ");
		for(i=0;i<L.length;i++) printf("%d ",L.elem[i]);  // 请填空
	}
	printf("\n");
	return OK;
}

int ListInsert_Sq(SqList &L,int i,int e)
{
// 算法2.4,在顺序线性表L中第i个位置之前插入新的元素e
// i的合法值为1≤i≤L.length +1
// 请补全代码
	if(i<1||i>L.length+1) return 0;
	for(int j=L.length-1;j>=i-1;j--)
	{
		L.elem[j+1]=L.elem[j];
	}
	L.elem[i-1]=e;
	L.length++;
	return 1;
}

int ListDelete_Sq(SqList &L,int i, int &e)
{
// 算法2.5,在顺序线性表L中删除第i个位置的元素,并用e返回其值
// i的合法值为1≤i≤L.length
// 请补全代码
	if(i<1||i>L.length) return 0;
	e=L.elem[i-1];
	for(int j=i-1;j<=L.length-1;j++)
	{
		L.elem[j]=L.elem[j+1];
	}
	L.length--;
	return 1;

}

int main()
{
	SqList A,B,C;
	int a_len,b_len;
	ElemType x;
	InitList_Sq(A);
	InitList_Sq(B);
	InitList_Sq(C);   
	
	//A
	scanf("%d",&a_len);
	for(int i=1;i<=a_len;i++){
		scanf("%d",&x);
		ListInsert_Sq(A,i,x);
	}
	printf("List A:");
	Load_Sq(A);
	
	//B
	scanf("%d",&b_len);
	for(int i=1;i<=b_len;i++){
		scanf("%d",&x);
		ListInsert_Sq(B,i,x);
	}
	printf("List B:");
	Load_Sq(B);
	
	int ap=0,bp=0,cp=1,last=0;
	while(ap<A.length&&bp<B.length)
	{
		if(A.elem[ap]>=B.elem[bp]){
			ListInsert_Sq(C,cp++,B.elem[bp++]);
		}
		else{
			ListInsert_Sq(C,cp++,A.elem[ap++]);
		}
		
	}
	//剩余部分处理 
	while(ap<A.length){
		ListInsert_Sq(C,cp++,A.elem[ap++]);
	}
	while(bp<B.length){
		ListInsert_Sq(C,cp++,B.elem[bp++]);
	}
	printf("List C:");
	Load_Sq(C);
}

8578 顺序表逆置

Description

顺序表的基本操作代码如下:

#include<stdio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int

typedef int Status;
typedef struct
{
    int *elem;
    int length;
    int listsize;
}SqList;

Status InitList_Sq(SqList &L) 
{  // 算法2.3
  // 构造一个空的线性表L。
  L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
  if (!L.elem) return OK;        // 存储分配失败
  L.length = 0;                  // 空表长度为0
  L.listsize = LIST_INIT_SIZE;   // 初始存储容量
  return OK;
} // InitList_Sq

Status ListInsert_Sq(SqList &L, int i, ElemType e) 
{  // 算法2.4
  // 在顺序线性表L的第i个元素之前插入新的元素e,
  // i的合法值为1≤i≤ListLength_Sq(L)+1
  ElemType *p;
  if (i < 1 || i > L.length+1) return ERROR;  // i值不合法
  if (L.length >= L.listsize) {   // 当前存储空间已满,增加容量
    ElemType *newbase = (ElemType *)realloc(L.elem,
                  (L.listsize+LISTINCREMENT)*sizeof (ElemType));
    if (!newbase) return ERROR;   // 存储分配失败
    L.elem = newbase;             // 新基址
    L.listsize += LISTINCREMENT;  // 增加存储容量
  }
  ElemType *q = &(L.elem[i-1]);   // q为插入位置
  for (p = &(L.elem[L.length-1]); p>=q; --p) *(p+1) = *p;
                                  // 插入位置及之后的元素右移
  *q = e;       // 插入e
  ++L.length;   // 表长增1
  return OK;
} // ListInsert_Sq

Status ListDelete_Sq(SqList &L, int i, ElemType &e) 
{  // 算法2.5
  // 在顺序线性表L中删除第i个元素,并用e返回其值。
  // i的合法值为1≤i≤ListLength_Sq(L)。
  ElemType *p, *q;
  if (i<1 || i>L.length) return ERROR;  // i值不合法
  p = &(L.elem[i-1]);                   // p为被删除元素的位置
  e = *p;                               // 被删除元素的值赋给e
  q = L.elem+L.length-1;                // 表尾元素的位置
  for (++p; p<=q; ++p) *(p-1) = *p;     // 被删除元素之后的元素左移
  --L.length;                           // 表长减1
  return OK;
} // ListDelete_Sq

设有一顺序表A=(a0,a1,…, ai,…an-1),其逆顺序表定义为A’=( an-1,…, ai,…,a1, a0)。设计一个算法,将顺序表逆置,要求顺序表仍占用原顺序表的空间。

输入格式

第一行:输入顺序表的元素个数
第二行:输入顺序表的各元素,用空格分开

输出格式

第一行:逆置前的顺序表元素列表
第二行:逆置后的顺序表元素列表

输入样例

10
1 2 3 4 5 6 7 8 9 10

输出样例

The List is:1 2 3 4 5 6 7 8 9 10
The turned List is:10 9 8 7 6 5 4 3 2 1

代码实现
#include<stdio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ElemType int

typedef int Status;
typedef struct
{
    int *elem;
    int length;
    int listsize;
}SqList;

Status InitList_Sq(SqList &L) 
{  // 算法2.3
  // 构造一个空的线性表L。
  L.elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
  if (!L.elem) return OK;        // 存储分配失败
  L.length = 0;                  // 空表长度为0
  L.listsize = LIST_INIT_SIZE;   // 初始存储容量
  return OK;
} // InitList_Sq

Status ListInsert_Sq(SqList &L, int i, ElemType e) 
{  // 算法2.4
  // 在顺序线性表L的第i个元素之前插入新的元素e,
  // i的合法值为1≤i≤ListLength_Sq(L)+1
  ElemType *p;
  if (i < 1 || i > L.length+1) return ERROR;  // i值不合法
  if (L.length >= L.listsize) {   // 当前存储空间已满,增加容量
    ElemType *newbase = (ElemType *)realloc(L.elem,
                  (L.listsize+LISTINCREMENT)*sizeof (ElemType));
    if (!newbase) return ERROR;   // 存储分配失败
    L.elem = newbase;             // 新基址
    L.listsize += LISTINCREMENT;  // 增加存储容量
  }
  ElemType *q = &(L.elem[i-1]);   // q为插入位置
  for (p = &(L.elem[L.length-1]); p>=q; --p) *(p+1) = *p;
                                  // 插入位置及之后的元素右移
  *q = e;       // 插入e
  ++L.length;   // 表长增1
  return OK;
} // ListInsert_Sq

Status ListDelete_Sq(SqList &L, int i, ElemType &e) 
{  // 算法2.5
  // 在顺序线性表L中删除第i个元素,并用e返回其值。
  // i的合法值为1≤i≤ListLength_Sq(L)。
  ElemType *p, *q;
  if (i<1 || i>L.length) return ERROR;  // i值不合法
  p = &(L.elem[i-1]);                   // p为被删除元素的位置
  e = *p;                               // 被删除元素的值赋给e
  q = L.elem+L.length-1;                // 表尾元素的位置
  for (++p; p<=q; ++p) *(p-1) = *p;     // 被删除元素之后的元素左移
  --L.length;                           // 表长减1
  return OK;
} // ListDelete_Sq

int Load_Sq(SqList &L)
{
// 输出顺序表中的所有元素
	int i;
	if(L.length==0)
        printf("The List is empty!");  // 请填空
    else
	{
		//printf("The List is: ");
		for(i=0;i<L.length;i++) printf("%d ",L.elem[i]);  // 请填空
	}
    printf("\n");
	return OK;
}

int main()
{
	SqList A,B;
	int a_len;
	ElemType x;
	InitList_Sq(A);
	InitList_Sq(B); 
	
	//A
	scanf("%d",&a_len);
	for(int i=1;i<=a_len;i++){
		scanf("%d",&x);
		ListInsert_Sq(A,i,x);
	}
	printf("The List is:");
	Load_Sq(A);
	int ap=A.length;
	while(ap>=0)
	{
		ListInsert_Sq(B,A.length-ap,A.elem[ap]);
		ap--;
	}
	printf("The turned List is:");
	Load_Sq(B);

}

8579 链式线性表的基本操作

Description

编写算法,创建一个含有n个元素的带头结点的单链表L并实现插入、删除、遍历操作。本题目提供部分代码,请补全内容。

#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1 
#define ElemType int

typedef struct LNode
{
 int data;
 struct LNode *next;
}LNode,*LinkList;

int CreateLink_L(LinkList &L,int n){
// 创建含有n个元素的单链表
  LinkList p,q;
  int i;
  ElemType e;
  L = new LNode;
  L->next = NULL;              // 先建立一个带头结点的单链表
  q = new LNode;
  q = L;
  for (i=0; i<n; i++) {
    scanf("%d", &e);
    p = new LNode;  // 生成新结点
    // 请补全代码

  }
  return OK;
}

int LoadLink_L(LinkList &L){
// 单链表遍历
 LinkList p = L->next;
 if(___________________________)printf("The List is empty!"); // 请填空
 else
 {
	 printf("The LinkList is:");
	 while(___________________________)    // 请填空
	 {
		printf("%d ",p->data); 
		___________________________    // 请填空
	 }
 }
 printf("\n");
 return OK;
}

int LinkInsert_L(LinkList &L,int i,ElemType e){
// 算法2.9
// 在带头结点的单链线性表L中第i个位置之前插入元素e
// 请补全代码

}

int LinkDelete_L(LinkList &L,int i, ElemType &e){
// 算法2.10
// 在带头结点的单链线性表L中,删除第i个元素,并用e返回其值
// 请补全代码

}

int main()
{
 LinkList T;
 int a,n,i;
 ElemType x, e;
 printf("Please input the init size of the linklist:\n");
 scanf("%d",&n);
 printf("Please input the %d element of the linklist:\n", n);
 if(___________________________)     // 判断链表是否创建成功,请填空
 {
	 printf("A Link List Has Created.\n");
	 LoadLink_L(T);
 }
 while(1)
	{
		printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n");
		scanf("%d",&a);
		switch(a)
		{
			case 1: scanf("%d%d",&i,&x);
				  if(___________________________) printf("Insert Error!\n"); // 判断i值是否合法,请填空
				  else printf("The Element %d is Successfully Inserted!\n", x); 
				  break;
			case 2: scanf("%d",&i);
				  if(___________________________) printf("Delete Error!\n"); // 判断i值是否合法,请填空
				  else printf("The Element %d is Successfully Deleted!\n", e);
				  break;
			case 3: LoadLink_L(T);
				  break;
			case 0: return 1;
		}
	}
}
输入格式

测试样例格式说明:
根据菜单操作:
1、输入1,表示要实现插入操作,紧跟着要输入插入的位置和元素,用空格分开
2、输入2,表示要实现删除操作,紧跟着要输入删除的位置
3、输入3,表示要输出顺序表的所有元素
4、输入0,表示程序结束

输入样例

3
3 6 9
3
1
4 12
2
1
3
0

输出样例

Please input the init size of the linklist:
Please input the 3 element of the linklist:
A Link List Has Created.
The LinkList is:3 6 9
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The LinkList is:3 6 9
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The Element 12 is Successfully Inserted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The Element 3 is Successfully Deleted!
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:
The LinkList is:6 9 12
1:Insert element
2:Delete element
3:Load all elements
0:Exit
Please choose:

代码实现
#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define ElemType int

typedef struct LNode
{
    int data;
    struct LNode *next;
}LNode,*LinkList;

int CreateLink_L(LinkList &L,int n){
// 创建含有n个元素的单链表
    LinkList node,pre;
    int i;
    ElemType e;
    L = new LNode;
    L->next = NULL;              // 先建立一个带头结点的单链表
    pre = L;
    for (i=0; i<n; i++) {
        scanf("%d", &e);
        node = new LNode;  // 生成新结点
        node->data=e;
        node->next=NULL;
        pre->next=node;
        pre=node;
        // 请补全代码
    }
    return OK;
}

int LoadLink_L(LinkList &L){
// 单链表遍历
    LinkList p = L->next;
    if(p==NULL)printf("The List is empty!"); // 请填空
    else
    {
        printf("The LinkList is:");
        while(p!=NULL)    // 请填空
        {
            printf("%d ",p->data);
            p=p->next;    // 请填空
        }
    }
    printf("\n");
    return OK;
}

int LinkInsert_L(LinkList &L,int i,ElemType e){
// 算法2.9
// 在带头结点的单链线性表L中第i个位置之前插入元素e
    LinkList node = L,newp;
    int j=1;
    while(node && j< i){
        node=node->next;
        j++;
    }
    if(node==NULL)return ERROR;
    newp= new LNode;
    newp->data=e;
    newp->next=node->next;
    node->next=newp;
    return OK;
// 请补全代码

}

int LinkDelete_L(LinkList &L,int i, ElemType &e){
// 算法2.10
// 在带头结点的单链线性表L中,删除第i个元素,并用e返回其值
    LinkList p = L,pre;
    int j=1;
    while(p&&j<=i){
        pre=p;
        p=p->next;
        j++;
    }
    if(p==NULL)return ERROR;
    e=p->data;
    pre->next=p->next;
    return OK;
// 请补全代码

}

int main()
{
    LinkList T;
    int a,n,i;
    ElemType x, e;
    printf("Please input the init size of the linklist:\n");
    scanf("%d",&n);
    printf("Please input the %d element of the linklist:\n", n);
    if(CreateLink_L(T,n))     // 判断链表是否创建成功,请填空
    {
        printf("A Link List Has Created.\n");
        LoadLink_L(T);
    }
    while(1)
    {
        printf("1:Insert element\n2:Delete element\n3:Load all elements\n0:Exit\nPlease choose:\n");
        scanf("%d",&a);
        switch(a)
        {
            case 1: scanf("%d%d",&i,&x);
                if(!LinkInsert_L(T,i,x)) printf("Insert Error!\n"); // 判断i值是否合法,请填空
                else printf("The Element %d is Successfully Inserted!\n", x);
                break;
            case 2: scanf("%d",&i);
                if(!LinkDelete_L(T,i,e)) printf("Delete Error!\n"); // 判断i值是否合法,请填空
                else printf("The Element %d is Successfully Deleted!\n", e);
                break;
            case 3: LoadLink_L(T);
                break;
            case 0: return 1;
        }
    }
}

8580 合并链表

Description

线性链表的基本操作如下:

#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1 
#define ElemType int

typedef int Status;
typedef struct LNode
{
 int data;
 struct LNode *next;
}LNode,*LinkList;


Status ListInsert_L(LinkList &L, int i, ElemType e) {  // 算法2.9
  // 在带头结点的单链线性表L的第i个元素之前插入元素e
  LinkList p,s;
  p = L;   
  int j = 0;
  while (p && j < i-1) {  // 寻找第i-1个结点
    p = p->next;
    ++j;
  } 
  if (!p || j > i-1) return ERROR;      // i小于1或者大于表长
  s = (LinkList)malloc(sizeof(LNode));  // 生成新结点
  s->data = e;  s->next = p->next;      // 插入L中
  p->next = s;
  return OK;
} // LinstInsert_L

Status ListDelete_L(LinkList &L, int i, ElemType &e) {  // 算法2.10
  // 在带头结点的单链线性表L中,删除第i个元素,并由e返回其值
  LinkList p,q;
  p = L;
  int j = 0;
  while (p->next && j < i-1) {  // 寻找第i个结点,并令p指向其前趋
    p = p->next;
    ++j;
  }
  if (!(p->next) || j > i-1) return ERROR;  // 删除位置不合理
  q = p->next;
  p->next = q->next;           // 删除并释放结点
  e = q->data;
  free(q);
  return OK;
} // ListDelete_L

设计一个算法将两个非递减有序链表A和B合并成一个新的非递减有序链表C。

输入格式

第一行:单链表A的元素个数
第二行:单链表A的各元素(非递减),用空格分开
第三行:单链表B的元素个数
第四行:单链表B的各元素(非递减),用空格分开

输出格式

第一行:单链表A的元素列表
第二行:单链表B的元素列表
第三行:合并后单链表C的元素列表

输入样例

6
12 24 45 62 84 96
4
15 31 75 86

输出样例

List A:12 24 45 62 84 96
List B:15 31 75 86
List C:12 15 24 31 45 62 75 84 86 96

代码实现
#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define ElemType int

typedef struct LNode
{
    int data;
    struct LNode *next;
}LNode,*LinkList;

int CreateLink_L(LinkList &L,int n){
// 创建含有n个元素的单链表
    LinkList node,pre;
    int i;
    ElemType e;
    L = new LNode;
    L->next = NULL;              // 先建立一个带头结点的单链表
    pre = L;
    for (i=0; i<n; i++) {
        scanf("%d", &e);
        node = new LNode;  // 生成新结点
        node->data=e;
        node->next=NULL;
        pre->next=node;
        pre=node;
        // 请补全代码
    }
    return OK;
}

int LoadLink_L(LinkList &L){
// 单链表遍历
    LinkList p = L->next;
    if(p==NULL)printf("The List is empty!"); // 请填空
    else
    {
        //printf("The LinkList is:");
        while(p!=NULL)    // 请填空
        {
            printf("%d ",p->data);
            p=p->next;    // 请填空
        }
    }
    printf("\n");
    return OK;
}

int CreateEmpLink_L(LinkList &L,int n){
// 创建含有n个元素的单链表
    LinkList node,pre;
    int i;
    ElemType e;
    L = new LNode;
    L->next = NULL;              // 先建立一个带头结点的单链表
    pre = L;
    for (i=0; i<n; i++) {
        node = new LNode;  // 生成新结点
        node->data=0;
        node->next=NULL;
        pre->next=node;
        pre=node;
    }
    return OK;
}

int LinkSort_L(LinkList &X,LinkList &Y,LinkList &C){
// 算法2.10
// 在带头结点的单链线性表L中,删除第i个元素,并用e返回其值
    LinkList pa = X->next,pb=Y->next,p=C->next;
    while(pa!=NULL&&pb!=NULL){
        if(pa->data>pb->data){
            p->data=pb->data;
            pb=pb->next;
            p=p->next;
        }else{
            p->data=pa->data;
            pa=pa->next;
            p=p->next;
        }
    }
    while(pb!=NULL) {
        p->data = pb->data;
        pb = pb->next;
        p = p->next;
    }
    while(pa!=NULL) {
        p->data = pa->data;
        pa = pa->next;
        p = p->next;
    }
    return OK;
// 请补全代码
}

int main()
{
    LinkList A,B,C;
    int a_len,b_len;
    //ElemType x, e;
    scanf("%d",&a_len);
    CreateLink_L(A,a_len);
    printf("List A:");
    LoadLink_L(A);

    scanf("%d",&b_len);
    CreateLink_L(B,b_len);
    printf("List B:");
    LoadLink_L(B);

    CreateEmpLink_L(C,a_len+b_len);
    LinkSort_L(A,B,C);
    printf("List C:");
    LoadLink_L(C);
}

8581 线性链表逆置

Description

线性链表的基本操作如下:

#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1 
#define ElemType int

typedef int Status;
typedef struct LNode
{
 int data;
 struct LNode *next;
}LNode,*LinkList;


Status ListInsert_L(LinkList &L, int i, ElemType e) {  // 算法2.9
  // 在带头结点的单链线性表L的第i个元素之前插入元素e
  LinkList p,s;
  p = L;   
  int j = 0;
  while (p && j < i-1) {  // 寻找第i-1个结点
    p = p->next;
    ++j;
  } 
  if (!p || j > i-1) return ERROR;      // i小于1或者大于表长
  s = (LinkList)malloc(sizeof(LNode));  // 生成新结点
  s->data = e;  s->next = p->next;      // 插入L中
  p->next = s;
  return OK;
} // LinstInsert_L

Status ListDelete_L(LinkList &L, int i, ElemType &e) {  // 算法2.10
  // 在带头结点的单链线性表L中,删除第i个元素,并由e返回其值
  LinkList p,q;
  p = L;
  int j = 0;
  while (p->next && j < i-1) {  // 寻找第i个结点,并令p指向其前趋
    p = p->next;
    ++j;
  }
  if (!(p->next) || j > i-1) return ERROR;  // 删除位置不合理
  q = p->next;
  p->next = q->next;           // 删除并释放结点
  e = q->data;
  free(q);
  return OK;
} // ListDelete_L

设有一线性表A=(a0,a1,…, ai,…an-1),其逆线性表定义为A’=( an-1,…, ai,…,a1, a0),设计一个算法,将线性表逆置,要求线性表仍占用原线性表的空间。

输入格式

第一行:输入n,表示单链表的元素个数
第二行:输入单链表的各元素,用空格分开

输出格式

第一行:输出单链表逆置前的元素列表
第二行:输出单链表逆置后的元素列表

输入样例

8
32 97 54 65 35 84 61 75

输出样例

The List is:32 97 54 65 35 84 61 75
The turned List is:75 61 84 35 65 54 97 32

代码实现
#include<stdio.h>
#include<malloc.h>
#define ERROR 0
#define OK 1
#define ElemType int

typedef struct LNode
{
    int data;
    struct LNode *next;
}LNode,*LinkList;

int CreateLink_L(LinkList &L,int n){
// 创建含有n个元素的单链表
    LinkList node,pre;
    int i;
    ElemType e;
    L = new LNode;
    L->next = NULL;              // 先建立一个带头结点的单链表
    pre = L;
    for (i=0; i<n; i++) {
        scanf("%d", &e);
        node = new LNode;  // 生成新结点
        node->data=e;
        node->next=NULL;
        pre->next=node;
        pre=node;
        // 请补全代码
    }
    return OK;
}

int LoadLink_L(LinkList &L){
// 单链表遍历
    LinkList p = L->next;
    if(p==NULL)printf("The List is empty!"); // 请填空
    else
    {
        //printf("The LinkList is:");
        while(p!=NULL)    // 请填空
        {
            printf("%d ",p->data);
            p=p->next;    // 请填空
        }
    }
    printf("\n");
    return OK;
}

int LinkSwitch_L(LinkList &X){
// 算法2.10
// 在带头结点的单链线性表L中,删除第i个元素,并用e返回其值
    LinkList head=X,pre = NULL,node=X->next,after=X->next;

    while(after!=NULL){
        after=after->next;
        node->next=pre;
        pre=node;
        node=after;
        //after=after->next;
    }
    X->next=pre;

    return OK;
// 请补全代码
}

int main()
{
    LinkList A;
    int a_len;
    //ElemType x, e;
    scanf("%d",&a_len);
    CreateLink_L(A,a_len);
    printf("The List is:");
    LoadLink_L(A);
    LinkSwitch_L(A);
    printf("The turned List is:");
    LoadLink_L(A);



}

-END-

  • 8
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值