数据结构与算法—单链表

/*
功能:
创建链表、链表的初始化、链表的长度、快慢指针查找中间值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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值