C++实现线性链表的基本操作

来源:互联网 发布:淘宝国庆活动策划方案 编辑:程序博客网 时间:2024/06/17 23:24

不多说,接上一篇博客内容:C++实现顺序结构线性表的基本操作

ps:编程水平有限,高手请见谅。

以下是代码:

main.cpp

/*    内容:链表的基本操作    作者:Dreamer_zz    日期:2017/1/10*/#include<iostream>#include"student.h"using namespace std;int main(){    Student *head, *t;    char num[10];    head = Creat();    cout << endl;    Travel(head);    cout << endl;    t = new Student;    cout << "请输入需要插入的同学的学号和成绩:" << endl;    cin >> t->num >> t->score;    head = InsertNode(head, t);    Travel(head);    cout << endl;    cout << "请输入需要删除的同学的学号:" << endl;    cin >> num;    DeleteNode(head, num);    Travel(head);    cout << endl;    ReleaseChain(head);    system("pause");    return 0;}

student.h

/* 创建节点 */struct Student{    char num[10];    int score;    Student *next; //存放下一个节点的地址};/* 创建一个链表 */Student *Creat();/* 遍历链表 */void Travel(Student *head);/* 在链表中插入一个节点 */Student *InsertNode(Student *head, Student *p);/* 删除链表中具有指定值的节点 */Student *DeleteNode(Student *head, char num[]);/* 释放链表 */void ReleaseChain(Student *head);

student.cpp

#include<iostream>#include"student.h"#include"string.h"using namespace std;/* 创建一个链表 */Student *Creat(){    Student *p, *q, *head; //p存放新节点地址,q存放上一个节点地址(当前链表的最后一个节点地址),head存放头节点地址    int i = 1, number; //number记录学生个数    cout << "请输入学生人数:" << endl;    cin >> number;    cout << endl;    cout << "请按学号顺序输入各同学的学号和成绩" << endl;    head = 0; q = 0;// 指针初始化    while (i <= number)    {        p = new Student; //创建一个新节点(空间大小为Student型占用的空间大小)        cout << "请输入第" << i << "个学生的学号和成绩:" << endl;        cin >> p->num;        cin >> p->score;        if (head == 0) //头节点为0,表示链表为空        {            head = p; //将p设置为头节点的地址            q = p; //创建第一个节点时,q也指向头节点        }        else //已有头节点,将新创建的节点p放在上一个节点q后面        {            q->next = p; //新创建的节点放在q后面            q = p; //将q指向新链表的最后一个节点        }        i++; //当前节点数加1    }    q->next = 0; //最后一个节点的next成员为0    return(head);}/* 遍历链表 */void Travel(Student *head){    Student *p;    int i = 1;    p = head; //从头节点开始遍历    cout << "链表中数据如下:" << endl;    while (p != 0)    {        cout << "第" << i << "个节点中的数据为:" << endl;        cout << p->num << '\t' << p->score << endl;        p = p->next; //让p指向下一个节点        i++;    }}/* 在链表中插入一个节点 ,按学号升序*/Student *InsertNode(Student * head, Student *p){    Student *be, *ne; //存放所插入节点的前节点和后节点    if (head == 0) //链表为空    {        head = p;        p->next = 0;        return head;    }    if (strcmp(head->num, p->num) >= 0) //新节点插在头节点位置    {        p->next == head;        head = p;        return head;    }    be = ne = head; //初始化插入节点的前后节点    while (ne != 0 && (strcmp(ne->num, p->num) < 0)) //当插入节点的学号大于当前节点前不是尾节点时执行循环    {        be = ne;        ne = ne->next;    }    p->next = ne;    be->next = p;    return head;}/* 删除链表中具有指定值的节点 */Student *DeleteNode(Student *head, char num[]){    Student *be, *p;    if (head == 0)    {        cout << "链表为空,无节点可删!" << endl;        return head;    }    if (strcmp(head->num, num) == 0) //头节点为删除的节点    {        p = head;        head = head->next; //改变头节点        delete p; //从内存中删除p    }    else    {        be = p = head;        while (strcmp(p->num, num) != 0 && p->next != 0) //如果不匹配,继续遍历节点        {            be = p;            p = p->next;        }        if (strcmp(p->num, num) == 0) //找到匹配节点        {            be->next = p->next;            delete p;            cout << "删除了一个节点!" << endl;        }        else        {            cout << "未找到所要删除的节点!" << endl;        }    }    return head;}/* 释放链表:将链表中所有节点从内存中删除 */void ReleaseChain(Student *head){    Student *p;    while (head) //遍历所有节点    {        p = head;        head = head->next;        delete p;    }}

运行结果如下:


运行结果

0 0
原创粉丝点击