您的位置:首页 > 理论基础 > 数据结构算法

第十三章 数据结构基础--单链表

2014-11-26 11:27 302 查看

1.1 单链表的创建

尾部加入法创建单链表:头节点、尾节点、中间节点;头节点与尾节点的初始化。



#include <stdlib.h>

typedef struct student {

int age;

struct student *next;

}Stu;

Stu *SingleListTailCreate() {

Stu *head = NULL, *tail = NULL, *cur = NULL;

int cycle = 1, x = 0, j = 0;

head = (Stu *)malloc(sizeof(Stu));

head->age = 1;

tail = head;

while(cycle) {

printf("please input data: \n");

scanf("%d", &x);

if (x != 0) {

cur = (Stu *)malloc(sizeof(Stu));

cur->age = x;

tail->next = cur;

tail = cur;

} else {

cycle = 0;

}

}

tail->next = NULL;

return head;

}

1.2 单链表的打印与测长

void PrintSingleList(Stu *head) {

Stu *cur = head;

if (head == NULL)

printf("the list is null.\n");

while (cur != NULL) {

printf("cur->age: %d\n", cur->age);

cur = cur->next;

}

}

int LenSingleList(Stu *head) {

int len = 0;

Stu *cur = head;

if (head == NULL) {

return len;

}

while (cur != NULL){

cur = cur->next;

len++;

}

return len;

}

1.3 单链表的插入

单链表的插入,必须有专门的指针pre来记录插入时的前驱节点;
以及插入节点cur的前驱与后驱节点的配线。



//index is [1, len-1]

Stu *SingleListInsert(Stu *head, int age, int index) {

int i = 0, len = 0;

Stu *pre = head, *cur = NULL;

if (head == NULL) {

return NULL;

}

len = LenSingleList(head);

if((index < 1) || (index > (len-1))) {

printf("position is wrong.\n");

return head;

}

for (i = 1; i < index; i++) {

pre = pre->next;

}

cur = (Stu *)malloc(sizeof(Stu));

cur->age = age;

//following two lines can not change each other.

cur->next = pre->next;

pre->next = cur;

return head;

}

1.4 单链表的删除

单链表的删除,必须有专门的指针pre来记录删除节点的前驱节点;
以及要删除的节点cur。



Stu *SingleListDel(Stu *head, int age) {

Stu *cur = NULL, *pre= NULL;

if (head == NULL)

return head;

cur = head;

while((cur->age != age) && (cur->next != NULL) ) {

pre = cur;

cur = cur->next;

}

if(cur->age == age) {

if (cur == head) {

head = head->next;

} else {

pre->next = cur->next;

}

free(cur);

} else {

printf("not found.\n");

}

return head;

}

1.5 单链表的逆置

逆置需要指定三节点:前驱节点pre、后驱节点nex与当前节点cur。

Stu *SingleListVerse(Stu *head) {

Stu *pre = NULL, *cur = NULL, *nex = NULL;

if(head == NULL)

return head;

if(head->next == NULL)

return head;

cur = head;

while(cur != NULL) {

nex = cur->next;

cur->next = pre;

pre = cur;

cur = nex;

}

return pre;

}

1.6 单链表的销毁

void FreeSingleList(Stu *head)

{

Stu *temp = NULL;

if (head == NULL) {

return;

}

while (head != NULL) {

temp = head->next;

free(head);

head = temp;

}

}

1.7 单链表的排序

例题1:数组的冒泡排序

冒泡排序的基本思想:两两比较相邻记录的关键字,如果反序则交换,直到没有反序的记录为止。
外循环控制遍历数组的次数;内循环控制相邻两数组的比较次数。
int *Bobble1(int *p, int len) {

int i = 0, j = 0, tmp = 0;

for(i = 0; i < len; i++)

for(j = 0; j < len - i - 1; j++) {

if (p[j] > p[j+1]) {

tmp = p[j];

p[j] = p[j+1];

p[j+1] = tmp;

}

}

return p;

}

例题2: 单链表的冒泡排序

Stu *BobbleList1(Stu *head) {

Stu *cur = NULL;

int i = 0, j = 0, tmp = 0;

if (head == NULL | head->next == NULL)

return head;

int len = LenSingleList(head);

for(i = 0; i < len; i++) {

//locate the head node.

cur = head;

for(j = 0; j < len - i - 1; j++) {

//change the element

if(cur->age > cur->next->age) {

tmp = cur->age;

cur->age = cur->next->age;

cur->next->age = tmp;

}

//node's movement

cur = cur->next;

}

}

return head;

}

1.8 main调用

int main() {

Stu *head = NULL;

int len = 0, age = 2, index = 2;

head = SingleListTailCreate();

PrintSingleList(head);

len = LenSingleList(head);

int a[7] = {1,5,9,6,4,3,4};

Bobble1(a, 7);

head = BobbleList1(head);

printf("After sort: \n");

PrintSingleList(head);

printf("length of list: %d\n", len);

head = SingleListInsert(head, age, index);

PrintSingleList(head);

len = LenSingleList(head);

printf("length of list: %d\n", len);

head = SingleListDel(head, age);

PrintSingleList(head);

len = LenSingleList(head);

printf("length of list: %d\n", len);

head = SingleListVerse(head);

PrintSingleList(head);

len = LenSingleList(head);

printf("length of list: %d\n", len);

FreeSingleList(head);

head = NULL;

len = LenSingleList(head);

printf("length of list: %d\n", len);

return 0;

}

其运行结果如图所示:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: