对c/c++链表的简单使用与解析

链表的优点:
对表的操作都可以使用数组来实现,因为数组是动态指定的,但是还是需要对表的大小的最大值进行估计,通常要估计的大一些,这会浪费大量的空间,这是严重的局限,这可能会导致程序出错.由于存在许多未知的大小的表的情况下.通过使用链表可以有效地避免这样的问题.同时使用链表可以较方便和快捷的对表进行插入和删除,减少了线性开销.下面让我们了解和学会使用对链表的简单操作:

   链表的基本使用(使用c/c++做例子): 

通过这个题来对链表进行理解:

编写一个程序,能够输入学生的学号和分数,并将其输出,同时能够删除学生信息和添加学生信息(要求使用链表完成,能够输出,删除和添加链表元素)

      创建链表:

      这是使用链表的第一步,废话不多说,上代码

#include <iostream>
#define NULL 0
using  std::cin;
using  std::cout;
using  std::endl;
//创建数据结构
struct student
{
    long num;   //学号
    float score;  //分数
    student *next;  //链表的指针
};
int count;  //全局变量,计数
//创建链表
student *creat()
{
    student *head;  //head 链表头
    student *p1,*p2;
    count=0;
    p1 = p2 = new student;  //开辟新单元,使p1,p2指向它


    cin >> p1->num >> p2->score;
    head = NULL;  //清空head


    while(p1->num != 0)
    {
        count+=1;
        //如果head==1,那么p1就赋值给head
        if(count==1)
        {
            head = p1;
        }
        //否则就将p1链接给p2的结点
        else
        {


            p2->next = p1;
        }
             p2 = p1;
        p1 = new student;  //将p1开辟新单元
        cin >> p1->num >> p1->score;
    }
        //清空p2的结点
    p2->next = NULL;
    return head;
}



输出链表:

链表的输出p先指向第一个结点,输完第一个之后,盘指向第二个结点.以此类推.

head的值由实参传过来,在函数中从head所指向的第一个结点出发顺序输出各个结点.

//输出创建的链表
void print(student *head)
{
    student *p;
    cout << endl << "输出链表节点:" << endl;
    p = head;
    //如果head不是空的,那么输出p
    if(head != NULL)
     do
    {
       cout << p->num << " " << p->score << endl;
    p = p->next;
    }while(p != NULL);


}

删除链表元素:

从动态链表中删除一个结点,并不是真正将其从内存中抹除,是将这个结点从链表中分离.这里我们制定学号作为删除结点的标志.

//操作链表进行删除
student *del(student *head,long num)
{
    student *p1,*p2;
    if(head == NULL)
    {
        cout << "八嘎呀路,空链表还操作个蛇!" << endl;
        return head;
    }


    p1 = head;
        //当num不是p1指向的num,p1的结点不是空的
    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;
            cout << "删除:" << num <<endl;
            count-=1;
        }
    }


    else
    {
        cout << "找不到该结点:" << num;
    }
    return head;
}

添加链表元素:

对链表的插入是将一个结点插入到一个已有的链表中.

//插入链表元素
student *insert(student *head,student *stud)
{
    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;
        }
        count+=1;
        return head;
        }


    }
下面写出这个题目的完整程序:
#include <iostream>
#define NULL 0
using  std::cin;
using  std::cout;
using  std::endl;
//创建数据结构
struct student
{
    long num;   //学号
    float score;  //分数
    student *next;  //链表的指针
};




//链表的创建,删除,插入,输出
student *creat();
void print(student *head);
student *del(student *head,long num);
student *insert(student *head,student *stud);


int count;  //全局变量,计数


int main()
{
    student *head,stu;
    long del_num;
    cout << "输入记录(学号和得分):" << endl;
    head = creat();
    print(head);
    cout << endl << "输入删除的数字:";
    cin >> del_num;
    head = del(head,del_num);
    print(head);
    cout << endl <<"输入要插入的结点:";
    cin >> stu.num >> stu.score;
    head = insert(head,&stu);
    print(head);
    return 0;
}


//创建链表
student *creat()
{
    student *head;  //head 链表头
    student *p1,*p2;
    count=0;
    p1 = p2 = new student;  //开辟新单元,使p1,p2指向它


    cin >> p1->num >> p2->score;
    head = NULL;  //清空head


    while(p1->num != 0)
    {
        count+=1;
        //如果head==1,那么p1就赋值给head
        if(count==1)
        {
            head = p1;
        }
        //否则就将p1链接给p2的结点
        else
        {


            p2->next = p1;
        }
             p2 = p1;
        p1 = new student;  //将p1开辟新单元
        cin >> p1->num >> p1->score;
    }
        //清空p2的结点
    p2->next = NULL;
    return head;
}


//输出创建的链表
void print(student *head)
{
    student *p;
    cout << endl << "输出链表节点:" << endl;
    p = head;
    //如果head不是空的,那么输出p
    if(head != NULL)
     do
    {
       cout << p->num << " " << p->score << endl;
    p = p->next;
    }while(p != NULL);


}


//操作链表进行删除
student *del(student *head,long num)
{
    student *p1,*p2;
    if(head == NULL)
    {
        cout << "八嘎呀路,空链表还操作个蛇!" << endl;
        return head;
    }


    p1 = head;
        //当num不是p1指向的num,p1的结点不是空的
    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;
            cout << "删除:" << num <<endl;
            count-=1;
        }
    }


    else
    {
        cout << "找不到该结点:" << num;
    }
    return head;
}
//插入链表元素
student *insert(student *head,student *stud)
{
    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;
        }
        count+=1;
        return head;
        }


    }





水平有限,欢迎在大家在评论区交流

 

猜你喜欢

转载自blog.csdn.net/qq_40570410/article/details/80380699