PDA

View Full Version : Singly Linked List in C++ help???




hardrhymer
Jan 13, 2012, 02:47 PM
i have sorted singly linked list but my problem is starting after it.i have to print the info within the range defined by user input.i tried i little but i need help.

void printUserInput()
{
char x,y;
node * temp=head;
cout<<"Please enter two latter"<<endl;
cin>>x;
cin>>y;

if(x>=y)
{
cout<<"The second user input must come after the first one."<<endl;

}

else
{
while(temp->surname[0]==x){
temp=temp->next;
}
while(temp->surname[0]==y){
cout<<temp->id<<" ";
cout<<temp->surname<<" ";
cout<<temp->name<<" ";
cout<<temp->age<<" "<<endl;
temp=temp->next;
}
}
}



KnightWRX
Jan 13, 2012, 03:33 PM
Use the [CODE] tags to surround your code.

Also, what is that code supposed to do ? What is it doing ? What is your expected result ?

What is head, what does it point to ? What's your node class ?

A lot of missing details here.

hardrhymer
Jan 13, 2012, 03:41 PM
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <cstdlib>

using namespace std;

void selectionSort(string [], int);
void showArray(string [], int);


struct node{
node * next;
node * tail;
node * head;
int id;
string name;
string surname;
int age;

};

node * thelist = NULL;
node * tail = NULL;
node * head = NULL;

void initList(int id, string surname, string name, int age){
thelist=new node;
thelist->next=NULL;
thelist->id=id;
thelist->surname=surname;
thelist->name=name;
thelist->age=age;
tail=thelist;
head=thelist;
}

void addToHead(int newid, string newsurname, string newname, int newage){

node * mynewnode = new node;
mynewnode->id=newid;
mynewnode->surname=newsurname;
mynewnode->name=newname;
mynewnode->age=newage;
mynewnode->next=thelist;
thelist=mynewnode;
head=thelist;


//Question : we did not check whether the list is empty or not, why ?
}

void removeFromHead(){
if(head==NULL){
return;
}
node * temp = head;
head=head->next;
delete temp;
}

node * getNodeFromValue(int searchValue){
node * temp=thelist;
while(temp!=NULL && temp->id!=searchValue){
temp=temp->next;
}
return temp;
}

void insertAfterNode(node * mynode,int newid, string newsurname, string newname, int newage){

node * mynewnode = new node;
mynewnode->id=newid;
mynewnode->surname=newsurname;
mynewnode->name=newname;
mynewnode->age=newage;
mynewnode->next=mynode->next;
mynode->next=mynewnode;
}

void addToTail(int newid, string newsurname, string newname, int newage) {
if (tail != 0) {
node * mynewnode = new node;
mynewnode->id=newid;
mynewnode->surname=newsurname;
mynewnode->name=newname;
mynewnode->age=newage;
tail->next=mynewnode;
tail=tail->next;
tail->next=NULL;

}
else node * head = tail = new node;
}

void deleteAfterNode(node * mynode){
if(mynode->next==NULL){

return;
}
node * temp=mynode->next;
mynode->next=mynode->next->next;
delete temp;
}
void printList(){
node * temp=head;
while(temp!=NULL){
cout<<temp->id<<" ";
cout<<temp->surname<<" ";
cout<<temp->name<<" ";
cout<<temp->age<<" "<<endl;
temp=temp->next;
}
cout<<endl;
}


void sortNodes(node * ptr) {
string temp ;
node * curr;
for(bool Swap = true; Swap; ) {
Swap = false;
for(curr = ptr; curr->next != NULL; curr = curr->next) {
if(curr->surname > curr->next->surname) {
temp= curr->surname;
curr->surname = curr->next->surname;
curr->next->surname = temp;
Swap = true;
}
}
}
}


void printUserInput()
{
char x,y;
node * temp=head;
cout<<"Please enter two latter"<<endl;
cin>>x;
cin>>y;

if(x>=y)
{
cout<<"The second user input must come after the first one."<<endl;
}

else
{
while(temp->surname[0]==x){
temp=temp->next;
}
while(temp->surname[0]==y){
cout<<temp->id<<" ";
cout<<temp->surname<<" ";
cout<<temp->name<<" ";
cout<<temp->age<<" "<<endl;
temp=temp->next;
}
}

}
int main()
{

initList(0,"Surname","Name",0);

ifstream infile;
infile.open ("projectsampleDB.txt");

string sString;
string id;

while(!infile.eof())
{
getline(infile,sString);

int i;

i=sString.find(" ");
int tempId=atoi(sString.substr(0,i).c_str());
sString.erase(0,i+1);

i=sString.find(" ");
string tempSurname=sString.substr(0,i);
sString.erase(0,i+1);

i=sString.find(" ");
string tempName=sString.substr(0,i);
sString.erase(0,i+1);

int tempAge=atoi(sString.substr().c_str());

addToTail(tempId, tempSurname, tempName, tempAge);

}

removeFromHead();


cout<<"Unsorted List : "<<endl;
printList();



cout<<"Sorted List : "<<endl<<endl;

sortNodes(head);
printList();

printUserInput();
}


# PersonelID Surname Name Age
11098 terzi Emre 25
14894 saglar Isil 45
09993 ozden Egemen 33
.......

myprogram must be able to process this kind of files and insert it into a circular singly
linked list. After reading this file, it must be able to sort the list according to surnames. After
that operation(which i failed), program asks for the user input to enter the start and end user surnames and it
will print the persons in the list whose surname is within that user input (inclusive).

KnightWRX
Jan 13, 2012, 05:08 PM
Let's first take care of the basics. You have warnings :

$ g++ -Wall -o test test.cpp
test.cpp: In function 'void addToTail(int, std::string, std::string, int)':
test.cpp:94: warning: unused variable 'head'

It refers to the else part of this function :

void addToTail(int newid, string newsurname, string newname, int newage) {
if (tail != 0) {
node * mynewnode = new node;
mynewnode->id=newid;
mynewnode->surname=newsurname;
mynewnode->name=newname;
mynewnode->age=newage;
tail->next=mynewnode;
tail=tail->next;
tail->next=NULL;

}
else node * head = tail = new node;
}

What is it you're supposed to do in else ?

lloyddean
Jan 13, 2012, 06:51 PM
Extracting the three fields can be accomplished with much less code.


#include <iostream>
#include <sstream>
#include <string>

int main()
{
std::string str("rhymer hard 21");
std::istringstream iss(str);

std::string strName;
std::string strSurName;
int intAge;
iss >> strSurName >> strName >> intAge;

std::cout << strName << " " << strSurName << " " << intAge << "\n";

return EXIT_SUCCESS;
}