编辑代码

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int SData_int;
typedef struct seqList
{
    SData_int *sq;
    int size;
    int capacity;
}seqList;
//初始化
void InitseqList(seqList *p)
{
    assert(p);
    p->sq=NULL;
    p->size=0;
    p->capacity=0;
}
//判断是否要扩容
void CheckCapactiy(seqList *p)
{
    if(p->size == p->capacity)
    {
        p->capacity == 0 ? 4 : p->capacity*2;
        int newcapacity = p->capacity;
        SData_int *newA =(SData_int *)realloc(p->sq,sizeof(SData_int)*newcapacity);
        if(newA == NULL)
        {
            printf("CheckCapacity ERROR\n");
            exit(-1);
        }
        p->sq = newA;
        p->capacity = newcapacity;
    }
}
//尾插顺序表
void Insert_Rear_ByValue(seqList *p,int n)
{
    assert(p);
    CheckCapactiy(p);
    int val,size=0;
    while(n--)
    {
        scanf("%d",&val);
        p->sq[size]=val;
        p->size++;
        size++;
    }

}
//头插法
void Insert_Head_ByValue(seqList *p)
{
    assert(p);
    int val;
    CheckCapactiy(p);
    scanf("%d",&val);
    if(p->size == 0)
    {
        p->sq[0] = val;
        p->size++;
    }
    else
    {
        int end = p->size - 1;
        while(end >= 0)
        {
            p->sq[end+1]=p->sq[end];
            end--; 
        } 
        p->sq[0] = val;
        p->size++;
    }
 
}
//打印
void Printf_SeqList(seqList *p)
{
    int start=0;
    while(p->size != 0)
    {
        printf("%d ",p->sq[start]);
        start++;
        p->size--;
    }
    printf("\n");
}
int main () {
    seqList *p; 
    InitseqList(p);
    int n;
    scanf("%d",&n);
    Insert_Rear_ByValue(p,n);
    while(n--)
    {
        Insert_Head_ByValue(p);
    }
    
    Printf_SeqList(p);
	return 0;
}