拉玛 发表于 2016-6-21 10:33:09

链表的创建、打印、插入和删除

本帖最后由 ~风介~ 于 2016-6-21 19:55 编辑

#include"stdafx.h"
#include"malloc.h"
#include "iostream"
using namespace std;

#define LEN sizeof(struct student)

struct student *creat();       //创建链表
void print(struct student *head);               //打印链表
struct student *del(struct student *head);   //删除链表
struct student *insert(struct student *head, struct student *stu_2);    //插入链表

int n;      //记录学生人数

struct student
{
        int num;
        float score;
        struct student *next;
};

void main()                      //主函数
{
        struct student *stu, *p, *stu_2;

        stu_2 = NULL;

        stu = creat();
        p = stu;
        print(p);
        print(insert(p, stu_2));
        print(del(p));
        /*
        print(creat());

        print(del(creat()));*/      //这样写之所以不对,是因为相当于由重新执行了一次creat(),所以应先把creat()赋给一个指针变量。

        cout << "\n\n";
        system("PAUSE");

}

/********************************创建链表**************************************/
struct student *creat()
{
        struct student *p1, *p2;
        struct student *head;
        p1 = p2 = (struct student *)malloc(LEN);

        cout << "the number of studnt is:";
        cin >> p1->num;
        cout << "the score is:";
        cin >> p1->score;
       
        head = NULL;
        n = 0;

        while (p1->num)
        {
                n++;
                if (1 == n)
                {
                        head = p1;      //链头
                }
                else
                {
                        p2->next = p1;
                }
                p2 = p1;

                p1 = (struct student *)malloc(LEN);//重新给p1开辟内存空间

                cout << "the number of student is:";
                cin >> p1->num;
                cout << "the score is:";
                cin >> p1->score;
        }
        p2->next = NULL;
        return head;
}

/******************************打印数据**********************************/
void print(struct student *head)
{
        struct student *p;
        p = head;
        cout << "There are " << n << " records" << endl;
        /*if (NULL != head)
        {
                do
                {
                        cout << "The score of " << p->num << " is " << p->score << endl;
                        p = p->next;                              //此处需要注意p指向下一个地址
                } while (NULL != p);
        }*/
        for(;p!=NULL;p=p->next)
                cout << "The score of " << p->num << " is " << p->score << endl;
}

/****************************************删除数据**************************************/
struct student *del(struct student *head)
{
        struct student *p1, *p2;

        int num;

        p1 = p2 = head;

        cout << "please inpute the delete num: ";
        cin >> num;
        if (head==NULL)
        {
                cout << "\nThere is no records!" << endl;
                goto end;
        }
       
        p1 = head;
        while (p1->num != 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 << "Delete No: " << num << " succeed!" << endl;
                n = n - 1;
        }
        else
        {
                cout<<num<<" not been found!\n";
        }
        end:
        return head;
}

/************************************插入数据*******************************************/

struct student *insert(struct student *head, struct student *stu_2)
{
        struct student *p1, *p2;
        stu_2 = (struct student *)malloc(LEN);

        /*为插入的数据赋值*/
        cout << "The number of insert student is: " ;
        cin >> stu_2->num ;
        cout << "The score of insert student is: ";
        cin >> stu_2->score;

        p1 = p2 = head;
        if (head == NULL)
        {
                stu_2 = head;
                stu_2->next = NULL;
        }
        else
        {
                while (p1->next != NULL&&stu_2->num > p1->num)
                {
                        p2 = p1;
                        p1 = p1->next;
                }
                if (stu_2->num <= p1->num)
                {
                        if (head == p1)
                        {
                                head = stu_2;
                                stu_2->next = p1;
                        }
                        else
                        {
                                p2->next = stu_2;
                                stu_2->next = p1;
                        }
                }
                else
                {
                        p1->next = stu_2;
                        stu_2->next = NULL;
                }
        }
        n++;
        cout << "The insert num is: " << stu_2->num << endl;
        return head;
}

~风介~ 发表于 2016-6-21 19:57:27

这是我写的,可以交流下哦!{:10_256:}
http://bbs.fishc.com/thread-46760-1-1.html

第二人生 发表于 2016-6-22 01:18:40

好好好好

第二人生 发表于 2016-6-22 01:19:11

好好好

学习电脑 发表于 2016-6-22 10:37:19

新人路过
页: [1]
查看完整版本: 链表的创建、打印、插入和删除