鱼C论坛

 找回密码
 立即注册
查看: 1700|回复: 1

有没有哥哥姐姐帮帮我

[复制链接]
发表于 2021-3-21 16:29:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

请使用单链表存储整型数据,编写如下函数:

(1)创建链表;

(2)输出查看链表所有结点;

(3)根据指定的位置插入一个新结点,若插入位置超出链表范围,提示插入失败;

(4)删除所有值为x的结点;

(5)查找值为x的结点,若查找成功则输出结点,若查找失败,则提示没有该结点;

(6)销毁整个链表;
#include <iostream>
using namespace std;
struct student
{
        int num;                       
        float score;                  
        struct student *next;  //next指针变量,存放下一结点的地址
};

//尾插法
struct student *insert_t(struct student *head) //定义插入结点函数
{
        struct student *p1,*p2;
        cout<<"输入需要插入的新结点学号、成绩"<<endl;
        p1=new struct student;  //动态申请空间,要插入的结点
        cin>>p1->num>>p1->score;
        if(head!=NULL) // 如果链表不为空
        {
                p2=head;//使p2指向第一个结点
                while(p2->next!=NULL) //p2指向表尾
                        p2=p2->next;
                p2->next=p1;
        }
        else
        {
                head=p1;
        }
        p1->next=NULL;   
        return(head);
}

//插入到表头
struct student *insert_h(struct student *head)
{
        student *p1,*p2;
        cout<<"输入需要插入的新结点学号、成绩"<<endl;
        p1=new struct student;  //重新开拓存放空间给插入结点
        cin>>p1->num>>p1->score;
        p1->next=head;
        head=p1;     
        return(head);
}

struct student *del(struct student *head,int num)             //定义删除结点函数
{
        struct student *p1,*p2;
        if (head==NULL)                                           // 是空表
        {
                cout<<"list null!"<<endl;
                return(head);
        }
        p1=head;                                                 //使p1指向第一个结点首部
        while(num!=p1->num && p1->next!=NULL)                    //p1指向的不是所要找的结点且后面还有结点
        {
                p2=p1;
                p1=p1->next;                                         // p1后移一个结点
        }                                                      
        if(num==p1->num)                                         // 如果找到删结点
        {
                if(p1==head)
                        head=p1->next;                                   //若p1指向的是首结点,把第二个结点地址赋予head
                else
                        p2->next=p1->next;                               // 否则将下一结点地址赋给前一结点地址
                delete p1;
                p1=NULL;
                cout<<endl;
                cout<<"删除结点"<<num<<"后"<<endl;
        }
        else
                cout<<num<<"not been found!"<<endl;                  //找不到该结点
        return(head);
}

void print(struct student *head)                        //定义输出链表函数
{
        struct student *p;
        p=head;
        if(head!=NULL)
        {        cout<<"数据输出如下:"<<endl;
            while(p!=NULL)
                {
                        cout<<p->num<<" "<<p->score<<endl;
                        p=p->next;
                }
        }       
}

int main()
{
        struct student *head=NULL;  
        int n=4;
        while(n--)
        {
                head=insert_h(head);//往链表里插入一个结点
        }
        print(head);  
    head=del(head,103);
        print(head);
        return 0;
}
有几个不知道怎么下手了。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-3-21 17:50:54 | 显示全部楼层
你这链表和节点都没创建,怎么搞下去..
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 03:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表