如何利用结构体将数组倒置

如何利用结构体将数组倒置例如输入1 2 3 4 5 6
输出6 5 4 3 2 1

结构链表指针反向连接。

#include<stdio.h>
#include<malloc.h>
typedef struct stt
{
    int n;
    struct stt *next;
}STT;
int main( )
{
    int num,count=6;
    STT *sttHead=NULL,*sttNew=NULL;
    sttHead=(STT *)malloc(sizeof(STT));
    sttHead->next=NULL;
    printf("输入6个数字:");
    while(count-->0)
    {
        scanf("%d",&num);
        sttNew=(STT *)malloc(sizeof(STT));
        sttNew->next=NULL;
        sttNew->n=num;
        if(sttHead->next!=NULL)
            sttNew->next=sttHead->next;
        sttHead->next=sttNew;
    }

    while(sttHead->next!=NULL)
    {
        printf("%d ",sttHead->next->n);
        sttHead=sttHead->next;
    }

    return 0;
}
温馨提示:答案为网友推荐,仅供参考
相似回答