- #include<stdio.h>
- #include<stdlib.h>
- struct node
- {
- int num;
- struct node *next;
- };
- typedef struct node Node;
- typedef struct node * Link;
- void create_link(Link *head)
- {
- *head = NULL;
- }
- void insert_node_head(Link *head,Link new_node)
- {
- new_node->next = *head;
- *head = new_node;
- }
- void display_node(Link head)
- {
- Link tmp;
- tmp = head;
- if(NULL == head)
- {
- printf("link is empty!\n");
- return;
- }
- while(tmp != NULL)
- {
- printf("num = %d\n",tmp->num);
- tmp = tmp->next;
- }
- }
- void is_malloc_ok(Link new_node)
- {
- if(new_node == NULL)
- {
- printf("malloc error!\n");
- exit(-1);
- }
- }
- void create_newnode(Link * new_node)
- {
- *new_node = (Link)malloc(sizeof(Node));
- is_malloc_ok(*new_node);
- }
- void insert_node_tail(Link *head,Link new_node)
- {
- Link tmp;
- tmp = *head;
- if(*head == NULL)
- {
- new_node->next = *head;
- *head = new_node;
- }
- else
- {
- while(tmp->next != NULL)
- {
- tmp = tmp->next;
- }
- tmp->next = new_node;
- new_node->next = NULL;
- }
- }
- /*void insert_node_mid(Link *head,Link new_node,int num)
- {
- Link tmp;
- tmp = *head;
- if(NULL == *head)
- {
- printf("link is empty!\n");
- return;
- }
- else
- {
- while(tmp->num != num && tmp->next != NULL)
- {
- tmp = tmp->next;
- }
- if(tmp->num == num)
- {
- new_node->next = tmp->next;
- tmp->next = new_node;
- }
- else
- {
- printf("no such node!\n");
- }
- }
- }*/
- void delete_node(Link *head,int num)
- {
- Link tmp;
- Link p;
- tmp = p = *head;
- if(NULL == *head)
- {
- printf("link is empty!\n");
- return;
- }
- while(tmp->num != num && tmp->next != NULL)
- {
- p = tmp;
- tmp = tmp->next;
- }
- if(tmp->num == num)
- {
- if(tmp == *head)
- {
- *head = tmp->next;
- free(tmp);
- }
- else
- {
- p->next = tmp->next;
- free(tmp);
- }
- }
- else
- {
- printf("no such node!\n");
- }
- }
- void release(Link *head)
- {
- Link tmp;
- tmp = *head;
- /*while (tmp != NULL)
- {
- *head = tmp->next;
- free(tmp);
- tmp = tmp->next;
- }*/
- while(tmp != NULL)
- {
- *head = (*head)->next;
- free(tmp);
- tmp = *head;
- }
- }
- void insert_node_mid(Link *head,Link new_node,int num)
- {
- Link tmp;
- Link p;
- tmp = *head;
- p = *head;
- if(NULL == *head)
- {
- new_node->next = *head;
- *head = new_node;
- }
- else
- {
- while((num > tmp->num) &&(tmp->next != NULL))
- {
- p = tmp;
- tmp = tmp->next;
- }
- if(num <= tmp->num)
- {
- if(tmp == *head)
- {
- new_node->next = tmp;
- *head = new_node;
- }
- else
- {
- p->next = new_node;
- new_node->next = tmp;
- }
- }
- if((num > tmp->num)&&(tmp->next == NULL))
- {
- tmp->next = new_node;
- new_node->next = NULL;
- }
- }
- }
- void reverse_link(Link *head)
- {
- Link p1 = NULL;
- Link p2 = NULL;
- Link p3 = NULL;
- if(NULL == *head || (*head)->next == NULL)
- {
- printf("No need reverse!\n");
- return;
- }
- else
- {
- p3 = *head;
- p2 = p3->next;
- if(p2->next == NULL)
- {
- p2->next = p3;
- p3->next = NULL;
- *head = p2;
- return;
- }
- p1 = p2->next;
- p3->next = NULL;
- while(p1->next != NULL)
- {
- p2->next = p3;
- p3 = p2;
- p2 = p1;
- p1= p1->next;
- }
- p2->next = p3;
- p1->next = p2;
- *head = p1;
- }
- }
- int main()
- {
- Link head = NULL;
- Link new_node = NULL;
- int i;
- //int num;
- create_link(&head);
- //printf("Enter 10 number!\n");
- for(i = 0; i < 10;i++)
- {
- create_newnode(&new_node);
- new_node->num = i;
- //scanf("%d",&(new_node->num));
- insert_node_mid(&head,new_node,new_node->num);
- //insert_node_head(&head,new_node);
- //insert_node_tail(&head,new_node);
- }
- //printf("please input a num!\n");
- //scanf("%d",&num);
- // create_newnode(&new_node);
- // new_node->num = num;
- // insert_node_mid(&head,new_node,10);
- // delete_node(&head,num);
- display_node(head);
- //release(&head);
- reverse_link(&head);
- display_node(head);
- return 0;
- }
无表头节点的单向链表
最新推荐文章于 2023-04-01 17:35:53 发布