473,513 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Alternate output between two linked lists

beacon
579 Contributor
I'm writing a program that will alternate printing out data between two linked lists into a third list. Actually I have to write a function that will do it.

I wanted to test it out, so I created two lists with test number in them in main and started to write my function, but I have to pass the head pointer for each of the linked lists by reference and I'm stuck on what I need to do with the third pointer.

Anyway, if list 1 has the numbers 1, 3, 5, 7 and list 2 has the numbers 2, 4, 6, 8 the third list will print out 1, 2, 3, 4, 5, 6, 7, 8. Any extra values are to be omitted.

Here's what I've got so far:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct node{
  6.     int data;
  7.     node *next;
  8. };
  9.  
  10. void interleave(node*,node*,node*);
  11.  
  12. void main(){
  13.  
  14.     node * head1;
  15.     node * list1_1;
  16.     list1_1 = new node;
  17.     head1 = list1_1;
  18.     list1_1->data = 2;
  19.     list1_1->next = NULL;
  20.  
  21.     node * list1_2;
  22.     list1_2 = new node;
  23.     list1_2->data = 5;
  24.     list1_1->next = list1_2;
  25.     list1_2->next = NULL;
  26.  
  27.     node * list1_3;
  28.     list1_3 = new node;
  29.     list1_3->data = 7;
  30.     list1_2->next = list1_3;
  31.     list1_3->next = NULL;
  32.  
  33.     node * list1_4;
  34.     list1_4 = new node;
  35.     list1_4->data = 9;
  36.     list1_3->next = list1_4;
  37.     list1_4->next = NULL;
  38.  
  39.     //********************************************
  40.  
  41.     node * head2;
  42.     node * list2_1;
  43.     list2_1 = new node;
  44.     head2 = list2_1;
  45.     list2_1->data = 3;
  46.     list2_1->next = NULL;
  47.  
  48.     node * list2_2;
  49.     list2_2 = new node;
  50.     list2_2->data = 6;
  51.     list2_1->next = list2_2;
  52.     list2_2->next = NULL;
  53.  
  54.     node * list2_3;
  55.     list2_3 = new node;
  56.     list2_3->data = 8;
  57.     list2_2->next = list2_3;
  58.     list2_3->next = NULL;
  59.  
  60.     //********************************************
  61.  
  62.     /*while(head1){
  63.         cout<<head1->data<<endl;
  64.         head1 = head1->next;
  65.     }
  66.  
  67.     cout<<endl<<endl;
  68.  
  69.     while(head2){
  70.         cout<<head2->data<<endl;
  71.         head2 = head2->next;
  72.     }*/
  73.  
  74.     interleave(head1,head2,head3);
  75.  
  76. }
  77.  
  78. void interleave(node*head1, node*head2, node*head3){
  79.  
  80.     while(head1 || head2 != NULL){
  81.  
  82.         node * head3;
  83.         head3 = new node;
  84.         head3 = head1;
  85.         head3->next = head2;
  86.         cout<<head3;
  87.     }
  88. }
  89.  
Nov 11 '07 #1
9 3287
beacon
579 Contributor
This is kinda off the subject, but how do I get my code that I include in my questions to be color differently. I've seen it in other posts and was just curious.
Nov 11 '07 #2
scruggsy
147 New Member
Expand|Select|Wrap|Line Numbers
  1. void interleave(node*head1, node*head2, node*head3){
  2.  
  3.     while(head1 || head2 != NULL){
  4.  
  5.         node * head3;
  6.         head3 = new node;
  7.         head3 = head1;
  8.         head3->next = head2;
  9.         cout<<head3;
  10.     }
  11. }
  12.  
Line 3: What are you trying to do with this expression? As written, the code inside your while loop will continue to execute as long as either head1 or head2 has a value other than NULL. This is fine if you know ahead of time that both lists have exactly the same number of elements; it is not fine if the number of elements can differ between the 2 lists. && instead of || would be safer.
Also, head1 and head2 have values (the addresses of your linked lists), and those values never change, so this loop will never terminate. If you are looking for the end of the list as a loop termination condition, you should be looking at the next members, not the heads of the lists.

Lines 5-6: Do you want to create a new linked list every time through the loop? It seems to me you want one new linked list, to which you will add 2 elements each time through your loop. head3 then should be declared and initialized outside of the loop.

Lines 7-8: Each time through your loop you are adding the first element of lists 1 and 2 to your new linked list. What you want to be doing is adding the next element of each list, each time through. Otherwise (assuming you fix the rest of this loop), you'll end up with an infinite list containing 2 3 2 3 2 3 2 3 2 3 .... Use the next member of lists 1 and 2 to traverse the lists.

Line 9: This will output the address of head3 as a number, which is probably not what you want. You probably want to display the data stored in one of head3's nodes.

Hope this helps. If not, keep asking. You will get it eventually.

EDIT: Use [ code=cpp] [ /code]
Nov 11 '07 #3
beacon
579 Contributor
In line 5 and 6, are you saying that node * head3 AND head3 = new node should be intialize outside of the loop or just node * head3?

One thing I'm really not clear on is whether or not I have this set up to pass the parameters correctly. I have to use the three head pointers as parameters in my function, but I wasn't sure if I should do everything that I did with list 1 and 2 in the function or if it should stay where it is.

Thanks for the help scrug, by the way.
Nov 11 '07 #4
scruggsy
147 New Member
In line 5 and 6, are you saying that node * head3 AND head3 = new node should be intialize outside of the loop or just node * head3?
Well, think about it. You only need one new linked list. If you call head3 = new node inside your while loop, you'll overwrite what is stored in head3 each time through.
However, if you initialize head3 as a new node before the loop, you can then add nodes to it within your loop to create a list.
The important thing to remember is to keep track of the current node you are operating on within your loop. That way, you can do currentNode->next = new node to add a new element at the end of the list.

One thing I'm really not clear on is whether or not I have this set up to pass the parameters correctly. I have to use the three head pointers as parameters in my function, but I wasn't sure if I should do everything that I did with list 1 and 2 in the function or if it should stay where it is.
If your instructor wants you to pass three node*'s as params, then you've done the function header properly. But in that case there should be no need to declare a third node* head3 within the function, as it is passed as a parameter. Currently, in main() you are calling interleave(head1, head2, head3) but head3 is never defined in main(), so this won't compile.
Nov 11 '07 #5
beacon
579 Contributor
Ok...I think I'm almost set. Let me ask you this while I've got your attention. When I write the interleave function in main do I just type interleave(node*,node*,node*head3)? I got an error when I compiled with this and I guess I don't understand how I'm going to get the function going with the pointers in there.

We really didn't learn how to do this...we were given a take home assignment and kind of just asked to figure it out. I like that style most of the time, but with this it's been difficult.

By the way, here's my modified function:
Expand|Select|Wrap|Line Numbers
  1. void interleave(node*head1, node*head2, node*head3){
  2.  
  3.     head3 = new node;
  4.     while(head1->next && head2->next != NULL){
  5.  
  6.         head3 = head1->next;
  7.         head3->next = head2->next;
  8.         cout<<head3->data;
  9.     }
  10. }
  11.  
Nov 11 '07 #6
scruggsy
147 New Member
We really didn't learn how to do this...we were given a take home assignment and kind of just asked to figure it out. I like that style most of the time, but with this it's been difficult.

By the way, here's my modified function:
Expand|Select|Wrap|Line Numbers
  1. void interleave(node*head1, node*head2, node*head3){
  2.  
  3.     head3 = new node;
  4.     while(head1->next && head2->next != NULL){
  5.  
  6.         head3 = head1->next;
  7.         head3->next = head2->next;
  8.         cout<<head3->data;
  9.     }
  10. }
  11.  
Hmm, I'm surprised your instructor didn't explain this stuff. Linked lists are notorious for making students' heads spin. ;)
You are getting somewhere with this function.
However, remember the structure of a linked list: the list is formed by each node having a pointer to the next, which has a pointer to the next, etc until the end of the list.
So when adding a node to the end of the list, you need to know the last node:
Expand|Select|Wrap|Line Numbers
  1. lastNode->next = new node
Similarly, when traversing a list (going through the nodes one at a time), you get to the next node via currentNode->next
Look at your code: each time through the loop, you're doing head1->next, head2->next, and head3->next. These always give you the second element of the list. To get the third, you need to do secondNode->next; for the fourth, thirdNode->next.

So while you are traversing this list, you want to keep track of your current position in the list. Easiest way to do that is with head1 = head1->next, for instance. This way, each time through the loop you are dealing with the next element in the list, until no more remain. Same for head2 and for adding nodes to head3.

Does your textbook have illustrations or diagrams of linked lists? It's much easier to grasp if you can visualize it.

When I write the interleave function in main do I just type interleave(node*,node*,node*head3)? I got an error when I compiled with this and I guess I don't understand how I'm going to get the function going with the pointers in there.
You can't declare a variable within a function call. Declare it first, then pass it to your function.
Nov 11 '07 #7
beacon
579 Contributor
I'm still confused. I added this to the function...but I'm not sure if this is where you're going or what you're trying to say.

Expand|Select|Wrap|Line Numbers
  1. void interleave(node*head1, node*head2, node*head3){
  2.  
  3.     head3 = new node;
  4.     while(head1->next && head2->next != NULL){
  5.  
  6.         head3 = head1->next;
  7.         head3->next = head2->next;
  8.         head1 = head1->next;
  9.         head2 = head2->next;
  10.         cout<<head3->data;
  11.     }
  12. }
  13.  
The book I have show linked lists embedded in classes and it's much different using them independently in a function, in my opinion. We've studied linked lists, but it's the pointer passing that we really didn't tackle. That's what's throwing me off my game.

With the code I've written above and with a function interleave(head1,head2,head3) in main, I got zero errors, but nothing output to the screen.
Nov 11 '07 #8
beacon
579 Contributor
Anybody else have any ideas what I'm doing wrong?
Nov 11 '07 #9
beacon
579 Contributor
I got this to work, save for a debugging error after I executed, but I think that's only because the next value is NULL. If anyone know how to fix this let me know, otherwise I'm finished working on it.
Nov 12 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

7
4806
by: Chris Ritchey | last post by:
Hmmm I might scare people away from this one just by the title, or draw people in with a chalange :) I'm writting this program in c++, however I'm using char* instead of the string class, I am...
10
15100
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
1
12825
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
12
2495
by: Jonathan Bartlett | last post by:
Just finished a new IBM DeveloperWorks article on linked lists, and thought you all might be interested. It's not an introduction -- it instead covers some of the more interesting aspects of...
4
3585
by: MJ | last post by:
Hi I have written a prog for reversing a linked list I have used globle pointer Can any one tell me how I can modify this prog so that I dont have to use extra pointer Head1. When I reverse a LL...
3
472
by: Little | last post by:
Could someone tell me what I am doing wrong here about declaring mutiple double linked lists. This is what the information is for the project and the code wil be below that. Thank your soo much for...
12
3928
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1...
19
7801
by: Dongsheng Ruan | last post by:
with a cell class like this: #!/usr/bin/python import sys class Cell: def __init__( self, data, next=None ): self.data = data
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7162
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7527
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5090
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1597
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.