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

数据结构与算法—单链表

2015-07-08 11:12 811 查看
/*

功能:

创建链表、链表的初始化、链表的长度、快慢指针查找中间值O(n/2)、插入、删除

时间:2015-07-07

人员:西瓜太郎

*/

#include <stdlib.h>

#include <stdio.h>

#define MAXSIZE 20

#define ElementType int

#define status int

struct Node;

typedef struct Node *PtrtoNode;

typedef PtrtoNode List;

typedef struct Node

{

    ElementType data;

    struct List *next;

};

int InitList(List *L)

{

    (*L) = malloc(sizeof(struct Node));

    (*L)->next = NULL;

    if(!*L)

        return 0;

    return 1;

}

unsigned int ListLength(List L)

{

    List tempList;

    int i = 0;

    tempList = L->next;

    while(tempList)

    {

        i++;

        tempList = tempList->next;

    }

    return i;

}

void printList(List L)

{

    List tempList;

    tempList = L;

    tempList = tempList->next;

    while(tempList)

    {

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

        tempList = tempList->next;

    }

    printf("\n");

}

void creatList(List L)

{

    List tempList,temp;

    int i = 1;

    temp = L;

    printf("尾插法创建链表:\n");

    while(i <= MAXSIZE)

    {

        tempList = malloc(sizeof(struct Node));

        tempList->data = rand()%100 + 1;

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

        

        temp->next = tempList;

        temp = tempList;

        i++;

    }

    temp->next = NULL;

    printf("\n");

    

}

status GetMiddleNode(List L, ElementType *e)

{

    List Middle, search,temp;

    Middle = search = L;

    while(search->next != NULL)

    {

        search = search->next;

        if(search->next != NULL)

        {

            search = search->next;

            Middle = Middle->next;

        }

        

    }

    *e = Middle->data;

    return 1;

}

List FindPreList(List L, int value)

{

    List Pre;

    int i = 0;

    Pre = L;

    if(value < 1 || value > ListLength(L) + 1)

    {

        printf("无法找到%d的前一个数!\n",value);

        exit(EXIT_FAILURE);

    }

    value--;

    while( Pre && i < value)

    {

        i++;

        Pre = Pre->next;

    }

    if(Pre)

        return Pre;

    else

        exit(EXIT_FAILURE);

}

void DeleteList(List L, int deleteItem)

{

    int i = 1;

    List PreList,temp;

    if( deleteItem < 1 || deleteItem > ListLength(L))

    {

        printf("删除数据在链表中不存在!!!\n");

        return ;

    }

    PreList = FindPreList(L,deleteItem);

    temp = PreList->next;

    PreList->next = temp->next;

    free(temp);

}

void InsertList(List L, int Item , int value)

{

    List Pre,InsertNode;

    InsertNode = malloc(sizeof(struct Node));

    InsertNode->data = value;

    if(Item < 0 || Item >ListLength(L))

    {

        printf("插入的位置不对!!!\n");

        return ;

    }

    Pre = FindPreList(L, Item);

    InsertNode->next = Pre->next;

    Pre->next = InsertNode;

}

int main()

{

    List L;

    int item, MiddleElement,DeleteItem,InsertItem,value;

    if(InitList(&L))

        printf("初始化链表成功!\n");

    else

    {

        printf("初始化链表失败!\n");

        return 0;

    }

    printf("初始化L后:ListLength(L) = %d\n",ListLength(L));

    while(1)

    {

        printf("1.查看链表\n");

        printf("2.创建链表(尾插法)\n");

        printf("3.删除链表中的值\n");

        printf("4.向链表中插入值\n");

        printf("5.链表长度\n");

        printf("6.中间节点值\n");

        printf("7.退出\n");

        printf("请选择你的操作:");

        scanf("%d",&item);

        switch(item)

        {

        case 1:

            printList(L);

            break;

        case 2:

            creatList(L);

            break;

        case 3:

            printf("删除第几个元素:");

            scanf("%d",&DeleteItem);

            DeleteList(L,DeleteItem);

            printList(L);

            break;

        case 4:

            printf("插入到第几个元素前、值为多少【X Y】:");

            scanf("%d %d",&InsertItem,&value);

            InsertList(L,InsertItem,value);

            printList(L);

            break;

        case 5:

            printf("链表的长度:%d\n",ListLength(L));

            break;

        case 6:

            GetMiddleNode(L,&MiddleElement);

            printf("中间节点:%d\n",MiddleElement);

            break;

        case 7:

            return 0;

            break;

        default:

            break;

        }

    }

    return 0;

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