Jul 9, 2018 at 1:32am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
#include <iostream>
#include <assert.h>
using namespace std;
struct node {
int data;
node *next;
};
class LinkedList {
node *head;
node *tail;
public :
LinkedList(){
head = nullptr ;
tail = nullptr ;
};
void createNode(int value);
void insertNodeAtStart(int value);
void insertNodeAtEnd(int value);
void insertNodeAtPosition(int position, int value);
void deleteNodeAtPosition(int position);
void display();
};
void LinkedList::createNode(int value){
assert (head == NULL && "List is not empty" );
node *temp = new node;
temp->data = value;
temp->next = nullptr ;
head = temp;
tail = temp;
// delete temp; --> I want to put this to free memory. but I can't. It will make linkedlist un-usuable.
temp = nullptr ;
};
void LinkedList::insertNodeAtStart(int value){
node *temp = new node;
temp->data = value;
temp->next = head;
head = temp;
// delete temp; --> I want to put this to free memory. but I can't. It will make linkedlist un-usuable.
temp = nullptr ;
};
void LinkedList::insertNodeAtEnd(int value){
node *temp = new node;
temp->data = value;
temp->next = nullptr ;
tail->next = temp;
tail = tail->next;
// delete temp; --> I want to put this to free memory. but I can't. It will make linkedlist un-usuable.
temp = nullptr ;
};
void LinkedList::insertNodeAtPosition(int position, int value){
node *count = new node;
count = head;
for (int i = 1; i < position-1; ++i){
count = count->next;
}
node *temp = new node;
temp->data = value;
temp->next = count->next;
count->next = temp;
delete temp; // * I can free memory. ??
temp = nullptr ;
};
void LinkedList::display(){
node *displayNode = new node;
displayNode = head;
while (displayNode != nullptr )
{
cout<<displayNode->data<<"\t" ;
displayNode=displayNode->next;
}
};
int main(){
LinkedList *list1 = new LinkedList();
list1->createNode(4);
list1->insertNodeAtEnd(6);
list1->insertNodeAtEnd(7);
list1->insertNodeAtEnd(8);
list1->insertNodeAtStart(3);
list1->insertNodeAtStart(2);
list1->insertNodeAtPosition(4, 5);
list1->insertNodeAtPosition(8, 9);
list1->display();
};
Please, anyone can Explain this?
Last edited on Jul 9, 2018 at 1:34am UTC
Jul 9, 2018 at 1:39am UTC
writing delete temp
deletes the "node" memory, that is allocated on the heap, and that your other nodes are pointing to. You dont want to do this (unless you actually want to delete the node).
remember "temp" is just a pointer that is sitting on the stack but it is pointing to a node on the heap.
you dont have to / cant delete the "temp" pointer itself (its automatically pop'd from the stack when it goes out of scope)
Last edited on Jul 9, 2018 at 1:58am UTC
Jul 9, 2018 at 10:04am UTC
Hi Stav, Thanks for reply.
Yes It makes sense, every node needs memory for their existence. You gave me a good clue.
But the one thing you mentioned, that I couldn't understand fully was..
As far as I know, when it comes to Dynamically allocated memory, It has no scope.
- Dynamically allocated memory has no scope and will stay allocated until you deallocate it or the program terminates.
http://www.learncpp.com/cpp-tutorial/69-dynamic-memory-allocation-with-new-and-delete/
Any thoughts?
Last edited on Jul 9, 2018 at 10:14am UTC