单链表逆转操作

来源:互联网 发布:开单软件 编辑:程序博客网 时间:2024/04/28 00:07
#include<stdio.h>#include<stdlib.h>#include<stdbool.h>#include<string.h>typedef struct node{int data;struct node *next;}list_node,*list;void init(list *head){*head=(list)malloc(sizeof(list_node));(*head)->next=NULL;}bool is_empty(list head){if(head->next==NULL)return true;else return false;}void  creat(list head,int x){list new,tmp;new=(list)malloc(sizeof(list_node));new->data=x;tmp=head;while(tmp->next!=NULL)tmp=tmp->next;tmp->next=new;}void show(list tmp){if(is_empty(tmp))puts("the list is empty");else{tmp=tmp->next;while(tmp!=NULL){printf("%d ",tmp->data);tmp=tmp->next;}printf("\n");}}void convert(list L){if(is_empty(L))puts("the list is empty");else {list tmp,p;p=L->next;L->next=NULL;while(p!=NULL){tmp=p;p=p->next;tmp->next=L->next;L->next=tmp;}}}int main(void){list L;init(&L);puts("please input the data and output in reverse order");while(1){int ret,tmp;ret=scanf("%d",&tmp);if(ret!=1){show(L);break;}creat(L,tmp);show(L);}puts("the converted data are:\n");convert(L);show(L);return 0;}


原创粉丝点击