单链表逆置

来源:互联网 发布:python 中文字符串 编辑:程序博客网 时间:2024/06/02 01:59
#include<stdio.h>#include<stdlib.h>#include<string.h>#define MAX 100typedef struct student{    char name[MAX];    int numb;    struct student *pNext;}NODE,*PNODE;PNODE crateList();PNODE opp(PNODE pList);void traverseList(PNODE pList);int main(void){    PNODE pList;    pList=crateList();    traverseList(pList);    pList=opp(pList);    traverseList(pList);    return(0);}PNODE crateList(){    PNODE pHead=NULL,pCurrent,pPre;    char input[MAX];    puts("enter the name");    while(gets(input)!=NULL&&input[0]!='\0'){        pCurrent=(PNODE)malloc(sizeof(NODE));        if(pCurrent==NULL)            return(0);        if(pHead==NULL)            pHead=pCurrent;        else            pPre->pNext=pCurrent;        pCurrent->pNext=NULL;        strcpy(pCurrent->name,input);        puts("enter the number:");        scanf("%d",&pCurrent->numb);        pPre=pCurrent;        puts("enter the next one or enter to leave");        while(getchar()!='\n')            continue;    }    return(pHead);}void traverseList(PNODE pList){    PNODE pCurrent;    pCurrent=pList;    if(pCurrent==NULL)        puts("no data enter!");    else        while(pCurrent!=NULL){            printf("the name is %s,and number is %d\n",pCurrent->name,pCurrent->numb);            pCurrent=pCurrent->pNext;        }}PNODE opp(PNODE pList){    PNODE pFirst,pMid,pLast;    if(!pList||pList==NULL)        return(0);    pFirst=pList;    pMid=pFirst->pNext;    pFirst->pNext=NULL;    pLast=pMid->pNext;    pMid->pNext=pFirst;    while(pLast){        pFirst=pMid;        pMid=pLast;        pLast=pMid->pNext;        pMid->pNext=pFirst;    }    return(pMid);}

原创粉丝点击