C++链表的创建和插入删除的实现

使用C++创建链表一直是很头疼的事,创建链表为了后面的操作方便,我们需要返回头指针,有了头指针就可以做后面的操作了,比如显示列表,增删改查等等,在create函数里首先申明两个指针,一个是用来保存上一个节点的地址P2,一个用来保存新创建节点的地址P1,当我们只创建了一个节点是,那么此时head指针和p1, p2都指向了这一个节点,也就是他们保存的地址是一样的,如果一个节点也没创建呢,就是将新创建出节点的地址P1删除掉,p2设置为空,p2的next设置为空,head设置为空,并且返回head指针,此时相当于什么也没操作;当创建两个以上的节点时,就是将上一个节点的next指向新节点,p2->next = p1; p2 = p1;这样循环往复的使用,知道出现图书编号为零的情况,那么此时将新创建的节点p1删掉,将p2->next = NULL即可,同时返回头指针,这样三种创建情况都有了。同时里面有个check字符串的函数,如果是字符串的那么直接就推出了,必须是数字才行,于是有用到了C++标准库函数atoi,atof,在code::blocks里要想用这两个函数必须#include <stdlib.h>和<stdio.h>这两个库才行。

      创建好了,就要显示我们创建的列表了,只需要王函数里传递链表的头指针就行,以此head表示当前节点,head->next表示下一个节点,以此显示他们的标号和价格就行。

      删除节点分两种情况,一种是删除头节点,那么将头指针指向下一个节点就行,如果是删除中间的节点,需要将要删除节点的前一个节点的next指向要删除节点的下一个节点就行了,然后再head = head->next这里的head表示的是当前节点,不是头节点。

     插入节点,往头部插,尾部插,中间插几种情况,头部的话就是将head指针指向新节点,新节点的next指向原来的头节点,尾部的话就是将原来的尾节点的next指向新节点,新节点的next设置为NULL即可,关键是中间节点的插入,就是将前一个节点的next指向新节点,新节点的next指向原来节点的下一个节点,具体代码如下:


#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

class Book
{
public:
    int num;
    float price;
    Book *next;
};

Book *head = NULL;

bool check(string str)
{
    for (int i = 0; i< int(str.length()); i++)
    {
        if((str[i] > '9'|| str[i] < '0')&&(str[i] != '.'))
            return false;
    }
    return true;
}

Book *create()
{
    Book *p1, *p2;
    p1 = new Book;
    head = p1;
    p2 = p1;
    cout << "input book num, end with 0" << endl;
    string str;
    cin >> str;
    while(!check(str))
    {
        cout << "input number, end with 0" << endl;
        cin >> str;
    }
    p1 -> num = atoi(str.c_str());
    if(p1 -> num != 0)
    {
        cout << "input book price" << endl;
        string str;
        cin >> str;
        while(!check(str))
        {
            cout << "input number, end with 0" << endl;
            cin >> str;
        }
        p1 -> price = atof(str.c_str());
    }
    else
    {
        delete p1; p2 = NULL; p2 -> next = NULL; head = NULL; return head;
    }
    while(p1->num != 0)
    {
        p2 = p1;
        p1 = new Book;
        cout << "input book num, end with 0" << endl;
        string str;
        cin >> str;
        while(!check(str))
        {
            cout << "input number, end with 0" << endl;
            cin >> str;
        }
        p1 -> num = atoi(str.c_str());
        if(p1 -> num != 0)
        {
            cout << "input book price" << endl;
            string str;
            cin >> str;
            while(!check(str))
            {
                cout << "input number, end with 0" << endl;
                cin >> str;
            }
            p1 -> price = atof(str.c_str());
        }
        p2 -> next = p1;

    }
        delete p1;
        p2 -> next = NULL;
        return head;
}

void showBook(Book *head)
{
    cout << endl;
    cout << "Book information:" << endl;
    while(head)
    {
        cout << "book num: "<< head -> num <<"\t";
        cout << "book price: " << head -> price << endl;
        head = head -> next;
    }
}

void deleteBook(Book *head, int num)
{
    Book *p;
    if(head -> num == num)
    {
        p = head;
        head = head -> next;
        ::head = head;
        delete p;
        cout << "delete successfully" << endl;
    }
    while(head)
    {
        if(head -> next == NULL)
        {
            cout << "not found num" << endl;
            return;
        }
        if(head -> next -> num == num)
        {
            p = head -> next;
            head -> next = p -> next;
            delete p;
            cout << "delete successfully" << endl;
            return;
        }
        head = head -> next;
    }
    cout << "not found num" << endl;
}

void insertBook(Book *head, int num, float price)
{
    Book *p = NULL;
    p = new Book;
    Book *l;
    p -> num = num;
    p -> price = price;
    while(head)
    {
        l = head;
        head = head -> next;
    }
    l -> next = p;
    p -> next = NULL;
}

void insertHeadBook(Book *head, int num, float price)
{
    Book *p = new Book;
    p -> num = num;
    p -> price = price;
    if(num < head -> num)
    {
        p -> next = head;
        ::head = p;
        return;
    }
    Book *temp = NULL;
    while((num > head -> num) && (head -> next != NULL))
    {
        temp = head;
        head = head -> next;
    }
    if(num > head -> num)
    {
        head -> next = p;
    }
    else
    {
        temp -> next = p;
        p -> next = head;
    }

}

int main()
{
    head = create();
    showBook(head);
    cout << "input delete book num" << endl;
    int num;
    cin >> num;
    deleteBook(head, num);
    cout << "after delete book list:" << endl;
    showBook(head);
    cout << "input new nook num and price" << endl;
    int bookNum;
    float bookPrice;
    cin >> bookNum;
    cin >> bookPrice;
    insertHeadBook(head, bookNum, bookPrice);
    cout << "after insert new book" << endl;
    showBook(head);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值