any idea whats wrong? i'm stumped.
#ifndef LISTS_H
#define LISTS_H
#include <iostream>
#include <cstddef>
using namespace std;
class Lists {
int A;
private:
struct Node{
int number;
Node *link;
};
typedef Node* NodePtr;
NodePtr head;
public:
void head_insert(NodePtr&, int);
Lists();
void add(int);
void merge_lists(NodePtr, NodePtr);
void show_list();
};
#endif
#include <iostream>
#include <cstddef>
#include <cstring>
#include "lists.h"
using namespace std;
Lists::Lists(){
NodePtr head;
head = new Node;
head = NULL;}
void Lists::head_insert(NodePtr& head, int a_number)
{
NodePtr temp_ptr;
temp_ptr = new Node;
temp_ptr-> number = a_number;
temp_ptr->link = head;
head = temp_ptr;
delete temp_ptr;
}
void Lists::show_list()
{
NodePtr here;
here = head;
while (here != NULL)
{
cout << here-> number << endl;
here = here->link;
}
}
void Lists::add(int the_number){
NodePtr temp_head;
if(head==NULL){
head->number=the_number;
head->link=NULL;
}
else
head_insert(head, the_number);
}
#include <iostream>
#include <cstddef>
#include "lists.h"
using namespace std;
int main(){
int listsize;
Lists firstlist;
Lists secondlist;
Lists mergedlist;
cout << "how many integers for the first list?\n";
cin >> listsize;
int list1[listsize];
cout << "enter " << listsize << " integers in numerical order.\n";
for(int i=0;i<listsize;i++)
cin >> list1[i];
for(int i=0;i<listsize;i++)
firstlist.Lists::add (list1[i]);
cout << "how many for the second?\n";
cin >> listsize;
int list2[listsize];
cout << "enter " << listsize << " integers in numerical order.\n";
for(int i=0;i<listsize;i++)
cin >> list2[i];
for(int i=0;i<listsize;i++)
secondlist.add (list2[i]);
firstlist.show_list();
return 0;
}
Here's a couple ideas:
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
#ifndef LISTS_H
#define LISTS_H
#include <iostream>
#include <cstddef>
using namespace std;
class Lists { // why plural?
int A;
private:
struct Node{
int number;
Node *link;
};
typedef Node* NodePtr;
NodePtr head;
public:
void head_insert(NodePtr&, int);
Lists();
void add(int);
void merge_lists(NodePtr, NodePtr);
void show_list();
};
#endif
#include <iostream>
#include <cstddef>
#include <cstring>
#include "lists.h"
using namespace std;
Lists::Lists(){
NodePtr head;
head = new Node;
head = NULL;}
void Lists::head_insert(NodePtr& head, int a_number)
{
NodePtr temp_ptr;
temp_ptr = new Node;
temp_ptr-> number = a_number;
temp_ptr->link = head;
head = temp_ptr;
delete temp_ptr;
}
void Lists::show_list()
{
NodePtr here;
here = head;
while (here != NULL)
{
cout << here-> number << endl;
here = here->link;
}
}
void Lists::add(int the_number){
NodePtr temp_head;
// I'd rethink this logic:
if(head==NULL){
head->number=the_number; // error! head is null, you can't call its member!
head->link=NULL;
}
else
head_insert(head, the_number);
}
#include <iostream>
#include <cstddef>
#include "lists.h"
using namespace std;
int main(){
int listsize;
Lists firstlist;
Lists secondlist;
Lists mergedlist;
cout << "how many integers for the first list?\n";
cin >> listsize;
int list1[listsize]; // this is nonstandard, I'm guessing you use GCC
cout << "enter " << listsize << " integers in numerical order.\n";
for(int i=0;i<listsize;i++)
cin >> list1[i];
for(int i=0;i<listsize;i++)
firstlist.Lists::add (list1[i]); // why not do this in the last loop?
cout << "how many for the second?\n";
// etc.
cin >> listsize;
int list2[listsize];
cout << "enter " << listsize << " integers in numerical order.\n";
for(int i=0;i<listsize;i++)
cin >> list2[i];
for(int i=0;i<listsize;i++)
secondlist.add (list2[i]);
firstlist.show_list();
return 0;
}
|
Last edited on