程序要求:
实现链表的逆序,及简单的链表操作说明(如链表的创建,删除、打印等操作)。
具体代码如下:
点击(
此处)折叠或打开
- #include <stdio.h>
- #include <stdlib.h>
- struct node
- {
- int data;
- struct node *next;
- };
- struct node *create_list(void)
- {
- struct node *head;
- head = (struct node *)malloc(sizeof(struct node));
- head->next = NULL;
- return head;
- }
- void insert_list(struct node *head, int data)
- {
- struct node *new;
- new = (struct node *)malloc(sizeof(struct node));
- new->data = data;
- new->next = head->next;
- head->next = new;
- return ;
- }
- void print_list(struct node *head)
- {
- struct node *p = head->next;
- while(p != NULL)
- {
- printf("%d ", p->data);
- p = p->next;
- }
- printf("\n");
- return ;
- }
- int reserve_list(struct node *head)
- {
- struct node *pe = NULL;
- struct node *ps = head->next;
- head->next = NULL;
- while(ps != NULL)
- {
- pe = ps->next;
- ps->next = head->next;
- head->next = ps;
- ps = pe;
- }
- return 0;
- }
- int main(int argc, const char *argv[])
- {
- int i = 0;
- struct node *head;
- head = create_list();
- for (i = 0; i < 10; i++)
- insert_list(head, i);
-
- print_list(head);
- reserve_list(head);
- print_list(head);
- return 0;
- }
阅读(411) | 评论(0) | 转发(0) |