单向循环链表

来源:互联网 发布:开单软件 编辑:程序博客网 时间:2024/05/17 06:24
#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;}list  creat(list head,int x){list new, tmp;new = (list)malloc(sizeof(list_node));new->data = x;new->next = NULL;tmp = head;while(tmp->next != NULL)tmp = tmp->next;tmp->next = new;return new;}void show(list tmp){if(is_empty(tmp))puts("the list is empty");else{list p=tmp;printf("%d ",tmp->data);tmp=tmp->next;while(tmp!=p){printf("%d ",tmp->data);tmp=tmp->next;}printf("\n");}}void output(list head){int i=1;list p=NULL;list tmp=head;while(tmp!=tmp->next){tmp=tmp->next;i++;if(i==2){p=tmp;continue;}if(i==3){i = 0;printf("%d ",tmp->data);p->next=tmp->next;free(tmp);tmp=p;}}printf("\nthe last data is:%d \n",tmp->data);}int main(void){list L;list last;init(&L);puts("please input the data and couts 3 to output");while(1){int ret, tmp;ret = scanf("%d",&tmp);if(ret != 1){break;}last = creat(L,tmp);}L = L->next;last->next = L;show(L);puts("when counts to 3 output");output(L);return 0;}


原创粉丝点击