您的位置:首页 > 其它

单链表复习篇

2015-07-30 10:43 274 查看
/*

**********************************************************

*FileName:list.h

*Function:

*Version:version_1

*Author:yuanweikang

*Date:2015/7/30

*Note:

**********************************************************

*/

#ifndef LIST_H

#define LIST_H

#define FALSE 0

#define TRUE 1

#define Status int

#define DATATYPE int

typedef struct node

{

DATATYPE data;

struct node *next;

} Node;

typedef struct list

{

Node *head;

int length;

} List;

Node *CreateNode(DATATYPE data);

List *CreateList(DATATYPE data);

Status ClearList(List *list);

Status DestroyList(List **list);

Status AppendList(List *list, DATATYPE data);

Status AddList(List *list, DATATYPE data);

Status PrintList(List *list);

Status DeletePosList(List *list, int pos);

Status DeleteDataList(List *list, DATATYPE data);

Status InsertList(List *list, int pos, DATATYPE data);

Status BubbleSortNodeList(List *list);

Status BubbleSortDataList(List *list);

Status TravelList(List *list, Status (*func)(DATATYPE ));

Status PrintData(DATATYPE data);

Status ReverseList(List *list);

#endif

/*

**********************************************************

*FileName:list.c

*Function:

*Version:version_1

*Author:yuanweikang

*Date:2015/7/30

*Note:

**********************************************************

*/

#include <stdlib.h>

#include <stdio.h>

#include "list.h"

Node *CreateNode(DATATYPE data)

{

Node *newNode = (Node *)malloc(sizeof(Node));

if (newNode == NULL)

{

return NULL;

}

newNode->data = data;

newNode->next = NULL;

return newNode;

}

List *CreateList(DATATYPE data)

{

Node *newNode = CreateNode(data);

if (newNode == NULL)

{

return NULL;

}

List *newList = (List *)malloc(sizeof(List));

if (newList == NULL)

{

/*空间分配失败后,退出之前需要释放已经分配的空间*/

free(newNode);

newNode = NULL;

return NULL;

}

newList->head = newNode;

newList->length = 0;

return newList;

}

Status ClearList(List *list)

{

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

{

return FALSE;

}

Node *tempNode = list->head->next;

Node *oldNode = tempNode;

while (tempNode != NULL)

{

tempNode = tempNode->next;

free(oldNode);

oldNode = NULL;

oldNode = tempNode;

}

list->length = 0;

return TRUE;

}

Status DestroyList(List **list)

{

if ((list == NULL) || (*list == NULL) || ((*list)->head == NULL))

{

return FALSE;

}

if (ClearList(*list) == FALSE)

{

return FALSE;

}

Node *oldNode = (*list)->head;

free(oldNode);

oldNode = NULL;

free(*list);

*list = NULL;

list = NULL;

return TRUE;

}

Status AppendList(List *list, DATATYPE data)

{

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

{

return FALSE;

}

Node *newNode = CreateNode(data);

if (newNode == NULL)

{

return FALSE;

}

Node *tempNode = list->head;

while (tempNode->next != NULL)

{

tempNode = tempNode->next;

}

tempNode->next = newNode;

list->length++;

return TRUE;

}

Status AddList(List *list, DATATYPE data)

{

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

{

return FALSE;

}

Node *newNode = CreateNode(data);

if (newNode == NULL)

{

return FALSE;

}

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

{

list->head->next = newNode;

}

else

{

newNode->next = list->head->next;

list->head->next = newNode;

}

list->length++;

return TRUE;

}

int LengthList(List *list)

{

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

{

return -1;

}

Node *tempNode = list->head->next;

int length = 0;

while (tempNode != NULL)

{

length++;

tempNode = tempNode->next;

}

return length;

}

Status PrintList(List *list)

{

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

{

return FALSE;

}

Node *tempNode = list->head->next;

printf("list: ");

while (tempNode != NULL)

{

printf("%d ", tempNode->data);

tempNode = tempNode->next;

}

printf("\n");

printf("the length is:%d\n", list->length);

return TRUE;

}

Status DeletePosList(List *list, int pos)

{

if ((list == NULL) || (list->head == NULL) || (pos < 1) || (pos > list->length))

{

return FALSE;

}

int i = 0;

Node *tempNode = list->head;

Node *oldNode = tempNode->next;

for (i=1; i<=list->length; i++)

{

if (pos == i)

{

tempNode->next = oldNode->next;

free(oldNode);

oldNode = NULL;

list->length--;

return TRUE;

}

tempNode = tempNode->next;

oldNode = tempNode->next;

}

}

Status DeleteDataList(List *list, DATATYPE data)

{

if ((list == NULL) || (list->head == NULL) || (list->length == 0))

{

return FALSE;

}

Node *tempNode = list->head;

Node *oldNode = tempNode->next;

while (tempNode != NULL)

{

if (oldNode->data == data)

{

tempNode->next = oldNode->next;

free(oldNode);

oldNode = NULL;

list->length--;

return TRUE;

}

tempNode = tempNode->next;

oldNode = tempNode->next;

}

return FALSE;

}

Status InsertList(List *list, int pos, DATATYPE data)

{

if ((list == NULL) || (list->head == NULL) || (pos < 1) || (pos > list->length))

{

return FALSE;

}

Node *newNode = CreateNode(data);

if (newNode == NULL)

{

return FALSE;

}

Node *tempNode = list->head;

int i = 0;

for (i=1; i<list->length; i++)

{

if (i == pos)

{

newNode->next = tempNode->next;

tempNode->next = newNode;

list->length++;

return TRUE;

}

tempNode = tempNode->next;

}

}

Status BubbleSortNodeList(List *list)

{

if ((list == NULL) || (list->head == NULL) || (list->length <= 1))

{

return FALSE;

}

DATATYPE tempData = 0;

Node *tempNode = list->head->next;

Node *tailNode = NULL;

while (list->head->next->next != tailNode)

{

while (tempNode->next != tailNode)

{

if (tempNode->data > tempNode->next->data)

{

tempData = tempNode->data;

tempNode->data = tempNode->next->data;

tempNode->next->data = tempData;

}

tempNode = tempNode->next;

}

tailNode = tempNode;

tempNode = list->head->next;

}

return TRUE;

}

Status BubbleSortDataList(List *list)

{

if ((list == NULL) || (list->head == NULL) || (list->length <= 1))

{

return FALSE;

}

int i = 0;

int j = 0;

Node *tempNode = list->head->next;

DATATYPE tempData = 0;

for (i=0; i<list->length - 1; i++)

{

for (j=0; j<list->length - 1 - i; j++)

{

if (tempNode->data > tempNode->next->data)

{

tempData = tempNode->data;

tempNode->data = tempNode->next->data;

tempNode->next->data = tempData;

}

tempNode = tempNode->next;

}

tempNode = list->head->next;

}

return TRUE;

}

Status TravelList(List *list, Status (*func)(DATATYPE ))

{

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

{

return FALSE;

}

Node *tempNode = list->head->next;

printf("list: ");

while (tempNode != NULL)

{

if (func(tempNode->data) == FALSE)

{

return FALSE;

}

tempNode = tempNode->next;

}

printf("\n");

printf("the list length is:%d\n", list->length);

return TRUE;

}

Status PrintData(DATATYPE data)

{

printf("%d ", data);

return TRUE;

}

Status ReverseList(List *list)

{

if ((list == NULL) || (list->head == NULL) || (list->length <= 1))

{

return FALSE;

}

Node *tempNode = list->head->next;

Node *reverseNode = NULL;

while (tempNode->next != NULL)

{

reverseNode = tempNode->next;

tempNode->next = reverseNode->next;

reverseNode->next = list->head->next;

list->head->next = reverseNode;

}

return TRUE;

}

/*

**********************************************************

*FileName:main.c

*Function:

*Version:version_1

*Author:yuanweikang

*Date:2015/7/30

*Note:

**********************************************************

*/

#include <stdio.h>

#include "list.h"

int main(int argc, char *argv[])

{

int i = 0;

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

List *list = CreateList(0);

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

{

AddList(list, a[i]);

}

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

{

AppendList(list, a[i + 5]);

}

PrintList(list);

DeletePosList(list, 1);

PrintList(list);

DeleteDataList(list, 1);

PrintList(list);

InsertList(list, 1, 11);

printf("after insert\n");

PrintList(list);

//BubbleSortNodeList(list);

BubbleSortDataList(list);

printf("after bubble sort\n");

PrintList(list);

printf("LengthList=%d\n", LengthList(list));

InsertList(list, 5, 12);

printf("after insert\n");

TravelList(list, PrintData);

ReverseList(list);

printf("after reverse\n");

TravelList(list, PrintData);

DestroyList(&list);

if (PrintList(list) == FALSE)

{

printf("list destroy success\n");

}

return 0;

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