【数据结构与算法】笔记四:链表

链表:

1.带头结点的链表

2.不带头结点的链表 

3.链表元素的插入(含头结点)

4.链表元素的查找(含头结点)

5.链表元素的删除(含头结点)

废话不多说,直接上代码,均经过编译有效:


//*****************************************************************************************
//链表
//
//含有头结点
//*****************************************************************************************
#include <stdio.h>
#include <stdlib.h>

typedef struct List
{
    int data;
    struct List *next;
}list;

list *InitListHead()
{
    list *l = (list *)malloc(sizeof(list));
    list *head = l;
    l->next = NULL;

    for(int i=1;i<5;i++)
    {
        list *new = (list *)malloc(sizeof(list));
        scanf("%d",&new->data);
        new->next = NULL;
        l->next = new;
        l = l->next;
    }
    return head;
}

void DisplayHead(list *l)
{
    list *temp = l;
    printf("**************************** \n");
    while(temp->next!=NULL)
    {
        temp = temp->next;
        printf("%d \n",temp->data);
    }
}

int main(void)
{
    list *l = InitListHead();
    DisplayHead(l);

    return 0;
}

//*****************************************************************************************
//链表
//
//不含有头结点
//*****************************************************************************************
#include <stdio.h>
#include <stdlib.h>

typedef struct List
{
    int data;
    struct List *next;
}list;

list *InitList()
{
    list *l = (list *)malloc(sizeof(list));
    list *temp = l;
    scanf("%d",&list->data);//没有头结点,都是含数据节点
    l->next = NULL;

    for(int i=1;i<5;i++)
    {
        list *new = (list *)malloc(sizeof(list));
        scanf("%d",&new->data);
        new->next = NULL;
        l->next = new;
        l = l->next;
    }
    return temp;
}

void Display(list *l)
{
    list *temp = l;

    while(temp!=NULL)
    {
        printf("%d",temp->data);
        temp = temp->next;
    }
}

int main(void)
{
    List *l = Initlist();
    Display(l);

    return 0;
}

//*****************************************************************************************
//链表
//
//含有头结点,插入
//*****************************************************************************************
#include <stdio.h>
#include <stdlib.h>

typedef struct List
{
    int data;
    struct List *next;
}list;

list *InitListHead()
{
    list *l = (list *)malloc(sizeof(list));
    list *head = l;
    l->next = NULL;

    for(int i=1;i<5;i++)
    {
        list *new = (list *)malloc(sizeof(list));
        scanf("%d",&new->data);
        new->next = NULL;
        l->next = new;
        l = l->next;
    }
    return head;
}

void DisplayHead(list *l)
{
    list *temp = l;
    while(temp->next!=NULL)
    {
        temp = temp->next;
        printf("%d \n",temp->data);
    }
}

int InsertData(list *l,int num,int data)
{
    list *temp = l;
    list *new = (list *)malloc(sizeof(list));
    new->data = data;
    new->next = NULL;

    for(int i=1;i<num;i++)
    {
        if(temp == NULL)
        {
            printf("there doesn't have enough list number \n");
            return -1;
        }
        temp = temp->next;
    }
    new->next = temp->next;
    temp->next = new;
    return 1;
}

int main(void)
{
    list *l = InitListHead();
    DisplayHead(l);

    return 0;
}

//*****************************************************************************************
//链表
//
//含有头结点,查找
//*****************************************************************************************

#include <stdio.h>
#include <stdlib.h>

typedef struct List
{
    int data;
    struct List *next;
}list;

list *InitListHead()
{
    list *l = (list *)malloc(sizeof(list));
    list *head = l;
    l->next = NULL;

    for(int i=1;i<5;i++)
    {
        list *new = (list *)malloc(sizeof(list));
        scanf("%d",&new->data);
        new->next = NULL;
        l->next = new;
        l = l->next;
    }
    return head;
}

void DisplayHead(list *l)
{
    list *temp = l;
    printf("********************************** \n");
    while(temp->next!=NULL)
    {
        temp = temp->next;
        printf("%d \n",temp->data);
    }
}

int FindData(list *l,int data)//通过数据查找
{
    list *temp = l;
    int num = 0;
    
    while(temp != NULL)
    {
		temp = temp->next;
        num++;
        if(temp->data == data)
        {
            return num;
        }
    }
    printf("doesn't find the data in the list \n");
    return -1;
}

int FindNum(list *l,int num)
{
    list *temp = l;
    
    for(int i=0;i<num;i++)
    {
        temp = temp->next;
        if(temp == NULL)
        {
            printf("there are no enough num in the list \n");
            return -1;
        }
    }
    return temp->data;
}

int main(void)
{
    list *l = InitListHead();
    int num;

    DisplayHead(l);
    FindData(l,3);
    printf("************************\n we search the num get the data %d \n",FindNum(l,2));

    return 0;
}

//*****************************************************************************************
//链表
//
//含有头结点,删除
//*****************************************************************************************

#include <stdio.h>
#include <stdlib.h>

typedef struct List
{
    int data;
    struct List *next;
}list;

list *InitListHead()
{
    list *l = (list *)malloc(sizeof(list));
    list *head = l;
    l->next = NULL;

    for(int i=1;i<6;i++)
    {
        list *new = (list *)malloc(sizeof(list));
        scanf("%d",&new->data);
        new->next = NULL;
        l->next = new;
        l = l->next;
    }
    return head;
}

void DisplayHead(list *l)
{
    list *temp = l;
    printf("***************************** \n");
    while(temp->next!=NULL)
    {
        temp = temp->next;
        printf("%d \n",temp->data);
    }
}

int DelData(list *l,int data)
{
    list *temp = l;

    while(temp != NULL)
    {
        temp = temp->next;
        if(temp->data == data)
        {
            list *p = temp->next;
            temp->data = temp->next->data;
            temp->next = temp->next->next;
            free(p);
            return 1;
        }
    }
    return -1;
}

int DelNum(list *l,int num)
{
    list *temp =l;
    if(num<1)
    {
        printf("input num is an illegal num \n");
        return -1;
    }

    for(int i=0;i<num-1;i++)
    {
        temp = temp->next;
        if(temp == NULL)
        {
            printf("there are no enough num \n");
            return -1;
        }
    }
    list *p = temp->next;
    temp->next = temp->next->next;
    free(p);
    return 1;
}

int main(void)
{
    list *l = InitListHead();

    DisplayHead(l);
    DelData(l,2);
    DisplayHead(l);
    DelNum(l,2);
    DisplayHead(l);

    return 0;
}

PS:本文不具有权威性,仅为个人学习笔记或有感而发,如有错误欢迎指正。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值