如何将顺序表改为逆序表

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct Node{ int data; int len; struct Node *pNext;}NODE,*PNODE;
PNODE creat_list(int len)//创建链表, {
int i; int val;
PNODE pHead = (PNODE)malloc(sizeof(NODE));
if (NULL == pHead){
printf("分配失败, 程序终止!\n"); exit(-1);} PNODE pTail = pHead;
pTail->pNext = NULL;
for (i=0; i<len; ++i){scanf("%d", &val); PNODE pNew = (PNODE)malloc(sizeof(NODE)); if (NULL == pNew) { printf("分配失败, 程序终止!\n"); exit(-1);} pNew->data = val;
pTail->pNext = pNew;
pNew->pNext = NULL;
pTail = pNew; }return pHead;}
void traverse_list(PNODE pHead,int len)//遍历链表{ PNODE p = pHead->pNext; int count=1; while(NULL!=p){
if(count==len) { printf("%d",p->data); } else { printf("%d ",p->data); } p = p->pNext; count++; } printf("\n"); return;}
int main(){ int len; while(~scanf("%d",&len)) { PNODE pHead=NULL;//等价于struct Node *pHead= NULL //创建链表creat_list2(); pHead=creat_list(len);//创建一个非循环单链表,并将其该链表的头结点指向PHead traverse_list(pHead,len);//遍历整个链表 free(pHead); } return 0;}

运行结果及代码如下:

img

代码:

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct Node
{
    int data;
    int len;
    struct Node* pNext;
}NODE, * PNODE;
PNODE creat_list(int len)//创建链表, 
{
    int i; int val;
    PNODE pHead = (PNODE)malloc(sizeof(NODE));
    if (NULL == pHead) {
        printf("分配失败, 程序终止!\n"); exit(-1);
    }
    PNODE pTail = pHead;
    pTail->pNext = NULL;
    for (i = 0; i < len; ++i) 
    {
        scanf("%d", &val);
        PNODE pNew = (PNODE)malloc(sizeof(NODE));
        if (NULL == pNew) { printf("分配失败, 程序终止!\n"); exit(-1); }
        pNew->data = val;
        pTail->pNext = pNew;
        pNew->pNext = NULL;
        pTail = pNew;
    }
    return pHead;
}
void traverse_list(PNODE pHead, int len)//遍历链表
{
    PNODE p = pHead->pNext;
    int count = 1;
    while (NULL != p)
    {
        if (count == len)
        {
            printf("%d", p->data);
        }
        else
        {
            printf("%d ", p->data);
        }
        p = p->pNext;
        count++;
    }
    printf("\n");
    return;
}


//逆序
void reverse(PNODE pHead)
{
    PNODE p,  t,q;
    p = pHead->pNext;
    if (p == 0 || p->pNext == 0) return;

    t = p->pNext;
    p->pNext = 0; 

    while (t)
    {
        pHead->pNext = t;
        
        q = t->pNext;
        t->pNext = p;
        p = t;
        t = q;
    }
}


int main() {
    int len;
    while (~scanf("%d", &len))
    {
        PNODE pHead = NULL;//等价于struct Node *pHead= NULL 
        //创建链表creat_list2(); 
        pHead = creat_list(len);//创建一个非循环单链表,并将其该链表的头结点指向PHead 
        traverse_list(pHead, len);//遍历整个链表 

        //逆序
        printf("\n逆序后:");
        reverse(pHead);
        traverse_list(pHead, len);//遍历整个链表 
        free(pHead);
    }
    return 0;
}




用一个整数指针存储顺序表的数据,然后将值逆序赋给一个新的链表,以完成链表逆序的方法不知道满足你的需求不?修改如下:

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct Node{ 
int data;
 int len;
struct Node *pNext;
}NODE,*PNODE;

NODE * creat_list(int len)//创建链表,
{
    int i; int val;
    NODE* pHead = (NODE*)malloc(sizeof(NODE));
    
    if (NULL == pHead){
        printf("分配失败, 程序终止!\n"); 
        exit(-1);
        } 
        
    NODE* pTail = pHead;
    pTail->pNext = NULL;
    
    for (i=0; i<len; ++i){
        scanf("%d", &val); 
    //    printf("val=%d\n",val);
        NODE* pNew = (NODE*)malloc(sizeof(NODE)); 
        if (NULL == pNew) { 
            printf("分配失败, 程序终止!\n"); 
            exit(-1);
        } 
        pNew->data = val;
        pTail->pNext = pNew;
        pNew->pNext = NULL;
        pTail = pNew; 
    }
    return pHead;
}

void traverse_list(NODE * pHead,int len)//遍历链表
{ 
    NODE * p = pHead->pNext; 
    int count=1; 
    while(NULL!=p&&count<=5){    
//        if(count==len) {
//             printf("%d",p->data); 
//         }
//         else { 
             printf("%d ",p->data); 
    //    } 
        p = p->pNext; 
        count++; 
    } 
    
    printf("\n"); 
    return;
}

NODE * reverseList(NODE * pHead,int len){
    
    NODE * p =pHead;
    NODE * t;
    int * dataindex = (int *)malloc(len*sizeof(int));
    
    
    p=p->pNext;
    int j=0;
    while(p!=NULL){
        dataindex[j] = p->data;
    //    printf("dataindex[%d]=%d\n",j,dataindex[j]);
        j++;
    //    t=p;
        p=p->pNext;        
    }
    
    NODE * rp = (NODE*)malloc(sizeof(NODE));
    NODE * trp = rp;
    rp->pNext=NULL;
    
    int i=0;
    while(i<len){
        NODE * rnewp = (NODE * )malloc(sizeof(NODE));
        
        rnewp->data=dataindex[--j];
        trp->pNext=rnewp;
        rnewp->pNext=NULL;
        trp=rnewp;
        i++;
    }
    
    return rp;
    
    
}

int main(){
     int len=5; 

    PNODE pHead=NULL;//等价于struct Node *pHead= NULL 
  //创建链表

    pHead=creat_list(len);//创建一个非循环单链表,并将其该链表的头结点指向PHead 
    traverse_list(pHead,len);//遍历整个链表 
    NODE* p = reverseList(pHead,len);
    traverse_list(p,len);//遍历整个链表 
    free(pHead); 
    
   
   return 0;
}

供参考:

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct Node { 
    int data; 
    int len; 
    struct Node* pNext; 
}NODE, * PNODE;
PNODE creat_list(int len)//创建链表, 
{
    int i; 
    int val;
    PNODE pHead = (PNODE)malloc(sizeof(NODE));
    if (NULL == pHead) {
        printf("分配失败, 程序终止!\n"); 
        exit(-1);
    }
    PNODE pTail = pHead;
    pTail->pNext = NULL;
    for (i = 0; i < len; ++i) {
        scanf("%d", &val);
        PNODE pNew = (PNODE)malloc(sizeof(NODE));
        if (NULL == pNew) {
            printf("分配失败, 程序终止!\n");
            exit(-1);
        }
        pNew->data = val;
        pTail->pNext = pNew;
        pNew->pNext = NULL;
        pTail = pNew;
    }
    return pHead; 
}
void Reverseorder(PNODE pHead)  //逆序链表
{
    PNODE pH = pHead->pNext, pR = NULL;
    pHead->pNext = NULL;
    while (pH) {
        pR = pH;
        pH = pH->pNext;
        pR->pNext = pHead->pNext;
        pHead->pNext = pR;
    }
}
void traverse_list(PNODE pHead, int len)//遍历链表
{ 
    PNODE p = pHead->pNext; 
    int count=1; 
    while(NULL!=p){
        if (count == len) { 
            printf("%d", p->data); 
        }
        else { 
            printf("%d ", p->data); 
        } 
        p = p->pNext; 
        count++;
    } 
    printf("\n"); 
    return;
}
int main() {
    int len; 
    while (~scanf("%d", &len)) {
        PNODE pHead = NULL;//等价于struct Node *pHead= NULL  creat_list(len); //创建链表
        pHead = creat_list(len);//创建一个非循环单链表,并将其该链表的头结点指向PHead 
        traverse_list(pHead,len);//遍历整个链表 

        Reverseorder(pHead);    //逆序链表
        traverse_list(pHead, len);//遍历整个链表 
        free(pHead); 
    } 
    return 0;
}