Build a two-way linked list c ++ - Programming - HWzone Forums
adplus-dvertising
Skip to content
  • Create an account
  • About Us

    Hello Guest!

     
    Please note - in order to participate in our community, comment and open new discussions, you must join as a registered member.

    Our members enjoy many advantages, including the ability to participate in discussions, enjoy raffles and promotions for members of the site, and receive our weekly content directly by email.

    Do not like being harassed by email? You can register for the site but do not submit your registration to the weekly email updates.

Build a two-way linked list c ++


BuziCon
 Share

Recommended Posts

Hello, I'm building a two-way linked list in c ++, 

By creating an object called a List that is supposed to point to the beginning and end of the list,

And creating a node that should point to the one before it and the one after it.

The problem, in the function of Insert (insert to the beginning of the list), I did (want to do anyway ..) that he get the list, and build a new node, in the first case if the list points to null, then he points to it,

And if not pointing to null, then the new member will point to what the list pointed to, and the list will point to it. Very simple.

The problem is that every time he enters a function he supposedly picks up a new list that points to null, and then there is no option to insert new members. I mean, he's making me a copy, not the original list.

I tried to send the address but that means I am sending a pointer, and then the voters who have access to the list do not have access to them or Moshe.

Here's the code:

#include 
#include
#include
using namespace std;

struct Node {
    int data;
    Node * next;
    Node * prev;
};
struct List {
    Node * head;
    Node * tail;
};

void Insert (List h, int new_data) {

    struct Node newNode;

    newNode.data = new_data;


    if (h.head == NULL) {
        newNode.next = NULL;
        newNode.prev = NULL;
        
        h.head = & newNode;
        h.tail = & newNode;
    }
    else {
        h.head-> prev = & newNode;
        newNode.next = h.head;
        newNode.prev = NULL;
        h.head = & newNode;
    }
}

void displayList (struct List & h)
{
    // cout << h.head-> data;

}

void main ()
{
    struct List list;
    list.head = NULL;
    list.tail = NULL;
    Insert (list, 23);
    Insert (list, 234);

    displayList (list);
}
In this code he creates a new list every time.

And if I change the insert function to

void Insert (List * h, int new_data) {

    struct Node newNode;

    newNode.data = new_data;


    if (h.head == NULL) {
        newNode.next = NULL;
        newNode.prev = NULL;
        
        h.head = & newNode;
        h.tail = & newNode;
    }
    else {
        h.head-> prev = & newNode;
        newNode.next = h.head;
        newNode.prev = NULL;
        h.head = & newNode;
    }
}

This creates a problem in all h.head and h.tail.

help?

 

Link to content
Share on other sites

#include  
#include 
#include
using namespace std;



struct Node {
	int date;
	Node* Next;
	Node* prev;
};
struct List {
	Node* head;
	Node* tail;
};

void Insertion(List* h, int new_data) {

	struct Node newNode;

	newNode.date = new_data;


	if (h.head == NULL) {
		newNode.Next = NULL;
		newNode.prev = NULL;
		
		h.head = &newNode;
		h.tail = &newNode;
	}
	else {
		h.head->prev = &newNode;
		newNode.Next = h.head;
		newNode.prev = NULL;
		h.head = &newNode;
	}
}

void displayList(struct List& h)
{

}

void main()
{
	struct List list;
	list.head = NULL;
	list.tail = NULL;
	Insertion(list, 23);
	Insertion(list, 234);

	displayList(list);
}

This is the written code

Link to content
Share on other sites

There are some issues. One is that in the Insert function you assign a local object to the stack and then save its address in an external list. This is not good, because the local object "dies" at the end of the function and its memory is released, meaning the pointer points to the trash. You need to assign by new (C ++ approach) or malloc (C approach).

 

Why you should do include to ?

 

Note that you set the Insert function to get * List but you pass it to Struct List. Are you sure it crumbled at all?

Link to content
Share on other sites

Without the * list yes, it just remains from one of the experiments I did ..

The problem you raised in the Insert function is my real problem, every time it points to null that a new temporary object is created.

How can one get the real object, and not a copy of it?

Link to content
Share on other sites

#include 
#include
using namespace std;



struct Node {
	int date;
	Node* Next;
	Node* prev;
};
struct List {
	Node* head;
	Node* tail;
};

void Insertion(List h, int new_data) {

	struct Node newNode;

	newNode.date = new_data;


	if (h.head == NULL) {
		newNode.Next = NULL;
		newNode.prev = NULL;
		
		h.head = &newNode;
        // its will be point to the last node in list
		h.tail = &newNode;
	}
	else {
    // "the first node will point (prev) to new node"
		h.head->prev = &newNode;
        // the new node will point (next) to the first node
		newNode.Next = h.head;
		newNode.prev = NULL;
        // the head will point to the new node, and its will be first now
		h.head = &newNode;
	}
}

void displayList(struct List h)
{
	// cout << h.head-> data;

}

void main()
{
	struct List list;
	list.head = NULL;
	list.tail = NULL;
	Insertion(list, 23);
	Insertion(list, 234);

	displayList(list);
}

Ok..

So this is the code, and it folds.

The problem is like you said, that every insert makes me a copy.

 

Edited By BuziCon
Link to content
Share on other sites

For this reason you need the Insert function to get * List and move the & list to it.

 

This will not solve the local assignment problem within Insert that I commented on in the previous answer.

 

It is obvious that you still have a certain misunderstanding about the difference between an object and a pointer to the same object, and the difference between the point operator and the arrow operator.

Link to content
Share on other sites

There is everything on the Internet: point guides for every subject, complete textbooks, recorded lectures. I just have no idea what your background is, how and what you learned, what you are learning and why. So I can not really give better advice than "type a few words in a search engine and start working on the sites you find".

Link to content
Share on other sites

Join the discussion

You can then join the discussion and then join our community. If you already have an account with us, please Log in now To comment under your username.
Note that: The comment will appear to the surfers after approval by the board management team.

guest
Add a comment

×   The content pasted is with formatting.   Remove formatting

  Only 75 emoji are allowed.

×   Your link has been automatically assimilated.   Show as regular link

×   Your previous content has been automatically restored.   Clear all

×   You can not paste images directly. Upload or insert images from URL.

 Share

×
  • Create new ...

At the top of the news:

new on the site