#include <stdio.h>
#include <stdlib.h>
//Requirement, write a sequential linear table and store an address position
#define LIST_INIT_SIZE 100 //The initial allocation of linear table storage space
#define LISTINCREMENT 10 //Linear table Allocation increment of storage space
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status; //function result status code
typedef struct{
int x;
int y ;
}Point2D;
typedef struct{
Point2D * element; //Base address of storage space
int length; //Current length
int listsize; //Currently allocated storage capacity (in sizeof(elementType))
}SqList;
//Initialization sequence table
Status InitList(SqList * L){
//Allocate a listsize space (now element is an array)
L->element=(Point2D*)malloc(LIST_INIT_SIZE*sizeof(Point2D));
if(!L ->element){
//No space is applied for
exit(OVERFLOW);
}
//After the application is successful, the length is set to 0
L->length=0;
L->listsize=LIST_INIT_SIZE;
return OK;
}
//Insert elements ( Insert data p before the i-th position inserted in the sequence table)
Status ListInsert(SqList * L,int i,Point2D p){
int j;
//Determine whether the inserted position is legal
//The legal position of i is 1 ListLength( L)+1
if(i<1||i>L->length+1){
//The range of i is illegal
return ERROR;
}
if(L->length>=L->listsize){
//If it is greater than or equal to the maximum allocation length, it means that memory needs to be re-allocated
Point2D * p=(Point2D*)realloc(L->element,(L-> listsize+LISTINCREMENT)*sizeof(Point2D));
if(!p){
//If it is empty, it means the allocation failed
return ERROR;
}
//The allocation is successful
L->element=p; //The new base address
L->listsize+ =LISTINCREMENT;
}
//If all are valid, insert the data, move the i-th position and the elements after it by one position
for(j=L->length-1;j>=i-1; j--){
L->element[j+1]=L->element[j];
}
//Write the data in the i-1 space
L->element[i-1]=p;
//The length add 1
L->length++;
return OK;
}
//合并顺序列表(将l1和l2中的元素给合并到l3中) l3必须在外面就进行分配内存空间
void MergeList(SqList l1,SqList l2,SqList *l3){
Point2D *l1_last,*l2_last;
Point2D *l1_start,*l2_start,*l3_start;
int i=l1.length+l2.length;
l3->length=l3->listsize=i;
l3_start=l3->element=(Point2D*)malloc(sizeof(Point2D));
if(!l3){
exit(OVERFLOW);
}
l1_start=l1.element;
l2_start=l2.element;
l1_last=l1.element+l1.length-1;
l2_last=l2.element+l2.length-1;
while(l1_start<=l1_last&&l2_start<=l2_last){
if(l1_start->x>l2_start->x){
*l3_start=*l1_start;
l3_start++;
l1_start++;
}else if(l1_start->x<l2_start->x){
*l3_start=*l2_start;
l3_start++;
l2_start++;
}else{
//相等
*l3_start=*l2_start;
l3_start++;
l2_start++;
l1_start++;
}
}
while(l1_start<=l1_last){
*l3_start=*l1_start;
l3_start++;
l1_start++;
}
while(l2_start<=l2_last){
*l3_start=*l2_start;
l3_start++;
l2_start;
}
}
//Test
int main(){
int i;
SqList * L1=(SqList *)malloc(sizeof(SqList));
Point2D p={1,2};
//Initialize a pointer to a structure
/ /The structure allocates a space to store the first address of the array, the length of the array, and the maximum capacity of the array
SqList * L=(SqList*)malloc(sizeof(SqList));
i=InitList(L);
if(i= =OK){
printf("Created successfully");
}
i=ListInsert(L,1,p);
if(i==OK){
printf("Insert successfully");
}
i=ListInsert(L,2,p );
//Print the inserted data element
printf("Data x is %d, data y is: %d, the length of the sequence table is %d", L->element[0].x, L->element[0 ].y,L->length);
printf("hello world");
printf("Create a new set of sequence lists");
MergeList(*L,*L,L1);
printf("The length of the merged sequence list is %d, and the total capacity is % d\n",L1->length,L1->listsize);
while(L1->element->x){
printf("The x value of the data:%d\n",L1->element->x) ;
L1->element++;
}
scanf("%d",&i);
}
#include <stdlib.h>
//Requirement, write a sequential linear table and store an address position
#define LIST_INIT_SIZE 100 //The initial allocation of linear table storage space
#define LISTINCREMENT 10 //Linear table Allocation increment of storage space
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status; //function result status code
typedef struct{
int x;
int y ;
}Point2D;
typedef struct{
Point2D * element; //Base address of storage space
int length; //Current length
int listsize; //Currently allocated storage capacity (in sizeof(elementType))
}SqList;
//Initialization sequence table
Status InitList(SqList * L){
//Allocate a listsize space (now element is an array)
L->element=(Point2D*)malloc(LIST_INIT_SIZE*sizeof(Point2D));
if(!L ->element){
//No space is applied for
exit(OVERFLOW);
}
//After the application is successful, the length is set to 0
L->length=0;
L->listsize=LIST_INIT_SIZE;
return OK;
}
//Insert elements ( Insert data p before the i-th position inserted in the sequence table)
Status ListInsert(SqList * L,int i,Point2D p){
int j;
//Determine whether the inserted position is legal
//The legal position of i is 1 ListLength( L)+1
if(i<1||i>L->length+1){
//The range of i is illegal
return ERROR;
}
if(L->length>=L->listsize){
//If it is greater than or equal to the maximum allocation length, it means that memory needs to be re-allocated
Point2D * p=(Point2D*)realloc(L->element,(L-> listsize+LISTINCREMENT)*sizeof(Point2D));
if(!p){
//If it is empty, it means the allocation failed
return ERROR;
}
//The allocation is successful
L->element=p; //The new base address
L->listsize+ =LISTINCREMENT;
}
//If all are valid, insert the data, move the i-th position and the elements after it by one position
for(j=L->length-1;j>=i-1; j--){
L->element[j+1]=L->element[j];
}
//Write the data in the i-1 space
L->element[i-1]=p;
//The length add 1
L->length++;
return OK;
}
//合并顺序列表(将l1和l2中的元素给合并到l3中) l3必须在外面就进行分配内存空间
void MergeList(SqList l1,SqList l2,SqList *l3){
Point2D *l1_last,*l2_last;
Point2D *l1_start,*l2_start,*l3_start;
int i=l1.length+l2.length;
l3->length=l3->listsize=i;
l3_start=l3->element=(Point2D*)malloc(sizeof(Point2D));
if(!l3){
exit(OVERFLOW);
}
l1_start=l1.element;
l2_start=l2.element;
l1_last=l1.element+l1.length-1;
l2_last=l2.element+l2.length-1;
while(l1_start<=l1_last&&l2_start<=l2_last){
if(l1_start->x>l2_start->x){
*l3_start=*l1_start;
l3_start++;
l1_start++;
}else if(l1_start->x<l2_start->x){
*l3_start=*l2_start;
l3_start++;
l2_start++;
}else{
//相等
*l3_start=*l2_start;
l3_start++;
l2_start++;
l1_start++;
}
}
while(l1_start<=l1_last){
*l3_start=*l1_start;
l3_start++;
l1_start++;
}
while(l2_start<=l2_last){
*l3_start=*l2_start;
l3_start++;
l2_start;
}
}
//Test
int main(){
int i;
SqList * L1=(SqList *)malloc(sizeof(SqList));
Point2D p={1,2};
//Initialize a pointer to a structure
/ /The structure allocates a space to store the first address of the array, the length of the array, and the maximum capacity of the array
SqList * L=(SqList*)malloc(sizeof(SqList));
i=InitList(L);
if(i= =OK){
printf("Created successfully");
}
i=ListInsert(L,1,p);
if(i==OK){
printf("Insert successfully");
}
i=ListInsert(L,2,p );
//Print the inserted data element
printf("Data x is %d, data y is: %d, the length of the sequence table is %d", L->element[0].x, L->element[0 ].y,L->length);
printf("hello world");
printf("Create a new set of sequence lists");
MergeList(*L,*L,L1);
printf("The length of the merged sequence list is %d, and the total capacity is % d\n",L1->length,L1->listsize);
while(L1->element->x){
printf("The x value of the data:%d\n",L1->element->x) ;
L1->element++;
}
scanf("%d",&i);
}