#include <stdio.h>
#include <stdlib.h>
typedef struct {
int info;
} DATA;
typedef struct node {
DATA data;
struct node* next;
} NODE;
void init(NODE** head) {
*head = NULL;
}
void print_list(NODE* head) {
NODE * temp;
for (temp = head; temp; temp = temp->next)
printf("%5d", temp->data.info);
}
NODE* add(NODE* node, DATA data) {
NODE* temp = (NODE*) malloc(sizeof (NODE));
if (temp == NULL) {
exit(0); // no memory available
}
temp->data = data;
temp->next = node;
node = temp;
return node;
}
void add_at(NODE* node, DATA data) {
NODE* temp = (NODE*) malloc(sizeof (NODE));
if (temp == NULL) {
exit(EXIT_FAILURE); // no memory available
}
temp->data = data;
temp->next = node->next;
node->next = temp;
}
void remove_node(NODE* head) {
NODE* temp = (NODE*) malloc(sizeof (NODE));
if (temp == NULL) {
exit(EXIT_FAILURE); // no memory available
}
temp = head->next;
head->next = head->next->next;
free(temp);
}
NODE * reverse_rec(NODE * ptr, NODE * previous) {
NODE * temp;
if (ptr->next == NULL) {
ptr->next = previous;
return ptr;
} else {
temp = reverse_rec(ptr->next, ptr);
ptr->next = previous;
return temp;
}
}
NODE * reverse(NODE * node) {
NODE * temp;
NODE * previous = NULL;
while (node != NULL) {
temp = node->next;
node->next = previous;
previous = node;
node = temp;
}
return previous;
}
NODE *free_list(NODE *head) {
NODE *tmpPtr = head;
NODE *followPtr;
while (tmpPtr != NULL) {
followPtr = tmpPtr;
tmpPtr = tmpPtr->next;
free(followPtr);
}
return NULL;
}
NODE *sort_list(NODE *head) {
NODE *tmpPtr = head, *tmpNxt = head->next;
DATA tmp;
while (tmpNxt != NULL) {
while (tmpNxt != tmpPtr) {
if (tmpNxt->data.info < tmpPtr->data.info) {
tmp = tmpPtr->data;
tmpPtr->data = tmpNxt->data;
tmpNxt->data = tmp;
}
tmpPtr = tmpPtr->next;
}
tmpPtr = head;
tmpNxt = tmpNxt->next;
}
return tmpPtr;
}
int main() {
int i;
NODE* head;
NODE* node;
DATA element;
printf("Add Elements to List:\n");
init(&head);
for (i = 53; i <= 63; i++) {
element.info = i;
printf("Add Element %2d To The List.\n", element.info);
head = add(head, element);
}
printf("\nPrint The List:\n");
print_list(head);
printf("\nRemove Element From The List:\n");
node = head->next->next;
remove_node(node);
printf("\nAdd Element To The List:\n");
node = head->next->next->next;
element.info = 2000;
add_at(node, element);
head = reverse(head); // Revers The List
head = sort_list(head); // Sort The List
head = free_list(head);
return (EXIT_SUCCESS);
}
```w
2013笔试
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 来自:http://blog.sina.com.cn/s/blog_a7a1eb490101hsoc.html 简...
- 此文为转载,原文戳这里:http://mianshiti.diandian.com/post/2013-04-20...