链表的学习笔记

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

/*

1.创建

2.插入

3.排序

*/




typedef struct student

{

int data;                      //数据域名

struct student *next;          //next指针

}node;



//创建链表

node *creat(void)

{

    node*  head;

head=(node*)malloc(sizeof(node));       //malloc开辟空间并且返回头指针

head->next=NULL;                        //设尾指针指向空

return head;                            //返回头指针

}


//插入链表

node *insert(node *head, int num)

{

    node *p,*st,*next1,*news;

    st = head;

    p=(node*)malloc(sizeof(node));

    while((st->data < num)&&(st->next != NULL))

    {

        next1 = st;

        st = st -> next;

    }

if(st->next == NULL&&st->data

    {

        st->next = p;

        p->data  = num;

        p->next  = NULL;

        return head;

    }else{

if(st==head)      //判断插入位置是不是链首

            {

                p->next=st;

                p->data=num;

                return p;

}else{            //判断插入位置是不是链表中间

            p->next=st;

            p->data=num;

            next1->next=p;

            return head;

            }

    }

}


//计算链表长度

int length(node *head)

{

    node *p;

    p=head;

    int count=0;

while(p->next!=NULL)  //判断当前位置是否已到链尾

    {

         count++;

         p=p->next;

    }

    count++;

printf("节点长度为%d\n",count);


}

//打印链表

void print(node *head)

{

    node *p;

    p=head;

while(p->next != NULL)      //判断当前位置是否已到链尾

    {

printf("节点的值为%d\n",p->data);

        p=p->next;

    }

printf("节点的值为%d\n",p->data);

}


node *del(node *head, int num)

{

    node *p, *qian;

    p=head;

while(p->data !=num && p->next !=NULL)  //确定要删除的节点不在链尾

        {

            qian=p;

            p=p->next;

        }


    if(p->data == num||p->next == NULL)

    {

if(p==head&&p->next!=NULL)     //删除的节点在链首且当前链表至少有两个节点

        {

            return p->next;

}else if(p->next==NULL&&p!=head)//删除的节点在链尾且当前链表至少有两个节点

        {

           qian->next=NULL;

            return head;

}else if(p->data == num&&p->next == NULL){  //删除的节点所在链表只有一个节点

printf("该链表已经被彻底删除,不存在");

            return  NULL;

        }else{

qian->next=p->next; //删除的节点在链表中

            return head;

        }


    }else{

printf("当前链表没有该节点");

    }

    return head;



}


int main(void)

{

    node* head={0};

head=creat();   //创建一个链表并返回一个头指针

    head->data=12;

    printf("ceshi %d\n",head->data);

    head =insert(head,17);

    head =insert(head,2);

    head =insert(head,87);

    head =insert(head,18);

    head =insert(head,19);

    head =insert(head,18);

    head =insert(head,50);

    head =insert(head,3);

    print(head);

    length(head);

    head=del(head,17);

    printf("---------------------\n");

    printf("---------------------\n");

    print(head);

    return 0;



}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 什么是数组? 数组简单来说就是将所有的数据排成一排存放在系统分配的一个内存块上,通过使用特定元素的索引作为数组的下...
    启明_b56f阅读 945评论 0 0
  • include<iostream> using namespace std; //单链表 typedef stru...
    jmychou阅读 450评论 0 0
  • 有头链表(注意:头结点有的地方是全局变量) 初学者学自于大话数据结构,不足及错误的地方请读者提出来,谢谢。 可加 ...
    lxr_阅读 825评论 0 2
  • 上次有同学讲到了链表,刚好我也学了一下,总结一下自己的学习过程,方便自己以后回顾。有疑惑的地方随时可以咨询我,有错...
    MuteZ阅读 3,114评论 0 1
  • C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构。C程...
    小辰带你看世界阅读 8,517评论 1 6