How do I add an element to the front of a linked list? - c++

this is how the node is set up:
struct Node {
Node *next;
Node *prev;
T datum;
};
this is my code
//MODIFIES: this
//EFFECTS: inserts i into the front of the list
void push_front(const T &datum)
{
Node newfirst = first; // set newnode to first
&first = &datum;
datum = newfirst;
}
Node *first; // points to first Node in list, or 0 if list is empty
Node *last; // points to last Node in list, or 0 if list is empty
for some reason, I don't think this is right.

It seems you need the following
//this is my code
//MODIFIES: this
//EFFECTS: inserts i into the front of the list
void push_front(const T &datum)
{
first = new Node { first, nullptr, datum };
if ( !last ) last = first;
}
If your compiler does not support initializer lists for the operator new then you can write
//this is my code
//MODIFIES: this
//EFFECTS: inserts i into the front of the list
void push_front(const T &datum)
{
Node *tmp = new Node();
tmp->datum = datum;
tmp->next = first;
first = tmp;
if ( !last ) last = first;
}

You want to (i) create a new node with a valid content, and (ii) set is as the first node of your list. You can do that like in the following example:
void push_front(const T &datum)
{
Node* newFirst = new Node; //construct new Node
newFirst->next = first; // set newFirst's next node
newFirst->datum = datum; //set the content
first = newFirst; //assign new first node;
}
This is just as a sketch; for more details, you should post more code (such as mentioned in one of the comments).
Another thing to mention: I would prefer using a unique_ptr for one of those Node pointers, e.g.
struct Node {
std::unique_ptr<Node> next;
Node *prev;
T datum;
};
This you can very easily destroy the list (and also avoid the new command which is often recommended in modern C++).

Related

Why my head pointer is changing in linked list,even not passing it by reference?

I created a linked list, and made a function reverseList which takes a pointer to head and return pointer to last node.
Node* reverseList(Node *head)
{
Node* curr=head;
Node* prev=NULL;
Node* ahead;
while(curr!=NULL)
{
ahead=curr->next;
curr->next=prev;
prev=curr;
curr=ahead;
}
return prev;
}
But in main when I am doing this
int main()
{
int n;///no of elements in list
cin>>n;
Node* head=NULL;
head=createList(head,n);///creating list(it is working properly)
printList(head);
cout<<endl;
Node* temp=reverseList(head);///reversing list and storing address of list in
//new node
printList(temp);///printing reversed list properly
cout<<endl;
printList(head);///while printing this it is printing only one elements,
//which implies head pointer changes but I don't know
///how
}
My head pointer changes, and it is printing only one value. I had pass my head pointer in reverseList by value. I am providing image of output.
Comments explain fine already, trying to illustrate to make it a little clearer:
1 > 2 > 3 > 4 > NULL
^
head
Now you reverse the list, resulting in:
4 > 3 > 2 > 1 > NULL
^ ^
temp head
As you never changed head, it still points to the same node as it pointed to before the list reversal, but after reversing the list, this node is now the last one.
Side note: Forgetting to re-assign is quite a common error, so it is a good idea to encapsulate the linked list in a separate class:
class LinkedList
{
Node* _head;
public:
class Node; // just as you have already
void reverse() // now a member function
{
//reverse as you did before
// encapsulating the assignment: (!)
_head = newHead;
}
Node* head() { return _head; }
};
LinkedList l;
// ...
Node* tmp = l.head();
l.reverse();
// tmp variable points to tail...
// expecting tmp pointing to head is still an error,
// and there is no way to prevent it
// BUT the correct head can always be re-acquired:
head = l.head();
Edit in response to comment:
If you want to create a new list, you will have to copy the nodes:
Node* createReversedList(Node* head)
{
Node* cur = NULL;
while(head)
{
Node* tmp = new Node(*head);
// (provided you have an appropriate copy constructor)
tmp->next = cur;
cur = tmp;
head = head->next;
}
return cur;
}
Note the new name, reverse rather implies modifying the original list as you did.
To create a new Linked List, you need to create a new variable of Node, and perform operations on that variable.
So, the code would be something like:
Node* reverseList(Node *head)
{
Node* newRootPtr = new Node(); //Pointer to the new root. This will be returned to the calling function.
newRootPtr->next = NULL; //In the reversed list, the original head will be the last node.
Node* curr=head; //For iterations
while(curr->next!=NULL) //For every node, until the last node. Note that here, we need to stop at the last node, which will become the first node of the new List.
{
Node ahead=*(curr->next); //Create a new Node equal to the next node of the original list.
Node* aheadPtr = &ahead; //Pointer to the new node
aheadPtr->next = newRootPtr; //Point the new node to the previous node of the new list
newRootPtr = aheadPtr; //update root node
curr=curr->next;
}
return newRootPtr;
}

LinkedList Node Insertion

I know this is seriously basic stuff, but I for the life of me can't seem to understand how I would go about doing this:
Q: Given the following struct definition for a Node:
struct Node
{
int value;
Node* next;
};
And given the declaration:
Node* head;
The following Linked List of Nodes has been constructed, with the head pointer assigned to the Node object 3:
head ----> 3 ----> 9 ----> 34 ----> -5 ----> NULL
Write a single C++ statement that will store the value 34 in the 3rd Node in the variable result:
int result =
Where would be a good place to start? Is this asking me to add an element to the list, or is it adding an entirely new node to the list? So confused!!
head->next->next will point to the third node in the linked list.
So,
int result = head->next->next->data;
will store the value in the third node in the list.
The question is asking you to read the third node and store it's value in result.
Linked lists employ a chain concept, where each chain-link along the entire length of the chain is connected at the front of each link.
People represent this in various ways. One example is:
struct Node {
int value;
Node* front;
};
Or:
struct Node {
int value;
Node* next;
};
When you establish the head of a linked list, you are creating the first node in the chain:
Node* head;
head = NULL; // this is an empty linked list
Whenever you want to add a node (or "link") to the chain, you pass along the new node to "Head", which keeps track of the chain:
void newNode(int value, Node* head)
{
Node *node;
node = new Node*; // or (Node*)malloc(sizeof(Node));
node->value = value;
node->next = head;
head = node;
}
Using this concept, you can add any number of links to your chain. Thomas and warun have good examples of this.
Hope this helps.
example for a loop.
Node* temp = head;
int number = 300; //the place you are looking for
if(temp)
{
int i;
for(i = 0 ; i< number && temp->next != nullptr ;i++)
{
temp = temp->next;
}
if(i == 300)
{
//found you value;
result = temp->value;
}
}

how to add to the first of a 2-way linked list?

I am trying to make a two way linked-list in c++ with class but I have a annoying problem to add node to the first of the list!
Here is Node class:
class node{
public:
node(int d = 0, node *p = NULL, node *n = NULL):data(d), pre(p), next(n){}
void setPrevious(node *p){this->pre = p;}
void setNext(node *n){this->next = n;}
void setData(int d){this->data = d;}
int data;
node *pre, *next;
};
Here is how I create the first Node:
node *head = new node(), *current = new node(), *last = new node();
cout<<msg_data;
// 'd' is an Integer variable
cin>>d;
current->setData(d);
head = new node(0, 0, current);
And this is how I try to add node to the first of the list:
cout<<"enter 'data' for list: ";
// 'd' is an Integer variable
cin>>d;
node *tmp = new node(d, 0, head);
head = tmp;
When I want to add node to first it add a '0' for 'data' after a value! ex. I want to add '21' to the first of the list but it adds 21 and 0 to the list!
Two observations:
Looking at your code that creates the first node it creates not one node but FOUR nodes. It seems you completely misunderstood something. Draw on paper how the list should look, and you will figure it out.
Your code that adds an extra node to the first of the list seems OK.

c++ remove similar nodes linked list

For a homework assignment I need to remove all similar nodes that the number passed into. For example if I have on the list
3
5
5
4
the 5's will be removed from the linked list and I will end with
3
4
we are not allowed to use the std library for this class and here is the header file
namespace list_1
{
class list
{
public:
// CONSTRUCTOR
list( );
// postcondition: all nodes in the list are destroyed.
~list();
// MODIFICATION MEMBER FUNCTIONS
//postcondition: entry is added to the front of the list
void insert_front(const int& entry);
//postcondition: entry is added to the back of the list
void add_back(const int& entry);
// postcondition: all nodes with data == entry are removed from the list
void remove_all(const int& entry);
// postcondition: an iterator is created pointing to the head of the list
Iterator begin(void);
// CONSTANT MEMBER FUNCTIONS
// postcondition: the size of the list is returned
int size( ) const;
private:
Node* head;
};
}
I can understand how to remove the front, and the back of the list. But for some reason I can't wrap my head around going through the list and removing all of the number that is passed in. Anything helps! Thanks
edited to include Node.h
#pragma once
namespace list_1
{
struct Node
{
int data;
Node *next;
// Constructor
// Postcondition:
Node (int d);
};
}
There are two ways of doing this. The first is to iterate through the list and remove the nodes. This is tricky because to do that you have to keep a pointer to the previous node so you can change its next value.
The code for removing a node would look like this (assume current is the current node and prev is the previous node)
Node* next = current->next;
delete current;
prev->next = next;
Maintaining a reference to the previous node can be a bit tedious though, so here is another way to do it. In this method, you essentially create a new list but don't insert Nodes who's data is equal to entry.
The code might look a little like this
void list::remove_all(const int &entry)
{
Node* newHead = NULL;
Node* newTail = NULL;
Node* current = head;
// I'm assuming you end your list with NULL
while(current != NULL)
{
// save the next node in case we have to change current->next
Node* next = current->next;
if (current->data == entry)
{
delete current;
}
else
{
// if there is no head, the set this node as the head
if (newHead == NULL)
{
newHead = current;
newTail = current;
newTail->next = NULL; // this is why we saved next
}
else
{
// append current and update the tail
newTail->next = current;
newTail = current;
newTail->next = NULL; // also why we saved next
}
}
current = next; // move to the next node
}
head = newHead; // set head to the new head
}
Note: I didn't test this, I just typed it up off the top of my head. Make sure it works. =)
Hope this helps! ;)

C++ Linked list implementation crashing

I am trying to implement a linked list for a data structures class and I am having some difficulty with the searching portion of the algorithm.
Below is the offending code, which I have tried to implement following the pseudo-code in the MIT introduction to algorithms text:
//
// Method searches and retrieves a specified node from the list
//
Node* List::getNode(unsigned position)
{
Node* current = m_listHead;
for(unsigned i = m_listSize-1; (current != 0) && (i != position); --i)
current = current->next;
return current;
}
The head at this point in the program is the 4th node, which contains the value of int 5. the problem appears to be in the body of the for-loop, where the pointer to the node object is assigned to the next node. But this is going beyond the head of the node, so it is essentially pointing at some random location in memory (this makes sense).
Shouldn't the algorithm be moving to the previous Node instead of the next Node in this case? Below is the pseudo-code:
LIST-SEARCH(L, k)
x <- head
while x != NIL and key != k
do x <- next[x]
return x
Also, here is the header file for my Linked list implementation. I haven't tried to implement it in Template form yet just to keep things simple:
#ifndef linkList_H
#define linkList_h
//
// Create an object to represent a Node in the linked list object
// (For now, the objects to be put in the list will be integers)
//
struct Node
{
// nodes of list will be integers
int number;
// pointer to the next node in the linked list
Node* next;
};
//
// Create an object to keep track of all parts in the list
//
class List
{
public:
// Contstructor intializes all member data
List() : m_listSize(0), m_listHead(0) {}
// methods to return size of list and list head
Node* getListHead() const { return m_listHead; }
unsigned getListSize() const { return m_listSize; }
// method for adding a new node to the linked list,
// retrieving and deleting a specified node in the list
void addNode(Node* newNode);
Node* getNode(unsigned position);
private:
// member data consists of an unsigned integer representing
// the list size and a pointer to a Node object representing head
Node* m_listHead;
unsigned m_listSize;
};
#endif
Implementation of addNode method:
//
// Method adds a new node to the linked list
//
void List::addNode(Node* newNode)
{
Node* theNode = new Node;
theNode = newNode;
theNode->next;
m_listHead = theNode;
++m_listSize;
}
Try this to construct the list:
void List::addNode(int number)
{
newNode = new Node;
newNode -> number = number;
newNode -> next = m_listHead ;
m_listHead = newNode;
++m_listSize;
}
It will add nodes to the head. Perhaps you may wish to store the pointer to the tail and insert the nodes there.
Unfortunately your code doesn't resemble the pseudo code you supply.
The pseudo-code is for searching a linked-list for a key, not a position.
The pseudo code reads as:
Assign head to (node) x.
while x isn't null and the key inside the current node (x) doesn't match k
assign x->next to x
return x
The returned value is either a pointer to the node that contains k or null
If you're trying to find the node at a given position your loop would be (note this is assuming you're going to use a zero-based index for accessing the list):
Assign head to (node) x
assign 0 to (int) pos
while x isn't null and pos not equal to given position
assign x->next to x
increment pos
return x
The result will either be a pointer to the node at the given position or null (if you hit the end of the list first)
Edit: Your code is very close to the latter if that's what you're trying to do ... can you see the difference?
Edit because I like homework where the OP asks the right questions :)
Node* List::getNodeContaining(int searchValue)
{
Node* current = m_listHead;
while (current != 0 && current->number != searchValue)
{
current = current->next;
}
return current;
}
Node* List::getNodeAtPos(int position)
{
Node* current = m_listHead;
int pos = 0;
while (current != 0 && pos != position)
{
current = current->next;
pos++;
}
return current;
}
You list is very different from what a normal list ADT looks like. Rather than returning nodes, which would require the client know about the list implementation, you return and accept the type you're making a list of.
In this case you're making a list of integers, sou you'd want
public:
void add(int num); //prepends an Item to the list
int get(int pos);
The implementations of both are simple. Add makes a new node, and links it in;
void List::add(int num)
{
Node *newNode = new Node;
newNode->number = num;
newNode->next = m_listHead;
m_listHead = newNode;
m_listSize++;
}
Then get is easy too:
int List::get(int pos)
{
if(pos>m_listSize)
;//throw an error somehow
Node *tmp = m_listHead;
while(pos-->0)
tmp=tmp->next;
return m->number
}