/**
* This c Project just for test
* 链表操作、排序
* author :pointer
* date:2015-12-22
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct _Simple_link {
int data;
struct _simple_link * next;
} single_link, *Sing_link;
single_link *create_Link();
int insert_link(single_link *head, int valude);
void print_link(single_link *head);
single_link* delete_link(single_link *head, int position);
int length_link(single_link* head);
single_link *reverse_link(single_link *head);
void bubble_sort(int a[], int len);
int main() {
int i = 0;
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
bubble_sort(a, 10);
single_link *head = NULL;
single_link *temp_head = NULL;
head = create_Link();
for (i = 0; i < 10; i++) {
insert_link(head, a[i]);
}
print_link(head);
head = delete_link(head, 9);
print_link(head);
temp_head = reverse_link(head);
print_link(temp_head);
printf("Press any key to continue...\n");
getchar();
return 0;
}
//creat a link head
single_link *create_Link() {
single_link *head = NULL;
head = (single_link*) malloc(sizeof(single_link));
if (head == NULL) {
printf("创建失败");
return NULL;
}
head->data = 0;
head->next = NULL;
return head;
}
//插入
int insert_link(single_link *head, int value) {
single_link* temp = NULL;
if (NULL == head) {
return 0;
}
temp = (single_link*) malloc(sizeof(single_link));
if (NULL == temp) {
printf("创建失败");
return 0;
}
while (head->next != NULL) {
head = head->next;
}
temp->data = value;
temp->next = NULL;
head->next = temp;
return 1;
}
//遍历
void print_link(single_link *head) {
if (head == NULL || head->next == NULL) {
printf("请输入正确的链表\n");
}
single_link* temp_head = NULL;
temp_head = head->next;
printf("链表的值为:");
while (temp_head->next != NULL) {
printf("%d\t", temp_head->data);
temp_head = temp_head->next;
}
printf("%d\n", temp_head->data);
// printf("\n");
}
//计算链表的长度
int length_link(single_link* head) {
if (head == NULL) {
printf("请输入正确的链表\n");
return 0;
}
int i = 0;
while (head->next != NULL) {
head = head->next;
i++;
}
return i;
}
//删除指定位置的元素
single_link* delete_link(single_link *head, int position) {
if (head == NULL) {
printf("请输入正确的链表\n");
return NULL;
}
int index = 0;
single_link* temp = NULL;
single_link* temp_head = head;
if (position <= length_link(head)) {
while (index < position - 2) {
index++;
head = head->next;
}
temp = head;
head = head->next;
printf("删除位置元素:%d\t %d\n", position, head->data);
temp->next = head->next;
free(head);
return temp_head;
} else {
printf("输入的长度无效\n");
return NULL;
}
}
single_link *reverse_link(single_link *head) {
if (head == NULL) {
printf("请输入正确的链表\n");
return NULL;
}
single_link* temp_next = NULL;
single_link* temp_head = NULL;
temp_head = head->next;
head->next = NULL;
while (temp_head->next != NULL) {
temp_next = temp_head->next;
temp_head->next = head;
head = temp_head;
temp_head = temp_next;
}
temp_head->next = head;
head = temp_head;
return head;
}
//冒泡排序
void bubble_sort(int a[], int len) {
int inner_index, wild_index;
int temp;
for (wild_index = 0; wild_index < len; wild_index++) {
for (inner_index = wild_index + 1; inner_index < len; inner_index++) {
if (a[inner_index] > a[wild_index]) {
temp = a[inner_index];
a[inner_index] = a[wild_index];
a[wild_index] = temp;
}
}
}
printf("排序后的值为:");
for (wild_index = 0; wild_index < len; wild_index++) {
printf("%d\t", a[wild_index]);
}
printf("\n");
}
单链表的操作(练习)
最新推荐文章于 2024-09-23 20:06:36 发布