中间问题中的单链接列表删除功能

时间:2017-03-28 18:35:24

标签: c++ linked-list

我四处寻找网络上单个链接列表的帮助,但无法找到我需要的信息。我想要做的是使用以下操作设计和实现单个链表

a-创建" n"节点

b-从中间删除(我不删除中间,但除了开始和结束节点之外的任何其他内容)

c-插入中间

我做了我的"插入"部分具有ADD功能 但是当我进入删除功能时,我陷入了如何使用单个链接列表&不是双倍,因为我试图打破这种习惯。无论如何我的代码,我的删除功能,我试图解决。谢谢

PS:告诉我我的添加功能是否正确,:))

#include <iostream>
#include <vector>
using namespace std;

class node
{
public:
    node(int user_input);
    int info;
    node * next_node;

};


node::node(int user_input)
{
    info = user_input;
    next_node = NULL;

}


class link_list
{
public:
    node * start;
    node * end;
    int size;//seperated the attritibute from the method

    link_list(); //default constuctor
    link_list(int value); //value constuctor
    link_list(int value, int num_of_Nodes); //n-ary

    void Add(int store_node);
    void Print(); //non midify
    void insert_at_begining(int value); //while these guys are modify
    void insert_at_end(int value); //
    void insert_at_middle(int delete_node);

    void delete_at_begining();
    void delete_at_end(); //
    void delete_at_middle(int Nodes_store);

private:
    int Number_of_nodeV2;

};

link_list::link_list(int value, int num_of_Nodes) //this 
{
    //if val = 0 & num = 5 we get 5 new nodes

    Number_of_nodeV2 = 0;

    if (num_of_Nodes < 1)
    {
        cout << "error, please enter correct numbers" << endl;
    }
    else
    {
        start = new node(value); //this is pointing to node that has value in it.
        //start == NULL;

        node* tracker = start;

        for (int i = 0; i < num_of_Nodes - 1; i++)
        {
            tracker->next_node = new node(value); //this track each node //-> de-renecen 
            tracker = tracker->next_node;
        }
    }


}

void link_list::Add(int store_node)
{

    node* tracker = start;
    node* newVal = new node(store_node);



    while (tracker->next_node != NULL)
    {
        tracker = tracker->next_node;
    }
    tracker->next_node = newVal;
}

void link_list::Print()
{
    node* tracker = start;

    while (tracker != NULL)
    {
        cout << tracker->info << endl;
        tracker = tracker->next_node;
        cout << size;
    }      
}

void link_list::delete_at_middle(int delete_node)
{
        //int center = 0;
        node* tracker = start;
        node* tracker2 = end;
        node* temp;
        node* temp1;

        temp1 = temp = start;

        if (start == NULL)
        {
            cout << "the list is empy" << endl;
        }
        else //this is where I cant seem to answer, thanks
        {
            while (temp != end)
            {
                temp = temp1->next_node;
                if (temp->info = delete_node)
                {
                    delete temp;
                    temp1->next_node = NULL; //ffffffffffff
                }

            }
                tracker = tracker->next_node;

            for (size_t i = 0; i < length; i++)
            {
                pre = start;
                pre

            }
        //iltertae with tracker until the tracker has hit the center


        }



}


int main() {
    int Nodes_store = 0;
    int delete_node = 0;

    cout << "please enter the number of nodes" << endl;
    cin >> Nodes_store;

    cout << "please enter the number of to delete nodes" << endl;
    cin >> delete_node;


    link_list list = link_list(0, Nodes_store);
    list.Print();

    //list.delete_at_middle();
    //list.Print();
    //
    //list.insert_at_middle(7);
    //list.Print();
}AS'
;'

0 个答案:

没有答案