Create a singly linked list and invert it (implemented in C language)

Problem description: Input n integers arbitrarily, with 0 as the terminator, create a singly linked list output with the lead node consistent with the input order; then output the table in in-situ spatial reverse order.

###flow chartInsert picture description here

#include<stdio.h>

struct sqlist
{
    
    
    int data[50];
    int last;
};

typedef struct sqlist SQLIST;///typedef 是为一种数据类型定义一个新名字
SQLIST w;///定义链表
int x;

SQLIST insert(SQLIST v,int x)///返回一个SQLIST类型的变量(小到大排过序的链表)
{
    
    
    int i;
    v.data[0]=x;
    i=v.last;
    while(v.data[i]>x)///此循环是排序(小->大)
    {
    
    
        v.data[i+1]=v.data[i];
        i--;
    }
    v.data[++i]=x;///安置x在适当位置
    v.last++;///表长加一
    return v;
}
SQLIST invert(SQLIST v)///返回一个SQLIST类型的变量
{
    
    
    int t,i,j;
    for(i=1,j=v.last;i<j;i++,j--)///此循环是前后交换
    {
    
    
        t=v.data[i];
        v.data[i]=v.data[j];
        v.data[j]=t;
    }
    return v;
}
void main()
{
    
    
    int i,n=0;
    w.last=0;
    x=9999;
    printf("Input data:\n");
    while(x!=0)///输入链表数据
    {
    
    
        scanf("%d",&x);
        w=insert(w,x);
    }
    printf("顺序(小->大)");
    for(i=2;i<=w.last;i++)///输出
        printf("%4d",w.data[i]);
    printf("\n");
    w=invert(w);
    printf("逆序(大->小)");
    for(i=1;i<w.last;i++)///输出
        printf("%4d",w.data[i]);
    printf("\n");
}

Welcome everyone to correct me!

Guess you like

Origin blog.csdn.net/weixin_46250447/article/details/106677969