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.
Related
I am designing a Linked List where I can Insert and delete item from the list. But there is an issue inserting item at the end of linked list.
I thought my logic was fine for inserting an item at the end of linked list. But I am getting this (segmentation fault : core dumped) error when I am trying to insert item that should be inserted at the end of Linked List. Can Anyone help me with this issue
#include <iostream>
using namespace std;
struct list
{
int data;
list *next,*back;
};
typedef struct list node;
node *start = NULL;
void display()
{
node *temp;
temp = start;
while (temp!=NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void insertion(int item)
{
node *temp,*temp2;
if(start == NULL)
{
start = new node();
start->data = item;
start->next = NULL;
}
else if(item <= start->data)
{
temp = new node();
temp->data = item;
temp->next = start;
start = temp;
}
else // this the logic for inserting middle and end of linked list
{
temp = start;
while(item >= temp->next->data && temp->next != NULL)
{
temp = temp->next;
}
temp2 = new node();
temp2->data = item;
temp2->next = temp->next;
temp->next = temp2;
}
}
void deletion()
{
}
void showMenu()
{
cout << "0.Exit\n1.ShowMenu\n2.Insert\n3.Delete\n4.Display" << endl;
}
int main()
{
showMenu();
int choice;
cout <<"Enter your Choice " << endl;
cin >> choice;
while(choice != 0)
{
if(choice == 1)
{
showMenu();
}
else if(choice == 2)
{
int item;
cout << "Enter your item " << endl;
cin >> item;
insertion(item);
}
else if(choice == 3)
{
deletion();
}
else if(choice == 4)
{
display();
}
else
{
choice = 0;
}
cout <<"Enter your Choice " << endl;
cin >> choice;
}
return 0;
}
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.
I had a circular link list which has add, delete, printlist and search functions etc. All the functions works correctly but when I delete all nodes in the list and than try to add new nodes to the same list, I get project.exe has triggered a breakpoint message.
List.h
#ifndef LIST_H
#define LIST_H
using namespace std;
class list {
public:
struct node {
string data, assignee, date;
node *next;
};
typedef struct node NodePtr;
NodePtr *head, *current, *temp, *dummy;
list();
void add();
void del();
void printList();
void menu();
void exit();
void search();
};
#endif
Source.cpp
#include <cstdlib>
#include <iostream>
#include "List.h"
#include<string>
using namespace std;
list::list() { //constructor
head = NULL;
current = NULL;
temp = NULL;
}
void list::add(){
string addTask, addAssignee, addDate;
NodePtr *newNode = new NodePtr;
//entering the data of the tasks
cout << "Enter the name of the task:\n"<<"<<";
getline(cin, addTask); // compailer doesnt detect the first getline function, ı dont understan why. ı had to put it twice
getline(cin, addTask);
cout << "Enter the assignee name for the task:\n" << "<<";
getline(cin, addAssignee);
cout << "Enter the deadline for the task:\n" << "<<";
getline(cin, addDate);
cout << "\n";
//assignt data to the new node
newNode->data = addTask;
newNode->assignee = addAssignee;
newNode->date = addDate;
newNode->next = NULL;
if (head != NULL){
current = head;
while (current->next != head){ //finding the last node
current = current->next;
}
current->next = newNode;
dummy = newNode;
dummy->next = head; // we make the list, circular
}
else //here will work if the list is empty
{
head = newNode;
dummy = newNode;
head->next=head;
}
}
void list::printList(){
int i = 1;
current = head;
if (head == NULL){
cout << "List is empty\n\n";
}
else
{
do
{
cout << i << "-";
cout << current->data << endl;
cout << current->assignee << endl;
cout << current->date << endl << endl;
current = current->next;
i++;
} while (current != dummy->next);
}
}
void list::del(){ //tüm liste silindikten sonra tekrar işlem(ekleme, listeleme etc.) yapılamıyor hata veriyor
NodePtr *delPtr;
delPtr = NULL;
int ID;
cout << "These tasks exist in the list:\n";
printList();
cout << "Choose the ID of the task you want to remove:\n";
cin >> ID;
if (head == NULL){
cout << "List does not includes any task.\n";
}
else
if (ID == 1){
delPtr = head;
head = head->next;
delete delPtr;
dummy->next = head;
}
else
{
current = head;
for (int k = 1; k <= ID - 1; k++){
temp = current;
current = current->next;
}
if (current == dummy){
dummy = temp;
dummy->next = head;
}
delPtr = current;
current = current->next;
temp->next = current;
delete delPtr;
}
}
void list::menu(){
cout << "Choose an operation\n"
<< "A: Add Task\n"
<< "S: Search for Task\n"
<< "L: List All Tasks\n"
<< "R: Remove Task\n"
<< "E: Exit\n\n"<<"<<";
}
void list::search(){
string searchTask;
NodePtr *search;
search = head;
cout << "To search for a task, enter its task name or assignee name:\n" << "<<";
getline(cin, searchTask);
getline(cin, searchTask);
while (search != dummy){//bu satır problemli gibi gözüküyor
if (search->data == searchTask || search->assignee == searchTask){
cout << search->data << endl
<< search->assignee << endl
<< search->date << endl<<endl;
}
search = search->next;
}
}
void list::exit(){
node *delPtr;
current = head;
if (head != NULL){
do{
temp = current;
delPtr = temp;
current = current->next;
delete delPtr;
} while (current != dummy);
}
}
Main.cpp
#include <cstdlib>
#include <iostream>
#include "List.h"
#include<string>
using namespace std;
int main(){
char operation;
list tasks;
tasks.menu();
cin >> operation;
while (operation!='E' && operation!='e'){
switch (operation)
{
case 'A':case'a':
tasks.add();
break;
case 'S': case's':
tasks.search();
break;
case 'L': case 'l':
cout << "All tasks are listed below:\n";
tasks.printList();
break;
case 'R': case 'r':
tasks.del();
break;
default:
break;
}
tasks.menu();
cin >> operation;
}
tasks.exit();
return 0;
}
I am trying to figure out how to get my link list to show the numerical variables in numerical order. I know I need to set another void but I am lost to set it in numerical order. Here is my source code:
#include <iostream>
struct node
{
int number;
node *next;
};
bool isEmpty(node *head);
char menu();
void insertAsFirstElement(node *&head, node *&last, int number);
void insert(node *&head, node *&last, int number);
void remove(node *&head, node *&last);
void showList(node *current);
void showLogic(node *current);
bool isEmpty(node *head)
{
if (head == NULL)
return true;
else
return false;
}
char menu()
{
char choice;
cout << "Menu\n";
cout << "1. Add an item.\n";
cout << "2. Remove an item.\n";
cout << "3. Show the list Physically.\n";
cout << "4. Show the list Logically.\n";
cout << "5. Exit.\n";
cin >> choice;
return choice;
}
void insertAsFirstElement(node *&head, node *&last, int number)
{
node *temp = new node;
temp->number = number;
temp->next = NULL;
head = temp;
last = temp;
}
void insert(node *&head, node *&last, int number)
{
if (isEmpty(head))
insertAsFirstElement(head, last, number);
else
{
node *temp = new node;
temp->number = number;
temp->next = NULL;
last->next = temp;
last = temp;
}
}
void remove(node *&head, node *&last)
{
if (isEmpty(head))
cout << "The list is already empty.\n";
else if (head == last)
{
delete head;
head == NULL;
last == NULL;
}
else
{
node *temp = head;
head = head->next;
delete temp;
}
}
void showList(node *current)
{
if (isEmpty(current))
cout << "The list is empty\n";
else
{
cout << "The list contains: \n";
while(current != NULL)
{
cout << current->number << endl;
current = current->next;
}
}
}
void showLogic(node *current)
{
}
int main()
{
node *head = NULL;
node *last = NULL;
char choice;
int number;
do
{
choice = menu();
switch(choice)
{
case '1': cout << "Please enter a number: ";
cin >> number;
insert (head,last,number);
break;
case '2': remove(head,last);
break;
case '3': showList(head);
break;
case '4':
break;
default: cout << "System Exit\n";
}
}
while(choice != '5');
system ("pause");
return 0;
}
One solution is to sort the linked list. The easiest way to do that is to take the values in the list, store them in a container, and sort the container. Since you're not allowed to change the order of the nodes (keep the physical order), maybe this is the best bet as far as showing a sorted list.
First, traverse the list from head to tail -- your showList() function does much of this work already. However, instead of showing each node, place the data for that node in a container (a dynamic array, vector, or a simple array). If it's a simple array, make sure you have room for all the elements, and keep track of the number of elements.
Then you take the container, and use any number of sorting algorithms to sort -- the simplest is the bubble sort. Then you display the sorted container to the user.
So to sum up, you are actually not changing the linked list, or even using the linked list to a great extent. All you're doing is gathering up all of the data in each node, placing it in an container, and then all the real sorting work shifts from the linked list to the container.
I am trying to add an element to the back of a linked list.
I am able to add the element and everything works fine on the first try but when i try to add another element, the previously added element becomes rubbish value.
The problem is solved when i replace the LinkedList::process_example(int choice,LinkedList &set) function in the main menu with exactly the same code in my function declaration. Can someone explain to me why????
#include <iostream>
#include <ctime>
using namespace std;
struct Node;
typedef void* VoidPtr;
typedef Node* NodePtr;
typedef char* ZodiacSign;
const int MAX=12;
struct Node
{
NodePtr next;
VoidPtr data;
};
class LinkedList
{
public:
LinkedList();
//~LinkedList();
void Addelement(VoidPtr);
void printSet();
int compareEqual(VoidPtr,VoidPtr);
void swap(int num,int x,ZodiacSign tempSign [MAX]);
void process_example(int choice);
int check_cardinality();
void Addelementfromback(VoidPtr);
private:
NodePtr head;
ZodiacSign getVP(VoidPtr);
};
int choice=1;
LinkedList set;
do {
cout<<endl
<<endl;
cout<<"Wish to try the following operation?"
<<endl
<<"1. Add an element to set"// the function to add to back of linked list
<<endl
<<"2. Check an element in set"
<<endl
<<"3. check carinality"
<<endl
<<"9. Quit"
<<endl
<<endl;
cout<<"Your choice : ";
cin>>choice;
cin.clear();
cin.ignore(200,'\n');
set.process_example(choice);
} while (choice !=9);
void LinkedList::process_example(int choice)
{
switch (choice)
{
case 1:
cout<<endl
<<endl
<<"Current S = ";
this->printSet();
cout<<"Enter an element :";
char element [30];
cin>>element;
cin.clear();
cin.ignore(200,'\n');
this->Addelementfromback(element);
cout<<endl
<<endl
<<"Current S = ";
this->printSet();
break;
case 3:
cout<<endl
<<endl;
cout<<"Current Set S = ";
set.printSet();
cout<<endl
<<"S has ";
int count=this->check_cardinality();
cout<<count
<<" elements";
}
}
void LinkedList::printSet()
{
NodePtr temp = head;
cout<<"{ ";
while (temp != NULL)
{
cout << getVP (temp -> data) << " , ";
temp = temp -> next;
}
cout<<" } ";
cout << endl;
}
void LinkedList::Addelementfromback(VoidPtr horoscope)
{
NodePtr temp = head;
while (temp->next != NULL)
{
temp=temp->next;
}
NodePtr element = new Node;
element->data=horoscope;
element->next=NULL;
temp->next=element;
}
As WhozCraig already mentioned you need to add the following lines to the constructor
Head = NULL;
and then you can add the something like this to the beginning of function Addelementfromback
If(Head == NULL)
{
Head = new Node;
Head->data = horoscope;
Head->next = NULL;
return;
}
you also need to change the following line in LinkedList::process_example
char elements[30];
to
char* elements = new char[30];