- #include <stdio.h>
- #include <stdlib.h>
- //#define CRT_SECURE_NO_WARNINGS
- #define LEN struct DataSequence
- struct DataSequence
- {
- int id;
- char commit[50];
- struct DataSequence * next;
- };
- //添加数据
- struct DataSequence * add(struct DataSequence * head)
- {
- struct DataSequence *p, *q;
- q = p = (struct DataSequence *)malloc(sizeof(struct DataSequence));
- if(head == NULL)
- {
- head = q;
- }
- else
- {
- p = head;
- while(p -> next != NULL)
- {
- p = p -> next;
- }
- p -> next = q;
- }
- printf("请输入编号:");
- scanf("%d", &q -> id);
- printf("请输入备注:");
- scanf("%s", q -> commit);
- q -> next = NULL;
- return head;
- }
- // 就地逆置
- struct DataSequence * reverse(struct DataSequence * head)
- {
- struct DataSequence * p, * q, * temp;
- temp = q = p = head;
- if(head == NULL)
- {
- printf("糟糕,该链表还没有数据\n\n");
- return head;
- }
- p = p -> next;
- temp -> next = NULL;
- q = temp;
- head = q;
- while(p != NULL)
- {
- temp = p;
- p = p -> next;
- temp -> next = q;
- q = temp;
- head = q;
- }
- return head;
- }
- // 求链表长度
- void length(struct DataSequence * head)
- {
- int n = 0;
- struct DataSequence * p = head;
- if(p == NULL)
- {
- n = 0;
- }
- while(p != NULL)
- {
- n++;
- p = p -> next;
- }
- printf("长度为:%d\n\n",n);
- }
- // 遍历链表并打印输出
- void print(struct DataSequence * head)
- {
- struct DataSequence * p = head;
- if(p == NULL)
- {
- printf("没有发现数据\n\n");
- }
- while(p != NULL)
- {
- printf("\t编号:%d, 备注:%s\n", p -> id, p -> commit);
- p = p -> next;
- }
- }
- void main()
- {
- struct DataSequence * head = (struct DataSequence *)malloc(sizeof(LEN));
- int input;
- head = NULL;
- while(1)
- {
- printf("\t1.添加\n");
- printf("\t2.逆序\n");
- printf("\t3.输出\n");
- printf("\t4.长度\n");
- printf("\t0.退出\n");
- scanf("%d", &input);
- if(input == 1)
- {
- head = add(head);
- }
- else if(input == 2)
- {
- head = reverse(head);
- }
- else if(input == 3)
- {
- print(head);
- }
- else if(input == 4)
- {
- length(head);
- }
- else if(input == 0)
- {
- printf("\t您选择了退出操作\n");
- exit(0);
- }
- }
- }
转载于:https://blog.51cto.com/fylxopensource/1154459