C language input method list head backward interpolation

Input: 12345-1

Output: 54321 

One of the founders of this title examines the head of the list: the first interpolation. The so-called interpolation starting from the head of an empty list, repeated read data to generate a new node, the read data stored in the data field of the new node, the new node is inserted and then speak after the current list to the first node, sign up to read until the end.

 

#include <stdio.h>
#include <stdlib.h>

 

typedef struct Node
{
int data ;
struct Node * pNext ;
}* PNODE ,NODE ;
PNODE creat_list(void) ;
void show_list(PNODE phead) ;

int main()
{
PNODE phead = NULL ;
phead = creat_list() ;
show_list(phead) ;
return 0 ;
}
PNODE creat_list(void)
{
int val;
PNODE phead = (PNODE)malloc(sizeof(NODE)) ; //为头指针开辟内存空间
phead->pNext = NULL ; //初始化空链表
// PNODE pNew =NULL ;//初始化新结点
while(1)
{
scanf("%d",&val);
if(val<0 ) break ;
PNODE pNew =(PNODE)malloc(sizeof(NODE)) ; //// 为新结点开辟内存空间
pNew->data = val ;
pNew->pNext = phead->pNext ;//! 将头指针所指向的下一个结点的地址,赋给新创建结点的next
phead->pNext = pNew ; //!把新结点挂到头结点后面(插入)

}

return phead ;
}
void show_list(PNODE phead)
{
PNODE p = phead->pNext ;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->pNext ;
}
printf("\n");
}

 

Guess you like

Origin www.cnblogs.com/cocobear9/p/12241575.html