C language to achieve the sequence of basic operations table summary
- 2020-04-02 02:24:00
- OfStack
This article summarizes the C language under the implementation and operation of the method of the sequence table, for learning the data structure of friends is a good reference program. The complete code is as follows:
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int status ;
typedef int ElemType ;
typedef struct{
ElemType *elem;
int length,listsize;
}SqList;
status InitList(SqList &L)//Initialize the
{
L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L.elem) exit(OVERFLOW);
L.listsize=LIST_INIT_SIZE;
L.length=0;
return OK;
}
status Build(SqList &L)//Set up table
{
int i,n;
printf(" Please enter the number of elements n and n An element n");
scanf("%d",&n);
if(n>LIST_INIT_SIZE)//If n is greater than the current space
{
L.elem=(ElemType *)realloc(L.elem,(n+LISTINCREMENT)*sizeof(ElemType));
if(!L.elem) exit(OVERFLOW);
L.listsize=n+LISTINCREMENT;
}
for(i=0;i<n;i++)
scanf("%d",L.elem+i);
L.length=n;
return OK;
}
void Print(SqList &L)//Output the elements and lengths in the table
{
int i;
for(i=0;i<L.length;i++)
printf("%d ",*(L.elem+i));
printf("n Length of :%dnn",L.length);
}
void Tips()//Prompt function
{
printf(" Please select the operation you want: n");
printf("<1> Output the sequence table and the length of the sequence table n");
printf("<2> Delete a value of x The node n");
printf("<3> Delete a given location i The node n");
printf("<4> Invert the order table n");
printf("<5> Sort the order table in ascending order n");
printf("<6> will x Insert into the appropriate place in the sequence table n");
printf("<7> Merge two ordered tables n");
printf("<0> exit nn");
}
status ListDelete1(SqList &L,int x)//Delete the element with value X
{
int i;
for(i=0;i<L.length;i++)
if(*(L.elem+i)==x)
break;
if(i==L.length)
return ERROR;
for(i++;i<L.length;i++)
*(L.elem+i-1)=*(L.elem+i);
L.length--;
return OK;
}
status ListDelete2(SqList &L,int x)//Delete the X th element
{
int i;
if(x<0||x>=L.length)
return ERROR;
for(i=x+1;i<L.length;i++)
*(L.elem+i-1)=*(L.elem+i);
L.length--;
return OK;
}
void Inverse(SqList &L)//Inverse set function
{
int i,t;
for(i=0;i<L.length/2;i++)
{
t=*(L.elem+i);
*(L.elem+i)=*(L.elem+L.length-i-1);
*(L.elem+L.length-i-1)=t;
}
}
void Sort(SqList &L)//Bubble sort (ascending order)
{
int i,j,t;
for(i=1;i<L.length;i++)
for(j=0;j<L.length-i;j++)
{
if(*(L.elem+j)>*(L.elem+j+1))
{
t=*(L.elem+j);
*(L.elem+j)=*(L.elem+j+1);
*(L.elem+j+1)=t;
}
}
printf(" In ascending order nn");
}
status ListInsert(SqList &L,int x)//I'm going to insert X so that it's still in order
{
int i,k;
if(L.length>=L.listsize)
{
L.elem=(ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!L.elem) exit(OVERFLOW);
L.listsize+=LISTINCREMENT;
}
for(i=0;i<L.length;i++)
if(x<*(L.elem+i))
break;
k=i;
for(i=L.length;i>k;i--)
*(L.elem+i)=*(L.elem+i-1);
*(L.elem+k)=x;
L.length++;
return OK;
}
status Merger(SqList &L,SqList &Lb)//Merge two linear tables
{
int i,j,k;
SqList Lc;
InitList(Lc);
if(Lc.listsize<L.length+Lb.length)
{
Lc.elem=(ElemType *)realloc(Lc.elem,(L.length+Lb.length+LISTINCREMENT)*sizeof(ElemType));
if(!L.elem) exit(OVERFLOW);
Lc.listsize=L.length+Lb.length+LISTINCREMENT;
}
i=j=k=0;
while(i<L.length && j<Lb.length)
{
if(*(L.elem+i) < *(Lb.elem+j))
{
*(Lc.elem+k)=*(L.elem+i);
k++;i++;
}
else
{
*(Lc.elem+k)=*(Lb.elem+j);
k++;j++;
}
}
while(i<L.length)
{
*(Lc.elem+k)=*(L.elem+i);
k++;i++;
}
while(j<Lb.length)
{
*(Lc.elem+k)=*(Lb.elem+j);
k++;j++;
}
Lc.length=L.length+Lb.length;
L=Lc;
return OK;
}
int main()
{
int op,x,flag;
SqList L,Lb;
InitList(L);
Build(L);
Tips();
scanf("%d",&op);
while(op)
{
switch(op)
{
case 1:
Print(L);
break;
case 2:
printf(" Please enter the data to be deleted X:n");
scanf("%d",&x);
flag=ListDelete1(L,x);
if(flag)
printf(" Delete the success !!nn");
else
printf(" Elements don't exist , Delete failed !!nn");
break;
case 3:
printf(" Please enter the location to delete i:n");
scanf("%d",&x);
flag=ListDelete2(L,x-1);//The index of the ith element is I minus 1
if(flag)
printf(" Delete the success !!nn");
else
printf(" Elements don't exist , Delete failed !!nn");
break;
case 4:
Inverse(L);
break;
case 5:
Sort(L);
break;
case 6:
printf(" Please enter the data to be inserted X:n");
scanf("%d",&x);
flag=ListInsert(L,x);
if(flag)
printf(" Insert the success !!nn");
else
printf(" Insert the failure !!nn");
break;
case 7:
printf(" Please enter the Lb The content of the :n");
InitList(Lb);
Build(Lb);
flag=Merger(L,Lb);
if(flag)
printf(" Merger success !!nn");
break;
}
Tips();
scanf("%d",&op);
}
return 0;
}