Click here to Skip to main content
15,887,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anyone help me in identifying my mistake in the following code to form a linked list
There is no compiler error


I don't take any classes on c++ and learn by myself.
Any help would be appreciated

What I have tried:

C++
#include <iostream>

using namespace std;

struct node
{
    int data;
    node* link;
};

int main()
{
    node * Head =NULL;
    node* temp= new node;

    temp->data=2;
    temp->link=NULL;


    delete temp;

    node*temp5= new node;
    temp5->data=3;
    temp5->link=temp;
    delete temp5;

    node*temp2=new node;
    temp2->data=4;
    temp2->link=temp5;
    temp2=Head;
    delete temp2;

    node*temp1=Head;
    while(temp1->link != NULL)
    {

        temp1=temp1->link;
    }

    delete temp1;

}
Posted
Updated 9-Jun-16 8:57am
v6
Comments
Suvendu Shekhar Giri 9-Jun-16 3:07am    
You haven't mentioned the problem you are facing.

You shouldn't delete allocated nodes (you are basically destroying the list at the same time you're creating it).
Deallocation of memory should happen only when you need the list no more.
 
Share this answer
 
v2
You are creating nodes and immediately deleting them. You also do not link the first one to the Head node, so you do not have a list, linked or otherwise. Your logic should be:
Create a new node
Set forward link to NULL
If Head == NULL Then set Head link to new node
If Head != NULL Then
    temp node pointer tp = Head
    While tp->link != NULL
        tp = tp->link
    tp->link = new node
Repeat for each node

You should then be able to traverse your list from head to the end, checking the content of each node.

It always helps to use your debugger to step through the code so you can see what is happening at each step.
 
Share this answer
 
Just adding to above points, You are trying to delete a NULL pointer which is not correct

node*temp2=new node;
temp2->data=4;
temp2->link=temp5;
temp2=Head;//Head will be NULL here as you have not assigned any thing here.
delete temp2;//Here you are trying to delete NULL.

Adding Some sample code for reference:

C++
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

struct Node
{
	int Data;
	Node* Link;
	friend class LinkedList;
};

class LinkedList
{
private:

	Node* _head;

	Node* GetNewNode(int value, Node* next)
	{
		Node*  newNode = new Node();
		if (newNode != NULL)
		{
			newNode->Data = value;
			newNode->Link = next;
		}
		return newNode;
	}

public:

	LinkedList()
	{
		_head = NULL;
	}

	void Insert(int value, Node* next)
	{
		Node* newNode = GetNewNode(value, next);

		if (newNode != NULL)
		{
			if (_head == NULL)
			{
				_head = newNode;
			}
			else
			{
				Node* temp = NULL;
				temp = _head;
				_head = newNode;

				Node* travereList = NULL;
				travereList = newNode;
				while (travereList->Link != NULL)
				{
					travereList = travereList->Link;
				}
				travereList->Link = temp;
			}
		}
	}

	Node* GetHead()
	{
		return _head;
	}
};

void Print(LinkedList* l)
{
	if (l != NULL)
	{
		Node* temp = NULL;
		temp = l->GetHead();
		while (temp != NULL)
		{
			cout << temp->Data << endl;
			temp = temp->Link;
		}
	}
}

int _tmain(int argc, _TCHAR* argv[])
{

	LinkedList l;
	l.Insert(10, NULL);
	l.Insert(20, NULL);
	l.Insert(30, NULL);
	l.Insert(40, NULL);
	l.Insert(50, NULL);

	Print(&l);

	getchar();
	return 0;
}





Thanks And Regards
Sravan Vurapalli
 
Share this answer
 
v2
Comments
jeron1 9-Jun-16 11:37am    
While deleting the new'ed object may not technically be necessary as the program is immediately exiting, I think it would be good to add it here to maybe get the OP in the good habit of doing so.
In addition to previous solutions.
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Even if no other problem, this code walk the list but does nothing.
C++
while(temp1->link != NULL)
{

    temp1=temp1->link;
}


Advice: Take a sheet of paper and note how is the list on start and how it evolve as you add new nodes, then check if your code match the changes.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900