Doubly Linked List of Structures - Overwriting - c++

So I am attempting to make a program in C++ that performs basic operations on a doubly linked list. The main problem I am having is that every time I add a new element to the list, the id field I enter for the new element overwrites the id fields of all other elements. The id field is the only field that does this, which is extremely confusing to me. I believe my error could lie somewhere in my use of pointers, as this is my first program in C++ using them to manage a list(Pointers in Ada seem much simpler to me). I have ran my program through GDB step-by-step multiple times and still can't seem to determine what is causing the problem. This leads me to believe that it could be a logic error on my part.
Here is my main:
#include <iostream>
#include "list.h"
#include <string.h> // <string>
using namespace std;
int main (void)
{
int choice, printorder;
char idbuffer[100];
rec r;
do
{
cout << "Enter your choice: 1 - Add, 2 - Delete, 3 - Print, 0 - quit. " <<endl;
cin >> choice;
switch ( choice )
{
case 1: //AddItem
cout << "\nEnter ID ";
cin >> idbuffer;
r.id = idbuffer;
cout << "\nFirst Name ";
cin >> r.firstname;
cout << "\nLast Name ";
cin >> r.lastname;
if ( AddItem ( r ) )
{
cout << "\nSuccess!\n";
}
else
{
cout << "\nItem failed to be added.\n";
}
break;
case 2: //Delete
cout << "\nEnter id: ";
cin >> idbuffer;
if ( DeleteItem ( idbuffer ) )
{
cout << "\nDelete OK.\n";
}
else
{
cout << "\nDelete Failed for: " << idbuffer << endl;
}
break;
case 3: // Print
cout << "Enter order: 0 - Ascending, 1 - Descending. \n";
cin >> printorder;
PrintList (printorder);
break;
case 0: // quit
break;
default: // bad choice
break;
} // end switch
}
while ( choice != 0 );// end do while
} // end main
Here is my file list.h:
struct rec
{
char * id;
char firstname[15];
char lastname[15];
rec* prev;
rec* next;
};
int AddItem ( rec r );
int DeleteItem ( char* delid );
void PrintList ( int order );
And finally the AddItem function from my file list.cpp:
#include <iostream>
#include "list.h"
#include <string.h>
using namespace std;
rec * first = NULL;
rec * last = NULL;
int AddItem( rec r )
//Return: 1 if a success, 0 if failed.
//No duplicate id's, sort by lastname
{
rec * ptr = NULL;
rec * current;
rec * myStruct = new rec;
myStruct -> id = r.id;
strcpy(myStruct -> firstname, r.firstname);
strcpy(myStruct -> lastname, r.lastname);
ptr = first;
//Check for duplicate id's, currently commented out due to id's being overwritten
//while (ptr)
//{
// if (ptr -> id == myStruct -> id)
// {
// return 0;
// }
// ptr = ptr -> next;
//}
//ptr = first;
if (first == NULL) //Empty
{
first = myStruct; //Inserts node into empty list
last = myStruct;
myStruct -> prev = NULL;
myStruct -> next = NULL;
return 1;
}
else if (myStruct -> lastname > last -> lastname) //Add to end of list
{
last -> next = myStruct;
myStruct -> prev = last;
last = myStruct;
return 1;
}
else if (myStruct -> lastname && myStruct -> lastname < first -> lastname) //Add to beginning of list
{
ptr = first;
first = myStruct;
myStruct -> next = ptr;
ptr -> prev = myStruct;
return 1;
}
else
{
current = first;
while (current)
{
if (myStruct -> lastname > current -> lastname && myStruct -> lastname <= current -> next -> lastname)
{
ptr = current -> next;
current -> next = myStruct;
myStruct -> prev = current;
myStruct -> next = ptr;
ptr -> prev = myStruct;
return 1;
}
else
{
current = current -> next;
}
}
return 0;
}
I'm sure there are other errors throughout this code and some sloppy programming, but I plan on fixing those up and making the program neater/more efficient after I work out this overwriting problem, as it is also holding me back from testing other parts of my program. Thanks in advance and sorry for the lengthy post.
Update1: After realizing I needed to change the line
myStruct -> id = r.id;
to a string copy, such as:
strcpy(myStruct -> id, r.id);
I am now receiving a segmentation fault.. I would imagine something with my pointers is messed up. Back to work I go and thanks for all the help so far!
Update2: After much more editing and quite a few more errors, I have the program working! I am now in the process of cleaning things up and optimizing the program. Thanks to everyone for the great advice!

All id objects have the same value - the address of the first character of idbuffer.
In main
cin >> idbuffer;
r.id = idbuffer;
^^^^^^^^^
and then further in AddItem
int AddItem( rec r )
//Return: 1 if a success, 0 if failed.
//No duplicate id's, sort by lastname
{
rec * ptr = NULL;
rec * current;
rec * myStruct = new rec;
myStruct -> id = r.id;
^^^^^
//...
You should dynamically allocate memory that will be pointed to by id and there the actual string with id will be copied. Or you should declare data member id as a character array.
For example in main
cin >> idbuffer;
r.id = new char[std::strlen( idbuffer ) + 1];
std::strcpy( r.id, idbuffer );
and in AddItem
rec * myStruct = new rec( r );
A better approach is to use an object of type std::string.

Related

Inserting a basic singly linked list node seems to break my c++ code?

Singly Linked List and Node classes and the start of the main function, where I wrote a brief outline of the code functionality. The issue is toward the end of the main function. I wrote '...' in place of what I believe to be irrelevant code because it simply parses strings and assigns them to the string temp_hold[3] array.
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
string value;
string attr;
string tagname;
Node *next;
Node(string c_tagname, string c_attr, string c_value) {
this->attr = c_attr;
this->value = c_value;
this->tagname = c_tagname;
this->next = nullptr;
}
};
class SinglyLinkedList {
public:
Node *head;
Node *tail;
SinglyLinkedList() {
this->head = nullptr;
this->tail = nullptr;
}
void insert_node(string c_tagname, string c_attr,string c_value) {
Node *node = new Node(c_tagname,c_attr, c_value);
if (!this->head) {
this->head = node;
} else {
this->tail->next = node;
}
this->tail = node;
}
};
int main(int argc, char **argv) {
/* storage is a vector holding pointers to the linked lists
linked lists are created and the linked list iterator sll_itr is incremented when
previous line begins with '</' and the currentline begins with '<'
linked lists have nodes, which have strings corresponding to tagname, value, and attribute
*/
SinglyLinkedList *llist = new SinglyLinkedList();
vector<SinglyLinkedList*> sllVect;
sllVect.push_back(llist);
auto sll_itr = sllVect.begin();
string temp_hold[3];
// to determine new sll creation
bool prev = false;
bool now = false;
//input
int num1, num2;
cin >> num1; cin >> num2;
//read input in
for (int i = 0; i <= num1; ++i) {
string line1, test1;
getline(cin, line1);
test1 = line1.substr(line1.find("<") + 1);
//determine to create a new linked list or wait
if (test1[0] == '/') {
prev = now;
now = true;
} else {
//make a node for the data and add to current linked list
if (i > 0) {
prev = now;
now = false;
//if last statement starts with '</' and current statment starts with '<'
// then start a new sll and increment pointer to vector<SinglyLinkedList*>
if (prev && !now) {
SinglyLinkedList *llisttemp = new SinglyLinkedList();
sllVect.push_back(llisttemp);
sll_itr++;
}
}
//parse strings from line
int j = 0;
vector<string> datastr;
vector<char> data;
char test = test1[j];
while (test) {
if (isspace(test) || test == '>') {
string temp_for_vect(data.begin(),data.end());
if (!temp_for_vect.empty()) {
datastr.push_back(temp_for_vect);
}
data.clear();
} else
if (!isalnum(test)) {
} else {
data.push_back(test);
}
j++;
test = test1[j];
}
//each node has 3 strings to fill
int count = 0;
for (auto itrs = datastr.begin(); itrs!=datastr.end(); ++itrs) {
switch (count) {
case 0:
temp_hold[count]=(*itrs);
break;
case 1:
temp_hold[count]=(*itrs);
break;
case 2:
temp_hold[count]=(*itrs);
break;
default:
break;
}
count++;
}
}
cout << "before storing node" << endl;
(*sll_itr)->insert_node(temp_hold[0], temp_hold[1], temp_hold[2]);
cout << "after" << endl;
}
cout << "AFTER ELSE" << endl;
return 0;
}
And here is the line that breaks the code. The auto sll_itr is dereferenced which means *sll_itr is now a SinglyLinkedList* and we can call the insert_node(string, string, string) to add a node to the current linked list. However when I keep the line, anything after the else statement brace does not run, which means the cout<<"AFTER ELSE"<< endl; does not fire. If I remove the insert_node line, then the program runs the cout<<"AFTER ELSE"<< endl; I am unsure what the issue is.
(*sll_itr)->insert_node(temp_hold[0],temp_hold[1],temp_hold[2]);
cout << "after" << endl;
} //NOT HANGING. This closes an else statement.
cout << "AFTER ELSE" << endl;
return 0;
}
Compiled as g++ -o myll mylinkedlist.cpp and then myll.exe < input.txt And input.txt contains
8 3
<tag1 value = "HelloWorld">
<tag2 name = "Name2">
</tag2>
</tag1>
<tag5 name = "Name5">
</tag5>
<tag6 name = "Name6">
</tag6>
Your linked list isn't the problem, at least not the problem here.
A recipe for disaster in the making: retaining, referencing, and potentially manipulating, an iterator on a dynamic collection that potentially invalidates iterators on container-modification. Your code does just that. tossing out all the cruft between:
vector<SinglyLinkedList*> sllVect;
sllVect.push_back(llist);
auto sll_itr = sllVect.begin();
....
SinglyLinkedList *llisttemp = new SinglyLinkedList();
sllVect.push_back(llisttemp); // HERE: INVALIDATES sll_iter on internal resize
sll_itr++; // HERE: NO LONGER GUARANTEED VALID; operator++ CAN INVOKE UB
To address this, you have two choices:
Use a container that doesn't invalidate iterators on push_back. There are really only two sequence containers that fit that description: std::forward_list and std::list.
Alter your algorithm to reference by index`, not by iterator. I.e. man your loop to iterate until the indexed element reaches end-of-container, then break.
An excellent discussion about containers that do/do-not invalidate pointers and iterators can be found here. It's worth a read.

Reading data from a file into a linked list, and searching for elements in the linked list

I am trying to write code and am taking in data from a file and storing it into a struct and creating a linked list. I don't see any immediate problems with my code but I made a function to check if a zipcode exists within any of the structs in the linked list and it doesn't seem to be working. Here's what the data from the file looks like:
id,price,bedrooms,bathrooms,sqft,yr_built,zipcode
1370804430,543115,2,1,1380,1947,98199
3744000040,518380,4,2.5,2810,2014,98038
3313600266,190000,3,1,1180,1966,98002
EDIT I implemented new code for reading the file into a linked list but my function for finding the zipcode isn't working. When I enter a zipcode I know exists in the file, nothing gets printed.
typedef struct housetype house;
struct housetype{
int id;
int price;
int bedrooms;
double bathrooms;
int sqft;
int year;
int zipcode;
house *next; };
int findzipcode(house* head, int zip){
house* current = head;
while(current != NULL){
if(current->zipcode == zip){
cout << "Zipcode exists" << endl;
return 1;
break;}
current = current->next;
}
return 0;}
int main(){
house *head = NULL;
FILE *houseinfo = NULL;
houseinfo = fopen("house-info-v4.txt", "r");
if (houseinfo == NULL) {
cout << "Error reading file" << endl;
}
else {
int res;
house *last = head;
house h;
do {
res = fscanf(houseinfo, "%d,%d,%d,%lf,%d,%d,%d",
&h.id, &h.price, &h.bedrooms, &h.bathrooms,
&h.sqft, &h.year, &h.zipcode);
if (res > 0) { // <== fscanf successful (if file fits!)
house *n = (house*)malloc(sizeof(house));
memcpy(n, &h, sizeof(house));
n -> next = NULL;
if (last) last->next = n;
if ( ! head) head = n;
last = n;
}
} while (res > 0);
}
int zip;
cout << "Enter a zipcode" << endl;
cin >> zip;
findzipcode(head, zip);}
The code you present will only load the first item, so if you are searching for later items it won't work.
Notice how the code with the fscanf doesn't have a loop.
I think you read only one row from data and made one node for linked list. If you want to make linked list then you have to read all rows from file and create node of each row and link it with loop.
For example
while (1)
read_file
if feof:
break
make_node
add row_info -> node
link_node
Trying to write this code with c++
At least two problems
head remains NULL
only one house is read from the file
Try something like
if (houseinfo == NULL) {
cout << "Error reading file" << endl;
}
else {
int res; // <== fscanf result
house *last = head; // <== last house set (head is still null)
house h; // <== holds values until fscanf check
do {
res = fscanf(houseinfo, "%d,%d,%d,%lf,%d,%d,%d",
&h.id, &h.price, &h.bedrooms, &h.bathrooms,
&h.sqft, &h.year, &h.zipcode);
if (res > 0) { // <== fscanf successful (if file fits!)
house *n = (house*)malloc(sizeof(house)); // c++ cast malloc
memcpy(n, &h, sizeof(house)); // <== copy to allocated house
n -> next = NULL;
if (last) last->next = n; // last next is our n
if ( ! head) head = n; // set head if necessary
last = n; // last house set is n, now
}
} while (res > 0);
}
Explanations are in comments. Basically
loop on fscanf until no more lines in file
read values in local variable h
if fscanf successful, allocate and set n
if we have a last, set its next to our new n
if head not set yet, set it
finally, last points to n to be used at next iteration
First of all select a language. Either C or C++.
There is a problem in your code. The thing is, you have memory leak here and that in one way is the reason for the problem.
Well to say it in detail, you allocate memory and get input and store it in that memory and then you forget about it. You don't forget but yes there is no way you can access it. This is the memory leak. And then you pass a NULL valued pointer to the search function and it returns nothing.
This change will solve the problem,
house *n = malloc(sizeof(house));
head = n; //<----- We are not losing the allocated memory
fscanf(houseinfo, "%d.....
..
Also there is one redundant thing in your code (this is not causing the problem but yes you should know this).
cout << "Zipcode exists" << endl;
return 1;
break; //<--- this is redundant
}
Once a return statement is executed that break will never be executed. Just put the return statement. No use putting the break here.
Another point you didn't free the allocated memory.(Ah! to free you need to keep track of it - which you didn't do here). After you are done working with it you should free it.
findzipcode(head, zip);
free(head);
Also check the return value of malloc. In case it returns NULL take necessary action.
in the following proposed code:
error conditions are checked and acted upon
most of the comments to the question are incorporated
And now, the proposed code:
#include <cstdio>
#include <cstdlib>
#include <errno.h>
struct housetype
{
int id;
int price;
int bedrooms;
double bathrooms;
int sqft;
int year;
int zipcode;
struct housetype *next;
};
typedef struct housetype house;
void findzipcode(house* head, int zip)
{
house* current = head;
while( current )
{
if(current->zipcode == zip)
{
cout << "Zipcode exists" << endl;
return;
}
current = current->next;
}
}
int main( void )
{
house *head = NULL;
FILE *houseinfo = fopen("house-info-v4.txt", "r");
if ( !houseinfo )
{
cerr << "fopen failed due to" << strerror( errno ) << endl;
exit( EXIT_FAILURE );
}
// implied else, fopen successful
int res;
house *last = NULL;
house h;
while( 7 == (res = fscanf(houseinfo, "%d,%d,%d,%lf,%d,%d,%d",
&h.id, &h.price, &h.bedrooms, &h.bathrooms,
&h.sqft, &h.year, &h.zipcode) ) )
house *n = new house;
if( !n )
{
cerr << "malloc failed due to: " << strerror( errno ) << endl;
exit( EXIT_FAILURE );
}
// implied else, malloc successful
memcpy(n, &h, sizeof(house));
n -> next = NULL;
if (last)
last->next = n;
else if ( ! head)
head = n;
last = n;
}
int zip;
cout << "Enter a zipcode" << endl;
cin >> zip;
findzipcode(head, zip);
while( head )
{
house *nextHouse = head->next;
delete head;
head = nextHouse;
}
}
Of course, if you really want to use C++, then suggest using a vector rather than a linked list

Program Terminates unexpectedly C++

The goal of the program is to simulate a medical complex with 6 "Doctor Queues". I tried keeping it close enough to the Java version I've completed (must do this in 3 languages). At this point when I run the DequeuePatients and ListPatients method, the program terminates unexpectedly with no errors. I've tried the debugger but eclipse is ignoring all my breakpoints.Why is it terminating?
The ListPatients method is all follows in Driver class:
void ListPatients() {
int x, QueueChoice = 0;
bool exit = false;``
while (!exit) {
for (x = 1; x <= MAX; x++) {
cout << x << ": " << Doctor[x - 1] << "\n";
} // end-for
cout << "Choose a Queue to List From";
cin >> QueueChoice;
if (OpenFlag[QueueChoice - 1] == true) { //open flag for each queue
int i = Complex[QueueChoice - 1]->GetSize();//Global array of queues
//cout <<i<<endl;
Terminates in this loop if function is called
for (x = 1; x <= i; x++) {
Patient This = Complex[QueueChoice-1]->GetInfo(x); //Program Terminates here
cout << x<< ": " << endl;//This.ID_Number;
//<<Complex[QueueChoice - 1]->GetInfo(x + 1).PrintMe()
} // end-for
} // end-if
cout << "Press 1 to List Another Queue, press 2 to exit";
cin >> x;
switch (x) {
case 1:
break;
case 2:
exit = true;
break;
}//switch
} // end-while
} // List Patients`
Queue Class GetInfo and toArray():
/*Return Patient info from each Node*/
Patient Queue::GetInfo(int Pos) {
Node* ComplexArray= new Node[Length];
ComplexArray = this->toArray();
return ComplexArray[Pos - 1].Info;
}
// The toArray method
Node* Queue::toArray() {
// Copy the information in each node to an array and return the array.
int x = Length;
Node ComplexArray[Length] ={} ;
Node* Current = new Node();
Current = Rear;`
for (x = 0; x<Length;x++) {
ComplexArray[x] = Node();
ComplexArray[x].Modify(Current);
Current = Current->Next;
}
return ComplexArray;
} // End of toArray method
Modify Method in Node:
void Node :: Modify(Node* ThisNode)
{
Info = ThisNode->Info;
Next = ThisNode->Next;
}
If this is base-zero why are you subtracting 1 from x
for (x = 0; x<Length;x++) {
ComplexArray[x-1] = Node();
ComplexArray[x - 1].Modify(Current);
Current = Current->Next;
}
Subscript errors are hard crashes in C/C++.
The problem was sort of basic, it was way I was declaring my pointers, as well as changing the toArray function to return a Patient pointer(That can be treated as an array?) as apposed to Node, and not just to the first element of the array.
this caused toArray to return one Node pointer, with that instances Next pointer continuously pointing to itself.
Queue Complex[6] = Queue(); //in driver class
Patient* ComplexArray[Length] = new Patient();//Queue Class GetInfo & toArray
I needed:
Queue* Complex = new Queue[MAX];
Patient* ComplexArray = new Patient[Length];
and changed these functions:
Patient Queue::GetInfo(int Pos) {
Patient* ComplexArray = new Patient[Length];
ComplexArray= toArray();
return ComplexArray[Pos - 1];
}
// Now returns Patient pointer of Node->Info to GetInfo
Patient* Queue::toArray() {
Node* Current;
int x;
Patient* ComplexArray = new Patient[Length];
Current = Rear;
for (x = 1; x <= Length; x++) {
ComplexArray[x-1] = Patient();
ComplexArray[x - 1].Modify(Current->Info);
Current = Current->Next;
}
return ComplexArray;
} // End of toArray method

inserting into ordered LLL

So far I've been able to correctly add items into my linear linked list but I cannot compare data members from two separate places in my list so that I can find the correct place to insert my new node. I try to create a dummy pointer so that no data maybe lost when I get the next object in the linear linked list so that I may compare the current and previous items. However the previous item just get overridden by the new item. How do I create a dummy pointer to store data without writing over the last object?
(code below)
int political_party::insert_republican_party(candidate_info & a_candidate) {
republican_party * current = new republican_party(); //intiates the new node
current->a_candidate = a_candidate; //gives the new node a perticular value:
current->next = NULL; // declares next the next as null
if (rep_head == NULL) //no nodes exist so far
{
current->next = rep_head; //the node after current contains the value rep_head
rep_head = current; //rep head equals the value of current
}
else if (rep_head->next == NULL) {
republican_party * current2 = rep_head;
current2 = current2->next;
republican_party * previous = rep_head;
// previous -> a_candidate = current -> a_candidate;
// current -> a_candidate = a_candidate;
if (current->a_candidate.get_rank() > previous->a_candidate.get_rank()) {
current->next = rep_head;
rep_head = current;
}
}
else {
republican_party * current2 = rep_head;
while (current->next != NULL) {
current = current->next;
}
current->next = current2->next;
current2->next = current;
return 2;
}
}
Welcome to C++ and welcome to StackOverflow.
You have some hurdles to clear, so I'm going to do the following:
1) make some observations about your code.
2) Then I'm going to show you some sample code that runs with eough "printing" so you can tell what is going on.
3) Then I'll end with a hint or two on how you can proceed.
The big thing I want you to get is learning how to put enough print-outs in your code so you can follow what is going on.
They don't have to be pretty, they're just there to help your troubleshooting, then you can comment them out.
1) observations about your code.
This will be kind of long, I'm including your version of the code to talk about it.
Let's sketch out how the code you posted will run:
0: (step 0, the beginning)
rep_head = NULL
pretty much an empty list.
1: insert_republican_party( c1 );
current points to c1
execute /*A*/ if() block since rep_head is null (see below for /*A*/,
I might edit the original question to have these values for discussion).
after:
rep_head = current, so...
rep_head points to c1.
rep_head next = NULL
at the end of /*A*/ if-block, the list looks something like this:
rep_head-----+
|
v
current----> R[c1] ---next--> NULL
I'm kind of smashing R (republican_party pointer) together with c1 (a_candidate reference).
2: insert_republican_party( c2 );
rep_head points to c1, rep_head.next = NULL
current points to c2, current.next = NULL
We execute the "/*B*/ else if() block" since rep_head->next == NULL.
local var current2 gets pointed to same place rep_head points to.
current2 then pointed to rep_head->next, so current2 = NULL
At this point we don't do anything else with current2.
we'll skip the commented-out lines.
And you're looking to do an in-order insertion for your linked list based on candidate rank...
so we have 3 possible conditions:
maybe c1.rank < c2.rank
maybe c1.rank == c2.rank
maybe c1.rank > c3.rank
The /*C*/ if statement is checking to see if c2.rank > c1.rank
Suppose c2.rank > c1.rank then fine, c2 becomes the new head of the list.
after that:
current->next points to c1.
rep_head points to c2.
So the list might look like this, more or less (assuming c2.rank > c1.rank).
rep_head---+
|
v
current--> R[c2]--next--> R[c1]--next--> NULL
At this stage your previous pointers seem out synch.
Suppose c2.rank <= c1.rank, then... nothing happens and c2 never makes it onto your linked list.
This seems like a hole.
However I would recommend patching that hole in your else-block, /*E* below.
3: insert_republican_party( c3 );
Lets say we somehow get down to /*D*/ else.
and this list looks like step 2, above.
current is pointing to c3 (current.next points to NULL)
rep_head is pointing to c2.
rep_head.next is pointing to c1.
current2 is pointed to same as rep_head, which means c2.
the while() loop walks current down the linked list,
which seems like a logic errror? Should it be walking current2 (instead of current)?
int political_party::insert_republican_party(candidate_info & a_candidate)
{
republican_party * current = new republican_party(); //intiates the new node
current -> a_candidate = a_candidate; //gives the new node a perticular value:
current -> next = NULL; // declares next the next as null
/*A*/ if(rep_head == NULL) //no nodes exist so far
{
current -> next = rep_head; //the node after current contains the value rep_head
rep_head = current; //rep head equals the value of current
}
/*B*/ else if (rep_head -> next == NULL)
{
republican_party * current2 = rep_head;
current2 = current2 -> next;
republican_party * previous = rep_head;
// previous -> a_candidate = current -> a_candidate;
// current -> a_candidate = a_candidate;
/*C*/ if(current -> a_candidate.get_rank() > previous -> a_candidate.get_rank())
{
current -> next = rep_head;
rep_head = current;
}
}
/*D*/ else
{
republican_party * current2 = rep_head;
/*E*/ while(current -> next != NULL)
{
current = current -> next;
}
current -> next = current2 -> next;
current2 -> next = current;
return 2;
}
}
2) sample code that runs
I've hacked up a pretty rough "linked_list.cpp" version that has just enough code to run.
I'm going to suggest you adopt the printout part (Overrides for the output stream << since we're in c++ land).
Then add liberal printouts to your code. Search for "ostream&" in the code to find those overrides.
fwiw, this would have been easier to comment on if you had posted enough C++ code to run; I encourage you to do that in the future.
=== begin sample output ===
$ c++ linked_list.cpp
$ ./a.out
Hello from main()
c1=C[c1, rank=1]
c2=C[c2, rank=1]
c3=C[c3, rank=1]
before, r=R[C[?, rank=-1]]--next--> NULL
after, r=R[C[c1, rank=1]]--next--> NULL
party=Party.rep_head=0
1. calling party.insert_republican_party( C[c1, rank=1] )
insert: adding current=0x1498c20, *current=R[C[c1, rank=1]]--next--> NULL
insert.A: now rep_head=0x1498c20, *rep_head=R[C[c1, rank=1]]--next--> NULL
1. party=Party.rep_head=0x1498c20---> R[C[c1, rank=1]]--next--> NULL
2. calling party.insert_republican_party( C[c2, rank=1] )
insert: adding current=0x1498c50, *current=R[C[c2, rank=1]]--next--> NULL
insert.B: now rep_head=0x1498c20
2. party=Party.rep_head=0x1498c20---> R[C[c1, rank=1]]--next--> NULL
$
=== end sample output ===
=== begin linked_list.cpp ===
#include<iostream>
// disclaimer: I'm not trying to solve anybody's homework problem.
// Just adding enough code (not even good style code at that) to make
// it run well enough to offer a hint or two.
using namespace std;
class candidate_info {
public:
int rank;
const char *name;
int get_rank() { return rank; }
candidate_info( );
candidate_info( int rank, const char *name );
friend ostream& operator<<(ostream& os, const candidate_info& c);
};
class republican_party {
public:
republican_party * next;
republican_party * prev;
candidate_info a_candidate;
republican_party();
friend ostream& operator<<(ostream& os, const republican_party& r);
};
class political_party {
public:
republican_party * rep_head;
political_party();
int insert_republican_party(candidate_info & a_candidate);
friend ostream& operator<<(ostream& os, const political_party & p);
};
int main( int argc, char **argv ) {
cout << "Hello from main()\n";
candidate_info c1( 1, "c1" );
candidate_info c2( 1, "c2" );
candidate_info c3( 1, "c3" );
cout << "c1=" << c1 << "\n";
cout << "c2=" << c2 << "\n";
cout << "c3=" << c3 << "\n";
republican_party r;
cout << "before, r=" << r << "\n";
r.a_candidate = c1;
cout << "after, r=" << r << "\n";
political_party party;
cout << "party=" << party << "\n";
cout << "1. calling party.insert_republican_party( " << c1 << " )\n";
party.insert_republican_party( c1 );
cout << "1. party=" << party << "\n";
cout << "2. calling party.insert_republican_party( " << c2 << " )\n";
party.insert_republican_party( c2 );
cout << "2. party=" << party << "\n";
}
// === CANDIATE_INFO things ===
candidate_info::candidate_info( ) {
this->rank = -1;
this->name = "?";
}
candidate_info::candidate_info( int rank, const char *name ) {
this->rank = rank;
this->name = name;
}
ostream& operator<<(ostream& os, const candidate_info& c)
{
os << "C[" << c.name << ", rank=" << c.rank << "]";
return os;
}
// === REPUBLICAN_PARTY things ===
republican_party::republican_party()
{
next = prev = NULL;
}
ostream& operator<<(ostream& os, const republican_party& r)
{
// note about flush: sometimes when I feed a bad pointer to this
// it can blow up and hit "segmentation fault" so I'm adding
// flushes here and there to give us an idea of how far along we
// actually got before then.
// I strongly encourage you to do something like << for all of your
// classes to make it easier to see what is going on.
// Maybe you did this already, dont know (didn't see the full
// definition for republican_party and other classes).
os << "R[" << flush << r.a_candidate << "]" << flush;
republican_party *p = r.next;
do {
os << "--next--> ";
if( p == NULL ) {
os << "NULL";
} else {
os << " R[" << p->a_candidate << "]";
p = p->next;
}
} while( p != NULL );
return os;
}
// === POLITICAL_PARTY things ===
political_party::political_party() {
rep_head = NULL;
}
ostream& operator<<(ostream& os, const political_party & p) {
os << "Party.rep_head=" << p.rep_head << flush;
if( p.rep_head != NULL ) {
os << "---> " << *p.rep_head;
}
return os;
}
int political_party::insert_republican_party(candidate_info & a_candidate)
{
republican_party * current = new republican_party(); //intiates the new node
current -> a_candidate = a_candidate; //gives the new node a perticular value:
current -> next = NULL; // declares next the next as null
cout << "insert: adding current=" << current << ", *current=" << *current << "\n";
/*A*/ if(rep_head == NULL) //no nodes exist so far
{
current -> next = rep_head; //the node after current contains the value rep_head
rep_head = current; //rep head equals the value of current
cout << "insert.A: now rep_head=" << rep_head << ", *rep_head=" << *rep_head << "\n";
}
/*B*/ else if (rep_head -> next == NULL)
{
republican_party * current2 = rep_head;
current2 = current2 -> next;
republican_party * previous = rep_head;
// previous -> a_candidate = current -> a_candidate;
// current -> a_candidate = a_candidate;
/*C*/ if(current -> a_candidate.get_rank() > previous -> a_candidate.get_rank())
{
current -> next = rep_head;
rep_head = current;
cout << "insert.C: now rep_head=" << rep_head << "\n";
}
cout << "insert.B: now rep_head=" << rep_head << "\n";
}
/*D*/ else
{
republican_party * current2 = rep_head;
/*E*/ while(current -> next != NULL)
{
current = current -> next;
cout << "insert.E: current=" << current << "\n";
if( current != NULL ) {
cout << " *current=" << current << "\n";
}
}
current -> next = current2 -> next;
current2 -> next = current;
cout << "insert.D: now rep_head=" << rep_head << "\n";
return 2;
}
}
=== end linked_list.cpp ===
3) some hints about how you could proceed
I think you are actually fairly close with the /E/ while loop.
I would encourage you to focus your efforts on the /E/ while loop, and comment out everything in the previous /A/ and /B/ if blocks. Comment it out for now because some if it you'll want to salvage and re-use after you teach your while() loop how to figure out where the new candidate_info should go.
This is a bit complicated because the "prev" link part isn't working yet, so I would suggest fixing your code so that --next--> works (singly linked list).
Then come back and modify it so that <--prev-- also works (making it a doubly linked list).
Some questions to consider about making just the "next" linking part work all the way through...
Let's say you were inserting c25 (some candidate name with a rank of 25)..
existing list: rep_head--> R[c10]--next--> R[c20]--next--> R[c30]--next--> NULL
goal list: rep_head--> R[c10]--next--> R[c20]--next--> R[c25]--next--> R[c30]--next--> NULL
Question 1) What do you need to know to say where to splice in c25 ?
Question 2) How can you modify your /E/ while loop to find that splice point?
hint: in additon to checking for hitting end-of-list, what else to you need to consider? (e.g. why do we want c25 to go after c20, but before c30 ?
Question 3) Are you looking for a single splice point? Or would it be easier to solve with both before and after splice points?
You already have effective code that creates a new R[] and puts the candidate_info in them.
The next-handling is a bit rough, so let's close by focusing on that.
Question 4) to splice in R[c25] as show, which of the R's have next values that need to change?
How can your while-loop find those values?
Lastly, some potential null values to watch out for.
If you were inserting at the beginning of the list, what value will your "before" splice point have?
If you were adding to the end of the list, what value will your "after" splice point have?
Is there any difference between inserting into an empty list vs. inserting at both the beginning and end ?
Good luck, I think with a little bit better feedback from cout << printing you'll be able to get where you want to go.

Double linked list implementation

I am having some syntax problems with a double linked list program I am writing for educational purposes. I have created a struct in my header file, and my main program seems to be alright, but implementing my functions in the .cpp file is giving me immense difficulty. I am having trouble discerning the three cases for inserting a record into the list. Specifically, allocating the memory, initializing the list head and tail, and the order of statements is confusing to me, as is passing a copy of the record to be added to my list.
My header file is as follows:
struct rec
{
char * id;
char firstname[15];
char lastname[15];
struct rec* prev;
struct rec* next;
};
int AddItem ( rec r );
int DeleteItem ( char* delid );
void PrintList ( int order );
My .cpp file, which is where the difficulty lies, is as follows:
#include <iostream>
#include "list.h"
#include <string.h>
using namespace std;
// These pointers refer to the head and tail of the list.
rec* first = NULL;
rec* last = NULL;
int AddItem( Rec r )
{
rec* newRecEntry;
rec* current = NULL;
rec* previous = NULL;
// Check for duplicate id
current = first;
while (current)
{
if( strcmp(current -> id, r.id) == 0)
{
return 0;
}
else
// Create a new node
{
newRecEntry = new Rec;
newRecEntry->id = new char[strlen(r.id)+1];
strcpy(newRecEntry->id, r.id);
strcpy(newRecEntry->firstname,r.firstname);
strcpy(newRecEntry->lastname,r.lastname);
newRecEntry->next = NULL;
newRecEntry->prev = NULL;
}
// Find the appropriate position for the node and insert accordingly
// Check to see if the list is empty
if (first == NULL)
{
first = newRecEntry;
last = newRecEntry;
}
else if ( r.lastname>last.lastname)
{
else
{
return 0;
}
/*int DeleteItem(char* ID)
I am supposed to be able to insert at the beginning, middle, and end of the list. Delete an item from the list based on the ID, and print the list in ascending or descending order based on user input, but I'd first simply like to handle the addition of items to said list.
My function definitions are as follows and also contains some errors
lists.cpp
#include <iostream>
#include "list.h"
#include <string.h>
using namespace std;
// These pointers refer to the head and tail of the list.
rec* first = NULL;
rec* last = NULL;
int AddItem( Rec r )
{
rec* newRecEntry;
rec* current = NULL;
rec* previous = NULL;
// Check for duplicate id
current = first;
while (current)
{
if( strcmp(current -> id, r.id) == 0)
{
return 0;
}
else
// Create a new node
{
newRecEntry = new Rec;
newRecEntry->id = new char[strlen(r.id)+1];
strcpy(newRecEntry->id, r.id);
strcpy(newRecEntry->firstname,r.firstname);
strcpy(newRecEntry->lastname,r.lastname);
newRecEntry->next = NULL;
newRecEntry->prev = NULL;
}
// Find the appropriate position for the node and insert accordingly
// Check to see if the list is empty
if (first == NULL)
{
first = newRecEntry;
last = newRecEntry;
}
else if ( r.lastname>last.lastname)
{
else
{
return 0;
}
/*int DeleteItem(char* ID)
{
rec
}
*/
/*void printList(int order)
{
loop
{
cout << ptr -> Id << " ";
cout << ptr -> firstname << " ";
cout << ptr -> lastname << " ";
cout << ptr -> prev << " "; // address of previous
cout << ptr << " "; // address of item
cout << ptr -> next << " "; // address of next item
}
}
Main is as follows:
#include <iostream>
#include "list.h"
#include <string.h> // <string>
using namespace std;
void main (void)
{
int choice, printorder;
char idbuffer[100];
rec r;
do
{
cout << "Enter your choice 1 Add, 2 Delete, 3 Print, 0 quit "<<endl;
cin >> choice;
switch ( choice )
{
case 1: //AddItem
cout << "\nEnter ID ";
cin >> idbuffer;
r.id = idbuffer;
cout << "\nFirst Name ";
cin >> r.firstname;
cout << "\nLast Name ";
cin >> r.lastname;
if ( AddItem ( r ) )
{
cout << "\nSuccess!\n";
}
else
{
cout << "\nItem failed to be added\n";
}
break;
case 2: //Delete
cout << "\nEnter id :";
cin >> idbuffer;
if ( DeleteItem ( idbuffer ) )
{
cout << "\nDelete OK\n";
}
else
{
cout << "\nDelete Failed for " << idbuffer;
}
break;
case 3: // Print
cout << "Enter order 0 - Ascending, 1 - Descending\n";
cin >> printorder;
PrintList (printorder);
break;
case 0: // quit
break;
default: // bad choice
break;
} // end switch
}
while ( choice != 0 );// end do while
} // end main
It may not seem like it, but even this function
int AddItem(Record entry)
{
Record* newRecordPointer;
newRecordPointer=new Record;
strcpy(newRecordPointer->firstName,entry.firstName);
strcpy(newRecordPointer->lastName,entry.lastName);
newRecordPointer->ID=new char[strlen(entry.ID)+1];
strcpy(newRecordPointer->ID, entry.ID);
return 0;
}
is trying to do too many things.
Let's write the pseudocode description of adding an item to a list:
create a new node
populate the new node with the values provided
attach the new node to the list
I've marked the verbs and nouns involved, and you can already see one of the nouns is missing from your function. You're asking AddItem to add an item to a list ... but you don't give it a list to work on.
It's also useful to write out your expectations clearly:
before AddItem is called:
it needs a list to work on
we don't have a list container class, just the records, so we have to pass a Record
let's say we want to add our new item after the Record passed in
after AddItem is called:
whatever Record we passed in, its Next should point to the new node
the new node's Previous should point to the node passed in
etc. etc. (these are the standard doubly-linked list insertion behaviours)
note for later: we haven't described how we store an empty list
if it's a circular list, an empty list will be a Record whose Next and Previous members point to itself
if it's linear, they might both be NULL instead
it could just be a NULL pointer, but then adding the first node to an empty list needs more effort
So, let's say the minimal function that could possibly work is:
void AddItem(Record *insert_after, Record value)
{
Record *new_node = CreateRecord();
CopyRecordValues(new_node, &value);
AttachAfter(insert_after, new_node);
}
Note that if we were writing real C++ the first two lines could just use the copy constructor Record *new_node = new Record(value), but it will take more changes than that to reach idiomatic C++ code from where we started.
Now, given that, can you:
implement those three functions? (CreateRecord and CopyRecordValues are already handled in your current code)
write the equivalent pseudocode for your other operations, and translate it yourself?
Try changing this:
int AddItem(Record entry);
To this:
Record* AddItem(Record entry, Record *insertion_point = NULL );
If insertion_point is NULL, you can assume that the Record is the beginning of a new list.
Now you have enough information to set Next and Previous pointers, and return the newly created node.