C++ Implementing Linkedlist with multiple variables - c++

I've started c++ programming 5weeks back.
I'm working on a program and using linkedlists to store data in the program. So for this is what I'm done and the program is not compiling. I will be glad if the community gives some idea in fixing these errors.
// C++ Assignment.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
// Global variables
string name;
int nic;
int contact;
string address;
int customerId;
string status;
string todaysVehicle;
string vnum;
string serviceType;
int GetInput()
{
int choice;
cin >> choice;
return choice;
}
struct list
{
string name;
int nic;
int contact;
string address;
int customerId;
string status;
string todaysVehicle;
string vnum;
string serviceType;
list *next;
};
// List Function Prototypes
bool empty(list *head);
void DisplayMenu();
void firsttElement(list *&head, list *&last, int nic, string name, int contact, string address, int customerId, string todaysVehicle, string vnum);
void insertCusomter(list *&head, list *&last, int nic, string name, int contact, string address, int customerId, string todaysVehicle, string vnum);
void removeCustomer(list *&head, list *&last, int nic, string name, int contact, string address, int customerId, string todaysVehicle, string vnum);
void customerList(list *current);
bool empty(list *head){
if (head == NULL)
return true;
else
return false;
}
// Main menu of the program
void DisplayMenu(){
}
void firstElement(list *&head, list *&last, int nic, string name, int contact, string address, int customerId, string todaysVehicle, string vnum){
list *temp1 = new list;
temp1->nic = nic;
temp1->next = NULL;
head = temp1;
last = temp1;
list *temp2 = new list;
temp2->name = name;
temp2->next = NULL;
head = temp2;
last = temp2;
list *temp3 = new list;
temp3->nic = contact;
temp3->next = NULL;
head = temp3;
last = temp3;
list *temp4 = new list;
temp4->address = address;
temp4->next = NULL;
head = temp4;
last = temp4;
list *temp5 = new list;
temp5->nic = customerId;
temp5->next = NULL;
head = temp5;
last = temp5;
list *temp6 = new list;
temp6->todaysVehicle = todaysVehicle;
temp6->next = NULL;
head = temp6;
last = temp6;
list *temp7 = new list;
temp7->vnum = vnum;
temp7->next = NULL;
head = temp7;
last = temp7;
}
void insertCustomer(list *&head, list *&last, int nic, string name, int contact, string address, int customerId, string todaysVehicle, string vnum){
if (empty(head))
firstElement(head, last, nic, name, contact, address, customerId, todaysVehicle, vnum);
else {
list *temp = new list;
temp->nic = nic;
temp->next = NULL;
last->next = temp;
last = temp;
}
if (empty(head))
firstElement(head, last, nic, name, contact, address, customerId, todaysVehicle, vnum);
else {
list *temp = new list;
temp->name = name;
temp->next = NULL;
last->next = temp;
last = temp;
}
if (empty(head))
firstElement(head, last, nic, name, contact, address, customerId, todaysVehicle, vnum);
else {
list *temp = new list;
temp->address = address;
temp->next = NULL;
last->next = temp;
last = temp;
}
if (empty(head))
firstElement(head, last, nic, name, contact, address, customerId, todaysVehicle, vnum);
else {
list *temp = new list;
temp->nic = contact;
temp->next = NULL;
last->next = temp;
last = temp;
}
if (empty(head))
firstElement(head, last, nic, name, contact, address, customerId, todaysVehicle, vnum);
else {
list *temp = new list;
temp->nic = customerId;
temp->next = NULL;
last->next = temp;
last = temp;
}
if (empty(head))
firstElement(head, last, nic, name, contact, address, customerId, todaysVehicle, vnum);
else {
list *temp = new list;
temp->todaysVehicle = todaysVehicle;
temp->next = NULL;
last->next = temp;
last = temp;
}
if (empty(head))
firstElement(head, last, nic, name, contact, address, customerId, todaysVehicle, vnum);
else {
list *temp = new list;
temp->vnum = vnum;
temp->next = NULL;
last->next = temp;
last = temp;
}
}
void removeCustomer(list *&head, list *&last){
if (empty(head))
cout << "There are no Customers in the Database! \n";
else if (head == last){
delete head;
head = NULL;
last = NULL;
}
else{
list *temp = head;
head = head->next;
delete temp;
}
}
void customerList(list *current){
if (empty(current))
cout << "The list is empty \n;";
else
{
cout << "The list contains: \n";
while (current != NULL){
cout << current->customerId << endl;
cout << current->nic << endl;
cout << current->name << endl;
cout << current->vnum << endl;
cout << current->todaysVehicle << endl;
cout << current->vnum << endl;
cout << current->contact << endl;
current = current->next;
}
}
}
// Main Fucntion
int main(int argc, _TCHAR* argv[], void insertCustomer(list *&head, list *&last, int nic, string name, int contact, string address, int customerId, string todaysVehicle, string vnum), void removeCustomer(list *&head, list *&last), void customerList(list *current)){
list *head = NULL;
list *last = NULL;
int choice;
do{
system("cls");
cout << "Clean Park Daily Car Services\n";
cout << "******************************\n";
cout << "Please make your selection\n";
cout << " \n";
cout << "1 - Register Customer\n";
cout << "2 - Request for Serivce\n";
cout << "3 - UnRegister Customer\n";
cout << "4 - Update Customer\n";
cout << "5 - View Customer\n";
cout << "6 - View all Customers\n";
cout << "7 - Exit\n";
cout << "\n";
cout << "Choice: ";
cin >> choice;
DisplayMenu();
switch (choice){
case 1: // Register a new customer
system("CLS");
cout << "PLease enter Customer National Id: \n";
cin >> nic;
cout << " Enter Customer Name: \n";
cin >> name;
cout << "Enter Customer Contact Number: \n";
cin >> contact;
cout << "Enter Customer Address: \n";
cin >> address;
cout << "Enter Customer Number: \n";
cin >> customerId;
cout << "Enter Vehicle's Date: \n";
cin >> todaysVehicle;
cout << "Enter Vehicle Number: \n";
cin >> vnum;
void insertCustomer(list *&head, list *&last, int nic, string name, int contact, string address, int customerId, string todaysVehicle, string vnum);
break;
case 2: // Request for Service
system("CLS");
case 3: // Remove customer
system("CLS");
removeCustomer(*&head, *&last);
break;
case 4: // Update customer
system("CLS");
case 5: // View Customer
system("CLS");
case 6: // View All Customers
system("CLS");
void customerList(list *current);
break;
case 7: // Exit Application
cout << "Goodbye!.." << endl;
exit(0);
default:
cout << "System exit \n";
}
} while (choice != 7);
return 0;
}

First of all there is already a type list in namespace std. If you using namespace std; you can't declarate at type named list. You never should name any of your own types list and stay away from using namespace std.
Rename your type listto mylist, for example. Adapt your code like this:
...
struct mylist;
mylist *next;
...
struct mylist
{
...
};
bool empty(mylist *head);
...
void firstElement(myList *&head, myList *&last, int nic, string name, int contact, string address, int customerId, string todaysVehicle, string vnum){
head = last = new myList;
head->next = NULL;
head->nic = nic;
head->contact = contact;
head->address = address;
head->customerId = customerId;
head->name = name;
head->todaysVehicle = todaysVehicle;
head->vnum = vnum;
head->status = status;
head->serviceType = serviceType;
}
void insertCustomer(myList *&head, myList *&last, int nic, string name, int contact, string address, int customerId, string todaysVehicle, string vnum){
if (empty(head))
firstElement(head, last, nic, name, contact, address, customerId, todaysVehicle, vnum);
else {
last->next = new myList;
last = last->next;
last->next = NULL;
last->nic = nic;
last->contact = contact;
last->address = address;
last->customerId = customerId;
last->name = name;
last->todaysVehicle = todaysVehicle;
last->vnum = vnum;
last->status = status;
last->serviceType = serviceType;
}
}
Apart from this I recommend to use std::list and not to do it your own.

Related

This code just won't print the linked list

I have been learning about linked lists. I was writing the below code to print a linked list. But it just won't print the list. Can anybody help me troubleshoot?
#include<iostream>
#include "getline.h"
using namespace std;
struct Node
{
string name, dream;
Node *randomptr;
};
Node *Populating(){
cout << "Enter name(press enter to exit): ";
string name = GetLine();
if(name == "") return NULL;
Node *newOne = new Node;
cout << "enter dream: ";
string dream = GetLine();
newOne->randomptr = NULL;
return newOne;
}
void PrintNode(Node *eachnode)
{
cout << eachnode-> name<<endl;
cout << eachnode-> dream << endl;
}
Node* BuilingLinkedList(){
Node *listHead = NULL;
while(true)
{
Node *newOne = Populating();
if(newOne == NULL) break;
newOne->randomptr=listHead;
listHead = newOne;
}
return listHead;
}
void PrintList(Node *list)
{
for(Node *cur = list; cur!= NULL; cur = cur->randomptr)
PrintNode(cur);
}
int main()
{
Node *list = BuilingLinkedList();
PrintList(list);
}
Node *Populating(){
cout << "Enter name(press enter to exit): ";
string name = GetLine();
if(name == "") return NULL;
Node *newOne = new Node;
cout << "enter dream: ";
string dream = GetLine();
newOne->randomptr = NULL;
return newOne;
}
You're not doing anything with name or dream. They're read into local variables but never assigned to the new node.

Not too sure how to move on from here. Using linked list to search?

Question: Create a Linked list of nodes. Each node should have student names, age, course num, grades and pointer that is used to link the next node in the list. Use append node member function to generate a linked list of 5 students. Create a search member function to display the name of the student who has got 'A' grade. Assume that only one student got "A" grade.
Not too sure how to move on from here. I am new to linked list/nodes and not too sure what I am doing wrong. Any help would be greatly appreciated.
#include <iostream>
#include <cstring>
using namespace std;
struct node
{
string name;
int age;
int coursenum;
char grade;
node *next;
};
node *search (node * head)
{
node *temp = head;
char grade;
while (temp != NULL)
{
//if (temp->grade == 'A')
//if((temp->grade, grade)=='A')
if(strcmp(temp->grade, grade)=='A')
{
cout << temp->name;
return temp;
}
temp = temp->next;
}
}
int main ()
{
node *head = NULL;
string name;
int age;
int coursenum;
char grade;
int i = 0;
while (i < 2)
{
cout << "Enter the students name: ";
cin >> name;
cout << "Enter the studens age: ";
cin >> age;
cout << "Enter the students course number: ";
cin >> coursenum;
cout << "Enter the students grade: ";
cin >> grade;
node *temp = new node;
temp->grade = grade;
temp->next = head;
head = temp;
i++;
}
search (head);
}
strcmp(string1,string2) // returns 0 if strings are equal.
String str1 = 'apple';
String str2 = 'apple';
if(strcmp(str1,str2))
{
cout<<"inside if";
}
else{
cout<<"inside else"
}
// output will be "inside else"
because if condition will evaluate to '0'
Your if statement in search function must be;
if (temp->grade == 'A')
However, your problem is not in your understanding of linked list. You cannot write values to name, age, and coursenum correctly. You need to assign all your read values to struct's variables.
temp->name = name;
temp->age = age;
temp->coursenum = coursenum;
temp->grade = grade;

Program skips over std::getline [duplicate]

This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 6 years ago.
I have to design a program to allow the user to input information for 5 nodes in a doubly linked list and then sort the doubly linked list alphabetically. It skips over allowing the user to input the address, doesn't print any information besides the name ,and doesn't sort the list alphabetically. What am I doing wrong?
#include <iostream>
#include <string>
struct node
{
std::string firstName;
std::string lastName;
std::string address;
long phoneNum;
node *next;
node *prev;
};
bool isEmpty(node *head);
void insertAsFirstElement(node *&head, node *&last, std::string firstName, std::string lastName, std::string address, long phoneNum);
void insert(node *&head, node *&last, std::string firstName, std::string lastName, std::string address, long phoneNum);
void searchFor(node *&last, std::string lastName);
void showList(node *current);
bool isEmpty(node *head)
{
if(head == NULL)
return true;
else
return false;
}
void insertAsFirstElement(node *&head, node *&last, std::string firstName, std::string lastName, std::string address, long phoneNum)
{
node *temp = new node;
temp->firstName = firstName;
temp->lastName = lastName;
temp->address = address;
temp->phoneNum;
temp->next = NULL;
temp->prev = NULL;
head = temp;
last = temp;
}
void insert(node *&head, node *&last, std::string firstName, std::string lastName, std::string address, long phoneNum)
{
if(isEmpty(head))
insertAsFirstElement(head, last, firstName, lastName, address, phoneNum);
else
{
node *temp = new node;
temp->firstName = firstName;
temp->lastName = lastName;
temp->address = address;
temp->phoneNum;
int result = lastName.compare(last->lastName);
if (result < 0)
{
temp->next = head;
head->prev = temp;
temp->prev = NULL;
head = temp;
}
else if ( result > 0)
{
temp->next = NULL;
temp->prev = last;
last->next = temp;
last = temp;
}
}
}
void searchFor(node *&last, std::string findName)
{
node *temp = last;
while (temp != NULL)
{
if (temp->lastName == findName)
{
std::cout << temp->firstName << " " << temp->lastName << " " << temp->address << " " << temp->phoneNum << std::endl;;
}
temp = temp->prev;
}
}
void showList(node *current)
{
if(isEmpty(current))
std::cout << "The list is empty\n";
else
{
std::cout << "The list contains: \n";
while(current != NULL)
{
std::cout << current->firstName << " " << current->lastName << " " << current->address << " " << current->phoneNum << std::endl;
current = current->next;
}
}
}
int main()
{
node *head = NULL;
node *last = NULL;
std::string firstName;
std::string lastName;
std::string address;
long phoneNum;
int count;
count = 5;
while (count > 0)
{
std::cout << "Enter the first name of person #" << count << ":\n";
std::cin >> firstName;
std::cout << "Enter the last name of person #" << count << ":\n";
std::cin >> lastName;
std::cout << "Enter the address of person #" << count << ":\n";
std::getline(std::cin,address);
std::cout << "Enter the phone number of person #" << count << ":\n";
std::cin >> phoneNum;
insert(head, last, firstName, lastName, address, phoneNum);
count = count - 1;
}
showList(head);
std::string findName;
std::cout << "What is the last name of the person you would like to find?\n";
std::cin >> findName;
searchFor(last, findName);
return 0;
}
Your issue is that you are mixing cin >> and getline which is problematic in C++ due to trailing newlines and what not.
Good practice is to always use getline and then use a stringstream to split the line up into tokens. For example, I modified your solution to only use getline and string streams (NOTE: You need to #include <sstream> at the top of your file.
You can read more about the issues with mixing cin >> and getline and also other ways to solve them here.
int main()
{
node *head = NULL;
node *last = NULL;
std::string firstName;
std::string lastName;
std::string address;
std::string phoneNumStr;
long phoneNum;
int count;
count = 5;
while (count > 0)
{
std::cout << "Enter the first name of person #" << count << ":\n";
std::getline(std::cin,firstName); // no use of cin >>
std::cout << "Enter the last name of person #" << count << ":\n";
std::getline(std::cin,lastName); // no use of cin >>
std::cout << "Enter the address of person #" << count << ":\n";
std::getline(std::cin,address);
std::cout << "Enter the phone number of person #" << count << ":\n";
std::getline(std::cin,phoneNumStr);
std::stringstream s(phoneNumStr); // no use of cin. Using stringstream to break up line and extract it into phoneNum
s >> phoneNum;
insert(head, last, firstName, lastName, address, phoneNum);
count = count - 1;
}
showList(head);
std::string findName;
std::cout << "What is the last name of the person you would like to find?\n";
std::cin >> findName;
searchFor(last, findName);
return 0;
}

Implement a linked list program with the following functions

i am trying to implement a linked list with add, search, remove and print functions. But i keep getting errors that "search, print and file are not declared in this scope" and ISO C++ forbids comparison between pointer and integer [-fpermissive] error. Any help would be appreciated.
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
struct node
{
public:
char name;
node *next;
};
bool isEmpty(node *head);
char welcomeScreen();
void addInitialElement(node *head, node*last, char name);
void add (node *&head, node *&last, char name);
void serach (node *current);
void remove (node *&head, node *&last);
void printFile(node *current);
bool isEmpty(node *head)
{
if(head == NULL)
return true;
else
return false;
}
char welcomeScreen()
{
char options;
cout <<"Address Book \n";
cout <<"Available Commands \n";
cout <<" add <name>. \n";
cout <<" search <name>. \n";
cout <<" remove <name>. \n";
cout <<" print \n";
cout <<" file <filename>. \n";
cout <<" quit \n";
cin >> options;
return options;
}
void addInitialElement(node *head, node*last, char name)
{
node *temp = new node;
temp->name = name;
temp->next = NULL;
head = temp;
last = temp;
}
void add (node *&head, node *&last, char name)
{
if(isEmpty(head))
addInitialElement(head, last,name);
else
{
node *temp = new node;
temp->name = name;
temp->next = NULL;
last->next = temp;
last = temp;
}
}
void serach (node *current)
{
node *temp = new node;
if (current->name == temp)
{
cout << "The List Contains the Following: \n";
while(current != NULL)
{
cout<< current->name <<endl;
current = current->next;
}
}
}
void remove (node *&head, node *&last)
{
if(isEmpty(head))
cout << "ERROR: Not found for Removal. \n";
else if(head == last)
{
delete head;
head == NULL;
last == NULL;
}
else
{
node *temp = head;
head = head->next;
delete temp;
}
}
void printFile(node *current)
{
if(isEmpty(current))
cout << "NO NAME IN LIST. \n";
else
{
cout << "The List Contains the Following: \n";
while(current != NULL)
{
cout<< current->name <<endl;
current = current->next;
}
}
}
int main()
{
node *head = NULL;
node *last = NULL;
char options;
char name;
do{
options = welcomeScreen();
switch(options)
{
case '1': cout <<"please add a name: ";
cin >> name;
add(head, last, name);
break;
case '2' : search(head, last);
cin >> name;
break;
case '3' : remove(head, last);
break;
case '4' : print(head);
break;
case '5' : file(head);
break;
default: cout << "system exit \n";
}
}while(options != '6');
}
You misspelled search. Do a find / replace on "serach".
In case 4, you may mean to call printFile instead of print
case '4' : printFile(head);
In case 5, I don't know what your intention is here, but there is no function called "file". Try searching on that word, file, and you'll see.

Displaying multiple values linked list

I'm new to C++ and currently experimenting about linked lists and I'm having trouble with displaying the multiple values in my program. I know the problem is somewhere in the pointers(DisplayAll function) but I'm not sure how to solve it.
node* InfoBook::AddNode(nodePtr temp)
{
string firstname;
string lastname;
string phonenumber;
string dayofbirth;
string monthofbirth;
string yearofbirth;
string age;
string streetname;
string city;
string state;
string zipcode;
InfoBook ad;
if(head != NULL)
{
current = head;
while(current -> next != NULL)
{
current = current -> next;
}
current -> next = new node;
current -> firstname = temp -> firstname;
current -> lastname = temp -> lastname;
////code here to add the other values////
current -> zipcode = temp -> zipcode;
current -> next -> next = nullptr;
return current;
ad.userPromptStatement();
}
else
{
head = new node;
head -> firstname = temp -> firstname;
head -> lastname = temp -> lastname;
////code here to add the other values////
head -> zipcode = temp -> zipcode;
head -> next = nullptr;
return current;
}
}
////////////////////////////////DisplayAll/////////////////////////////////
void InfoBook::DisplayAll()
{
current = head;
int count = 1;
string firstname;
string lastname;
string phonenumber;
string dayofbirth;
string monthofbirth;
string yearofbirth;
string age;
string streetname;
string city;
string state;
string zipcode;
if(current == nullptr)
{
cout << "\n\n\No Record exists.";
}
while(current != NULL)
{ ////////I know the problem is somewhere between here////////
cout << "Record # " << count << " : ";
cout << current -> firstname << endl;
cout << current -> lastname << endl;
cout << current -> phonenumber << endl;
cout << current -> dayofbirth << endl;
cout << current -> monthofbirth << endl;
cout << current -> yearofbirth << endl;
cout << current -> age << endl;
cout << current -> streetname << endl;
cout << current -> city << endl;
cout << current -> state << endl;
cout << current -> zipcode << endl;
cout <<"\n\n\n";
current = current -> next;
count++;
}
}
///////////////////////////////////////////////
////pointers
InfoBook::InfoBook()
{
head = NULL;
current = NULL;
temp = NULL;
}
////////
class InfoBook
{
private:
nodePtr head;
nodePtr current;
nodePtr temp;
public:
InfoBook();
void userPromptStatement();
node* AddNode(nodePtr);
void DisplayAll();
/////////////
typedef struct node
{
string firstname;
string lastname;
string phonenumber;
string dayofbirth;
string monthofbirth;
string yearofbirth;
string age;
string streetname;
string city;
string state;
string zipcode;
static int count;
node* next;
} *nodePtr;
the program displays only 'Record # :'s but not the values. Any ideas?
i think after
current -> next = new node;
you should add this:
current = current->next;
because you have to assign to the node you've alloc, not the current one.