Click here to Skip to main content
15,902,904 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C++
#include "stdafx.h"
#include 

using namespace std;

typedef struct node {
	int data;
	node *next;
};

void push(node*,int);
void print(node*);

int main()
{
	node* head = NULL;
	push(head, 2);
	if (head == NULL) {
		cout << "vrvrvr";
	}
	push(head, 3);
	push(head, 5);
	push(head, 2);
	//print(head);
	getchar();
    return 0;
}

void push(node* x, int y){
	node *temp = new node();
	if (x == NULL) {
		temp->next = x;
		temp->data = y;
		x = temp;
		
	}
	else {
		node *temp1 = new node();
		temp1 = x;
		while (temp1->next != NULL) {
			temp1 = temp1->next;
		}
		temp1->next = temp;
		temp->data = y;
		temp->next = NULL;
		delete temp1;
	}
}

void print(node* x){
	node *temp1 = new node();
	temp1 = x;
	while (temp1->next != NULL) {
		cout << temp1->data << endl;
		temp1 = temp1->next;
	}
}
Posted
Updated 7-Jan-16 6:47am
v2
Comments
Tomas Takac 7-Jan-16 12:51pm    
I guess you need to pass pointer to a pointer to a node (node**) otherwise you will be not able to modify the value in x.
simi5 7-Jan-16 13:23pm    
Exception thrown: read access violation.

temp1 was 0x1.

If there is a handler for this exception, the program may be safely continued.

Still not working
Richard MacCutchan 7-Jan-16 13:28pm    
You need to initialise head to point to something before the first call to push.
simi5 7-Jan-16 13:53pm    
solved it. thank u for the help

1 solution

There are three problems in the code of your push function:
C++
void push(node* x, int y){
	node *temp = new node();
	if (x == NULL) {
		temp->next = x;
		temp->data = y;
		x = temp; // this will not do what you want!!! Note that
                      // x is being passed by value!
		
	}
	else {
		node *temp1 = new node(); // why allocate a second node? Just need a pointer
		temp1 = x; // you lose that freshly allocated node; => memory leak
		while (temp1->next != NULL) {
			temp1 = temp1->next;
		}
		temp1->next = temp;
		temp->data = y;
		temp->next = NULL;
		delete temp1; // you are not deleting the temporary node from above
                     // but the second last node of your list!!!

	}
}


To solve the first problem you need to pass a pointer to a pointer (as long as you are working in C; in C++ you could use a reference parameter). This will look like this:
C++
void push (node** ppAnchor, int value)
{
    ...


To eliminate the second and third problem I would suggest that you run with a pointer over your existing list and make that pointer always point to the place where a new node can be attached; like this:

C++
void push (node** ppAnchor, int value)
{
    node** pLink = ppAnchor;
    while (*pLink != NULL)
        pLink = &(*pLink)->next;


Now pLink is pointing the next member of the last node in the list or to the anchor of the list if it is empty. So it points exactly to the location where you want to attach your new node. The rest is simple:

C++
    node* pNode = new node;
    pNode->data = data;
    pNode->next = NULL;
    *pLink = pNode;
}


In your main you call the function like this:
C++
int main()
{
	node* head = NULL;
	push (&head, 2);


A last remark: I would call that function append instead of push, as it attaches the new element to the end of the list.

Attaching the new element at the begin of the list is even easier than that and doesn't need the while-loop. I leave that as an exercise to you.

Now, that while-loop may get a performance problem if your lists grow to a large size. Therefore, it would be clever to also store a pointer to the last element of the list. To do that, I would define a new structure called List that contains both pointers. Try that also as an exercise.
 
Share this answer
 
Comments
simi5 7-Jan-16 14:38pm    
thank u for the solution! :)

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