473,672 Members | 2,539 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 3294
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(head 1, 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*he ad3)? 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*he ad3)? 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(head 1,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
4819
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 ordered by my instructor and she does have her reasons so I have to use char*. So there is alot of c in the code as well Anyways, I have a linked list of linked lists of a class we defined, I need to make all this into a char*, I know that I...
10
15122
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 *enemydata; // Holds information about an enemy (in a game) // Its a double linked list node
1
12835
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
2503
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 linked lists. http://www.ibm.com/developerworks/linux/library/l-listproc/ Jon ---- Learn to program using Linux assembly language http://www.cafeshops.com/bartlettpublish.8640017
4
3591
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 using the recursive call how to change the last node pointer to head and head->next to null without using the extra global pointer Mayur
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 your assitance in helping me solve this problem. Information: Create 4 double linked lists as follows: (a) A double linked list called NAMES which will contain all C like
12
3944
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 and list2, each node within list1 has various data and needs to have a pointer to the corresponding node in list2, but I cant figure out how to do this. Could someone explain what I might be missing, or maybe point me in the direction of a good...
19
7816
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
8486
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
7446
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4418
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2819
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 we have to send another system
2
2063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.