C/C++链表基本知识及操作

有关动态内存分配的函数

1、malloc函数
函数原型为void *malloc(unsigned int size);
作用是在内存的动态存储区中分配一个长度为size的连续空间。此函数的值(即“返回值”)是一个指向分配域起始地址的针(类型为void)。如果此函数未能成功地执行(例如内存空不足),则返回空指针(NULL)。

定义动态数组
double m,n;
double **p'
p=(double *)malloc(sizeof(double *)*m);
for(i=0; i<m; i++)
	p[i]=(double *)malloc(sizeof(double)*n);

2、calloc函数
函数原型为void *calloc(unsigned n, unsigned size);
作用是在内存的动态存储区中分配n个长度为size的连续空间。
函数返回一个指向分配域起始地址的指针;如果分配不成功,返回NULL。
用calloc函数可以为一维数组开辟动态存储空间,n为数组元素个数,每个元素长度为size
3、free函数
函数原型为void free(void *p);其作用是释放由p指向的内存区,使这部分内存区能被其他变量使用。

链表的数据结构

用结构体定义链表
struct student
{
	int num;
	float score;
	struct student *next;
}

有头链表和无头链表:
有头:
在这里插入图片描述
头指针指向第一个元素的地址
无头:
在这里插入图片描述
没有头指针,直接开始存储第一个元素
动态链表的建立和使用:
头插:每次将元素插入链表的头部
尾插:每次将元素插入链表的尾部

无头操作

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node *creat(struct node *head,int n);
void print(struct node *head);
struct node
{
    int num;
    float sco;
    struct node *next;
};
int main()
{
    struct node *head;
    head=(struct node *)malloc(sizeof(struct node));
    if(!head)//容错
        exit(0);
    int n;
    scanf("%d",&n);
    head=creat(head,n);
    print(head);
    return 0;
}
struct node *creat(struct node *head,int n)//尾插法
{
    int i;//统计节点的个数
    struct node *p1,*p2;
    head=NULL;//表示无头
    p2=head;//无头就这么写
    for(i=0;i<n;i++)
    {
        p1=(struct node *)malloc(sizeof(struct node));
        if(!p1)//容错
            exit(0);
        scanf("%d%f",&(p1->num),&(p1->sco));
        if(head==NULL)//如果链表为空 则连接到第一个节点
        {
            head=p1;
        }
        else//如果链表非空 则插入尾部
        {
            p2->next=p1;
        }
        p1->next=NULL;//当前节点指向空
        p2=p1;//指向当前插入的节点
    }
    return head;
}
struct node *creat(struct node *head,int n)//头插法
{
    int i;//统计节点个数
    struct node *p;
    head=NULL;//表示无头
    for(i=0;i<n;i++)
    {
        p=(struct node *)malloc(sizeof(struct node));
        if(!p)//容错
            exit(0);
        scanf("%d%f",&(p->num),&(p->sco));
        if(head==NULL)
        {
            p->next=NULL;
        }
        else
        {
            p->next=head;
        }
        head=p;//将头结点 next指向赋给新结点
    }
    return head;
}
void print(struct node *head)
{
    struct node *p;
    p=head;
    if(head!=NULL)
    {
        do
        {
            printf("%d %.2f\n",p->num,p->sco);
            p=p->next;
        }while(p!=NULL);
    }
}

有头操作

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node *creat(struct node *head,int n);
void print(struct node *head);
struct node
{
    int num;
    float sco;
    struct node *next;
};
int main()
{
    struct node *head;
    head=(struct node *)malloc(sizeof(struct node));
    if(!head)//容错
        exit(0);
    int n;
    scanf("%d",&n);
    head=creat(head,n);
    print(head);
    return 0;
}
struct node *creat(struct node *head,int n)//头插法
{
    int i;//统计节点个数
    struct node *p;
    head->next=NULL;//表示有头
    for(i=0; i<n; i++)
    {
        p=(struct node *)malloc(sizeof(struct node));
        if(!p)//容错
            exit(0);
        scanf("%d%f",&(p->num),&(p->sco));
        p->next=head->next;//把头结点的next指向新节点
        head->next=p;
    }
    return head;
}
struct node *creat(struct node *head,int n)//尾插法
{
    struct node *p1,*p2;
    int i;
    head->next=NULL;//表示有头
    p2=head;
    for(i=0;i<n;i++)
    {
        p1=(struct node *)malloc(sizeof(struct node));
        if(!p1)
            exit(0);
        scanf("%d%f",&(p1->num),&(p1->sco));
        p1->next=p2->next;
        p2->next=p1;
        p2=p1;
    }
    return head;
}
void print(struct node *head)
{
    struct node *p;
    p=head->next;
    if(head!=NULL)
    {
        while(p)
        {
            printf("%d %.2f\n",p->num,p->sco);
            p=p->next;
        }
    }
}

链表中节点的删除
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

struct student *del(struct student *head,long num)
{
    struct student *p1,*p2;
    if (head==NULL)
    {
        printf("\nlist null!\n");
        goto end;
    }
    p1=head;
    while(num!=p1->num && p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    if(num==p1->num)
    {
        if(p1==head)
            head=p1->next;
        else
            p2->next=p1->next;
        printf("delete:%ld\n",num);
        n=n-1}
    else
        printf("%ld not been found!\n",num);
    end;
    return(head);
}

链表中插入节点
在这里插入图片描述
在这里插入图片描述

struct student *insert(struct student *head, struct student *stud)
{
    struct student *p0,*p1,*p2;
    p1=head;
    p0=stud;
    if(head==NULL)
    {
        head=p0;
        p0->next=NULL;
    }
    else
    {
        while((p0->num>p1->num) && (p1->next!=NULL))
        {
            p2=p1;p1=p1->next;
        }
        if(p0->num<=p1->num)
        {
            if(head==p1)
                head=p0;
            else
                p2->next=p0;
            p0->next=p1;
        }
        else
        {
            p1->next=p0;
            p0->next=NULL;
        }
    }
    n=n+1;
    return(head);
}

在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
struct node
{
    int data;
    struct node *next;
};
struct node *creat(struct node *head,int n);
void print(struct node *head);
struct node *insert(struct node *head);
int main()
{
    struct node *head;
    head=(struct node *)malloc(sizeof(struct node));
    if(!head)
        exit(0);
    head=creat(head,3);
    head=insert(head);
    print(head);
    return 0;
}
struct node *creat(struct node *head,int n)
{
    struct node *p1,*p2;
    int i;
    head->next=NULL;
    p2=head;
    for(i=0;i<n;i++)
    {
        p1=(struct node *)malloc(sizeof(struct node));
        if(!p1)
            exit(0);
        scanf("%d",&(p1->data));
        p1->next=p2->next;
        p2->next=p1;
        p2=p1;
    }
    return head;
}
void print(struct node *head)
{
    struct node *p;
    p=head->next;
    if(head!=NULL)
    {
        while(p)
        {
            printf("->%d",p->data);
            p=p->next;
        }
    }
}
struct node *insert(struct node *head)
{
    struct node *p1;
    p1=head;
    int n=2;
    while(n--)
        p1=p1->next;
    //printf("%d",p1->data);
    struct node *p2;
    p2=p1->next;
    struct node *p0;
    p0=(struct node *)malloc(sizeof(struct node));
    scanf("%d",&(p0->data));
    p1->next=p0;
    p0->next=p2;
    return head;
}

猜你喜欢

转载自blog.csdn.net/qq_46126537/article/details/105469855