C++ 链表操作

#include <iostream.h>

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

int n;

struct student *CreateLink()
{
    struct student *head;
    struct student *p1=NULL;
    struct student *p2=NULL;
    int num = 1;
    int score = 11;
    n = 0;
    p1 = new struct student();
    p2 = p1;
    if (p1 == NULL)
    {
        cout<<"/nCann't create it, try it again in a moment!/n";
        return NULL;
    }
    else
    {
        head = NULL;
        p1->num = num++;
        p1->score = score++;
    }
   
    while(p1->num < 10)
    {
        n += 1;
        if (n==1)
        {
            head = p1;
            p2->next = NULL;
        }
        else
        {
            p2->next = p1;
        }
        p2 = p1;
        p1 = new struct student();
        p1->num = num++;
        p1->score = score++;
    }
    p2->next = NULL;
    delete(p1);
    p1 = NULL;
    return head;
}

void Print(struct student *head)
{
    struct student *p;
    p = head;
    if(head != NULL)
    {
       
        do
        {
            cout<

<<'/T'< num<<'/t'< score<<'/t'< next<
            p = p->next;
        }
        while (p != NULL);
    }
}

struct student *DeleteLink(struct student *head, int num)
{
    struct student *p1;
    struct student *p2;
    if (head == NULL)
    {
        cout<<"/nList is null!/n";
        return head;
    }
    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;
        }

        delete p1;
        p1 = NULL;
        n -= 1;
    }
    else
    {
        cout<<"/nnot been found!/n";
    }

    return head;
}

struct student *InsertLink(struct student *head, int num, struct student *node)
{
    struct student *p1;
   
    if (head == NULL)
    {
        head = node;
        node->next = NULL;
        n += 1;
        return head;
    }

    p1 = head;
    while (p1->num != num && p1->next != NULL)
    { 
        p1 = p1->next;
    }
   
    if (num == p1->num)
    {
        node->next = p1->next;
        p1->next = node;
        n += 1;
    }
    else
    {
        cout<<"/nnot been found!/n";
    }

    return head;
}

struct student *ReverseLink(struct student *head)
{
    struct student *p;
    struct student *p1;
    struct student *p2;

    p1 = NULL;
    p2 = head;
    while (p2 != NULL)
    {
        p = p2->next;
        p2->next = p1;
        p1 = p2;
        p2 = p;
    }
    head = p1;
   
    return head;
}

void main()
{
    struct student *head;
    struct student *stu;
    int thenumber;

    head = CreateLink();
    Print(head);

    cout<<"/nWhich one delete: ";
    cin>>thenumber;
    head = DeleteLink(head,thenumber);
    Print(head);
   
    stu = new struct student();
    cout<<"/nPlease input insert node -- num,score: ";
    cin>>stu->num>>stu->score;
    cout<<"/nInsert behind num: ";
    cin>>thenumber;
    head = InsertLink(head,thenumber,stu);
    Print(head);
   
    head = ReverseLink(head);
    Print(head);
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值