• 0

[C++] hints needed


Question

I have an assignment due for my class taking 2 linked lists and combining them in order into a third.

but all we talked about was inserting a new Node at specific points and deleting them

i cant seem to get the basic task of just making the a linked list without an error.

the structure looks like

struct Node
{
 ? ? int item;
 ? ? Node *next;
};

all i want is a way to make 1 linked list with values 1, 3, 5, 7, 9, 11, 13, 15, 17, 19

and the other 0, 2, 4, 6, 8, 10, 12, 14, 16, 18

the assignment is combining the two into a third

if someone could help(hints of course - I dont need code psuedocode would be great to explain it) with this I could start on the rest of the assignment

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

main code i have sofar

int main()
{
Node *head;
	Node *cur;
	Node *newPtr;
	head = new Node;
	cur = head;
	int item_enter = 0;

	for (int i = 0; i < 10; i++)
	{  
        cur->item = item_enter;
  newPtr = new Node;
  cur->next = newPtr;	
  cur = newPtr;
  item_enter += 2;
	}
	cur->next = NULL;

	for(Node *cur = head; cur != NULL; cur = cur->next)
  cout << cur->item << endl;

	return 0;
}

Link to comment
Share on other sites

  • 0

// this way, your i = the num u want to insert in the list 1
//   and i+1 = the num. u want to insert in the list 2
//    so that way, u can create both lists in one loop
for (int i = 0; i < 20; i+=2) {
  ...
}

You can now remove the item_enter variable; use i instead.

Also, just duplicate what u already have in the loop, and fill the 2nd list with i+1 instead of i.

Link to comment
Share on other sites

  • 0

int main()
{
	Node *head, *head_2;
	Node *cur, *cur_2;
	Node *newPtr;
	head = new Node;
	head_2 = new Node;
	cur = head;
	cur_2 = head_2;

	for (int i = 0; i < 20; i+=2)
	{  
        cur->item = i;
  newPtr = new Node;
  cur->next = newPtr;	
  cur = newPtr;
	}
	cur->next = NULL;

	for (int j = 1; j < 21; j+=2)
	{  
        cur_2->item = j;
  newPtr = new Node;
  cur_2->next = newPtr;	
  cur_2 = newPtr;
	}
	cur_2->next = NULL;

	for (Node *cur = head; cur != NULL; cur = cur->next)
  cout << cur->item << endl;
	for (Node *cur_2 = head; cur_2 != NULL; cur_2 = cur_2->next)
  cout << cur_2->item << endl;

	return 0;
}

output is as follows

2
4
6
8
10
12
14
16
18
-842150451

2
4
6
8
10
12
14
16
18
-842150451
Press any key to continue

Link to comment
Share on other sites

  • 0

First, u should merge the two for loops into one like i just explained.

2nd, u get that -842150451 because u add the pointer to the list before u fill the item variable for it on the next iteration... so what happens on the last iterations is:

 newPtr = new Node;
  cur->next = newPtr;
  cur = newPtr;

cur->next = NULL;

so u create a new node, specify that the current node point to this new node, then change the *new* node next pointer to null... so u basically added an empty (non-initialized item) node at the end of your list.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.