trying to use dynamic memory to create a new node?

I know you guys don't like the word help, and I assure you I'm not looking for anyone to give me an answer. This is a homework problem, and it requires me to use dynamic memory to create a new node for x value I pass in not equal to the x value in any other node. Below is what I have so far. Is there anyway someone could point me in the right direction? I feel like I might just be making a really stupid mistake, but I don't know. Thanks for any and all responses

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
  void Singly_Linked_List :: add_unique_age_to_the_front(int x)
{
	//Insert a new age to the front of the singly linked list

	
	
	
	
	
	
	
	
	
	
	Node* addUnique=head;
	
	
   

	while(addUnique!=NULL)
	{
		 

		if(x!=addUnique->age)
		{
			
			 Node* newNode2;
			newNode2= new Node(x);
			newNode2->next=head;
		     head= newNode2; 
			
		}

		else
		{
			 cout<<"This number: " << x << " already exists in the singly linked list and cannot be entered again" << endl;
		}

		
		


	      addUnique=addUnique->next;

		  
	
	}

	
	  
	    	 
	    
		

}


Last edited on
I apologize for being pushy and bumping my own post, but I really feel close to figuring out what I've done wrong but I just can't do it.

In my original description, I didn't specify my problem. In my output, the program acknowledges that the x I'm passing in is part of the linked list and displays the message "This number already exists"

But for some reason it also goes right ahead and adds the x I've passed in to my linked list and I can't figure out why.
Write small functions: each small function doing one small thing. For instance, one function to check if a value exists in the list and another to add a value unconditionally to the front of the list.

Then add_unique_to_front becomes easy to reason about.

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
#include <iostream>

struct singly_linked_list
{

    struct node
    {
        int value ;
        node* next ;
        node( int v, node* n = nullptr ) : value(v), next(n) {}
    };

    // ... destructor etc. ...

    bool is_present( int x ) const // return true if 'x' is present in the list
    {
        for( node* n = first ; n != nullptr ; n = n->next ) if( n->value == x ) return true ;
        return false ;
    }

    void push_front( int x ) // unconditionally, add 'x' to the front of the list
    {
        if( first == nullptr ) first = last = new node(x) ;
        else
        {
            node* n = new node(x,first) ;
            first = n ;
        }
    }
    
    // if 'x' is not present in the list, add it to the front of the list
    void add_unique_to_front( int x ) { if( !is_present(x) ) push_front(x) ; } 

    std::ostream& print( std::ostream& stm = std::cout ) const
    {
         for( node* n = first ; n != nullptr ; n = n->next ) stm << n->value << ' ' ;
         return stm << ']' ;
    }

    node* first  = nullptr ;
    node* last = nullptr ;
};

int main()
{
    singly_linked_list lst ;

    for( int v : { 0, 1, 2, 1, 3, 2, 4, 5, 1, 6, 7, 5, 0, 7, 8, 8 } )
    {
        std::cout << "add_unique_to_front(" << v << ") => [ " ;
        lst.add_unique_to_front(v) ;
        lst.print() << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/420da2bf452a2f07
Last edited on
Thanks for the response. I actually am using multiple different functions inside of multiple classes. I don't use structs. Anyways, I tried this and it worked


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Node* addUnique=head;
	
	
   

	
		
		if(!test_age_contained_in_singly_linked_list(x))
		{
			
		 Node* newNode2;
			newNode2= new Node(x);
			newNode2->next=head;
		     head= newNode2; 
		
		}






In another function, the final one I'm writing for my program, I need to delete a node from anywhere in the linked list if it matches another node. How can I say test_age_contained_in_singly_linked_list(x) is true in an if statement?

I've tried simply calling the function in an if statement but it only deletes the first node
Topic archived. No new replies allowed.