C++:操作单向链表的实现

程序功能:

1、能够创建链表

2、遍历链表

3、查找链表的结点

4、插入结点到链表

5、删除链表中的结点


主程序:

#include <iostream>

using namespace std;

struct node // 定义结点类型
{
    char data; // 用于存放字符数据
    node *next; // 用于指向下一个结点(后续结点)
};

node * create(); // 创建链表的函数,返回表头
void showList(node *head); // 遍历链表的函数,参数为表头
node * search(node *head, char keyWord);
void insert(node *&head, char keyWord, char newdata);
void Delete(node *&head, char keyWord);

int main()
{
    node *head;
    head=create(); // 以head为表头创建一个链表
    showList(head); // 遍历以head为表头的链表
    node * sea = new node;
    sea = search(head,'a');
    cout << sea->data;
    insert(head, 'b', 's');
    showList(head);
    Delete(head, 'k');
    showList(head);
    return 0;
}

创建链表

// 创建链表
node * create()
{
    node *head=NULL; // 表头指针,一开始没有任何结点,所有为NULL
    node *pEnd=head; // 表尾指针,一开始没有任何结点,所以指向表头
    node *pS; // 创建新结点时使用的指针
    char temp; // 用于存放从键盘输入的字符
    cout << "Please input a string end with '#':" << endl;
    do // 循环至少运行异常
    {
        cin >> temp;
        if(temp!='#') // 如果输入的字符不是结尾符#,则建立新结点
        {
            pS = new node; // 创建新结点
            pS->data = temp; // 新结点的数据为temp
            pS->next = NULL; // 新结点将成为表尾,所以next为NULL
            if(head==NULL) // 如果链表还没有任何结点存在
            {
                head = pS; // 则表头指针指向这个新结点
            }else{
                pEnd->next = pS; // 把这个新结点连接在表尾
            }
            pEnd = pS; // 这个新结点成为了新的表尾
        }
    }while(temp!='#');{ // 一旦输入了结尾符,则跳出循环
        return head; // 返回表头指针
    }

}

遍历链表

// 遍历链表
void showList(node *head)
{
    node *pRead = head;
    cout << "The data of the link list are:" << endl;
    while(pRead!=NULL) //当访问指针存在时(即没达到表尾之后)
    {
        cout << pRead->data; // 输出当前访问结点的数据
        pRead=pRead->next; // 访问指针向后移动
    }
    cout << endl;
}

查找链表中的结点

// 查询链表
node *search(node *head, char keyWord)
{
    node *pRead = head;
    while(pRead!=NULL)
    {
        if(pRead->data==keyWord) //如果当前结点的数据和查找的数据相符
        {
            return pRead; // 则返回当前结点的指针
        }
        pRead = pRead->next; // 数据不匹配,pRead指针向后移动,准备查找下一个结点
    }
    return NULL;
}

插入链表中的结点

void insert(node *&head, char keyWord, char newdata) // 可能要操作表头指针,所以head是引用
{
    node * newnode = new node; // 新建结点
    newnode->data = newdata; // newdata是新结点的数据
    node *pGuard = search(head, keyWord); // pGuard是插入位置前的结点指针
    if(head==NULL || pGuard==NULL) // 如果链表没有结点或找不到关键字结点,则插入表头位置
    {
        newnode->next = head; // 先连
        head = newnode; // 后断
    }else{ // 插入在pGroud之后
        newnode->next = pGuard->next; // 先连
        pGuard->next = newnode; //后断
    }
}

删除链表中的结点

// 删除结点
void Delete(node *&head, char keyWord) // 可能要操作表头指针,所以head是引用
{
    if(head!=NULL) // 如果链表没有结点,就直接输出提示
    {
        node *p;
        node *pGuard = head; // 初始化pGuard指针
        if(head->data==keyWord) // 如果头结点数据符合关键字
        {
            p = head; // 头结点是待删除结点
            head = head->next; // 先连
            delete p; // 后断
            cout << "The deleted node is" << keyWord << endl;
            return;
        }else{
            while(pGuard->next!=NULL){ // 当pGuard没有达到表尾
                if(pGuard->next->data==keyWord) // 如果pGuard后续结点数据符合关键字
                {
                    p=pGuard->next; // pGuard后续结点是待删除结点
                    pGuard->next=p->next; // 先连
                    delete p; // 后断
                    cout <<"The delete node is" << keyWord << endl;
                    return; // 结束函数运行
                }
                pGuard = pGuard->next; // pGuard指针向后运动
            }
        }
    }
    cout << "The keyword node is not found or the link list is empty!" << endl; // 输出提示信息
}


 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值