欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

周总结

程序员文章站 2024-01-29 14:00:52
...

5)学习了C++编程,看了一些相关的习题,感觉还好,因为有一定的编程功底,自学起来不算难,今天总结了用结构体和指针建立单向链表,下面是程序:

/*由主程序调用函数,实现链表的建立、输出、删除和插入,在程序中指定需要删除和插入的结点*/
#include<iostream>
using namespace std;
struct student
{
    long num;
    float score;
    student *next;
};
int n;                                              //定义n为全局变量,本文件各个函数均用他 

int main()
{
    student *creat();
    student *del(student *,long);
    student *insert(student *,student *);
    void print(student *);
    student *head,*stu;
    long del_num;
    cout<<"input records:"<<endl;
    head=creat();                                   //返回头指针
    print(head);                                    //输出全部节点
    cout<<endl<<"input the deleted number:";         
    cin>>del_num;                                   //输入要删除的学号 
    while (del_num!=0)
    {head=del(head,del_num);                        //删除后链表的头地址 
    print(head);                                    //输出全部结点 
    cout<<endl<<"input the deleted number:";
    cin>>del_num;                                   //输入要插入的结点 
    }

    cout<<endl<<"input the inserted number:";
    stu=new student;                                //开辟一个新结点 
    cin>>stu->num>>stu->score;
    while (stu->num!=0)                             //当输入序号为0时结束输入 
    {head=insert(head,stu);                         //返回地址 
    print(head);                                    //输出全部结点 
    cout<<endl<<"input the instered number:";
    cin>>stu->num>>stu->score;
    }
    return 0;
}

student *creat(void)                                //建立链表函数 
{student *head;
student *p1,*p2;
n=0;
p1=p2=new student;                                  //开辟一个新单元并使p1,p2指向他 
cin>>p1->num>>p1->score;
head=NULL;
while (p1->num!=0)
{
    n=n+1;
    if (n==1) head=p1;
    else p2->next=p1;
    p2=p1;
    p1=new student;
    cin>>p1->num>>p1->score;
}
p2->next=NULL;
return (head);
}

student *del(student *head,long num)                //删除结点函数 
{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;
    cout<<"delete:"<<num<<endl;
    n=n-1;
}
else cout<<"cannot find"<<endl;                     //找不到结点 
return (head);
}

student *insert(student *head,student *stud)        //插入结点函数 
{student *p0,*p1,*p2;
p1=head;                                            //p1指向第一个结点 
p0=stud;                                            //指向要插入的结点 
if (head==NULL)                                     //原来的链表是空表 
{
    head=p0;p0->next=NULL;                          //p0指向的结点是头结点 
}
else
{
    while ((p0->num>p1->num)&&(p1->next!=NULL))
    {
        p2=p1;                                      //p2指向刚才的结点, 
        p1=p1->next;                                //p1后裔一个结点 
    }
    if (p0->num<=p1->num)
    {
        if (head==p1) head=p0;                      //查到原来第一个结点之前 
        else p2->next=p0;                           //插到p2指向的结点之后 
        p0->next=p1;
    }
    else
    {p1->next=p0;p0->next=NULL;}                    //插到最后的结点之后 
    n=n+1;                                          //节点数加1 
    return (head);
}
}


void print(student *head)                           //输出链表函数 
{student *p;
cout<<"Now,These"<<n<<"records are:"<<endl;
p=head;
if (head!=NULL)
do
{
    cout<<p->num<<" "<<p->score<<endl;
    p=p->next;
}while (p!=NULL);
}

By Canicula

相关标签: c语言 C++