This is my first time in building a custom deque(Double Ended Queue) class in c++ .This code is giving me Multiple definition error for head and tail.What should i do? what changes should be made?
// this is the header file(dequeue.h)
#ifndef DEQUEUE_H
#define DEQUEUE_H
#include <iostream>
#include <cstdlib>
using namespace std;
/*
* Node Declaration
*/
struct node
{
int info;
node *next;
node *prev;
}*head, *tail;
class dequeue
{
public:
int top1, top2;
void insert();
void del();
void display();
dequeue()
{
top1 = 0;
top2 = 0;
head = NULL;
tail = NULL;
}
};
#endif // DEQUEUE_H
//This is the dequeue.cpp file
#include "dequeue.h"
#include <iostream>
using namespace std;
/*
* Insert Element in Doubly Ended Queue
*/
void dequeue::insert()
{
struct node *temp;
int ch, value;
if (top1 + top2 >= 50)
{
cout<<"Dequeue Overflow"<<endl;
return;
}
if (top1 + top2 == 0)
{
cout<<"Enter the value to be inserted: ";
cin>>value;
head = new (struct node);
head->info = value;
head->next = NULL;
head->prev = NULL;
tail = head;
top1++;
cout<<"Element Inserted into empty deque"<<endl;
}
else
{
while (1)
{
cout<<endl;
cout<<"1.Insert Element at first"<<endl;
cout<<"2.Insert Element at last"<<endl;
cout<<"3.Exit"<<endl;
cout<<endl;
cout<<"Enter Your Choice: ";
cin>>ch;
cout<<endl;
switch(ch)
{
case 1:
cout<<"Enter the value to be inserted: ";
cin>>value;
temp = new (struct node);
temp->info = value;
temp->next = head;
temp->prev = NULL;
head->prev = temp;
head = temp;
top1++;
break;
case 2:
cout<<"Enter the value to be inserted: ";
cin>>value;
temp = new (struct node);
temp->info = value;
temp->next = NULL;
temp->prev = tail;
tail->next = temp;
tail = temp;
top2++;
break;
case 3:
return;
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
}
}
/*
* Delete Element in Doubly Ended Queue
*/
void dequeue::del()
{
if (top1 + top2 <= 0)
{
cout<<"Deque Underflow"<<endl;
return;
}
int ch;
while (1)
{
cout<<endl;
cout<<"1.Delete Element at first"<<endl;
cout<<"2.Delete Element at last"<<endl;
cout<<"3.Exit"<<endl;
cout<<endl;
cout<<"Enter Your Choice: ";
cin>>ch;
cout<<endl;
switch(ch)
{
case 1:
head = head->next;
head->prev = NULL;
top1--;
break;
case 2:
tail = tail->prev;
tail->next = NULL;
top2--;
break;
case 3:
return;
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
}
/*
* Display Doubly Ended Queue
*/
void dequeue::display()
{
struct node *temp;
int ch;
if (top1 + top2 <= 0)
{
cout<<"Deque Underflow"<<endl;
return;
}
while (1)
{
cout<<endl;
cout<<"1.Display Deque from Beginning"<<endl;
cout<<"2.Display Deque from End"<<endl;
cout<<"3.Exit"<<endl;
cout<<endl;
cout<<"Enter Your Choice: ";
cin>>ch;
cout<<endl;
switch (ch)
{
case 1:
temp = head;
cout<<"Deque from Beginning:"<<endl;
while (temp != NULL)
{
cout<<temp->info<<" ";
temp = temp->next;
}
cout<<endl;
break;
case 2:
cout<<"Deque from End:"<<endl;
temp = tail;
while (temp != NULL)
{
cout<<temp->info<<" ";
temp = temp->prev;
}
temp = tail;
cout<<endl;
break;
case 3:
return;
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
}
//This the main.cpp`#include
#include "dequeue.h"
using namespace std;
int main()
{
int choice;
dequeue dl;
while (1)
{
cout<<"\n-------------"<<endl;
cout<<"Operations on Deque"<<endl;
cout<<"\n-------------"<<endl;
cout<<"1.Insert Element into the Deque"<<endl;
cout<<"2.Delete Element from the Deque"<<endl;
cout<<"3.Traverse the Deque"<<endl;
cout<<"4.Quit"<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
cout<<endl;
switch(choice)
{
case 1:
dl.insert();
break;
case 2:
dl.del();
break;
case 3:
dl.display();
break;
case 4:
exit(1);
break;
default:
cout<<"Wrong Choice"<<endl;
}
}
return 0;
}
`
The head and tail variables will be defined everywhere you include the dequeue.h file.
Did you mean for them to be class members of dequeue?
If you really to share them, move them to the cpp file and declare them extern in the h file.
Related
My program splits the original linked list into halves after I call function binary search function, any way i can keep the original single linked list and calling the function? i did try to create a 2 extra nodes start2 and start1, and tried to copy all of the data of original start into start2 but again it just splits the original linked list.
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
//declaring
struct node{
int info;
struct node* next;
};
node* start= NULL;
node* start1= NULL;
node* start2= NULL;
int count = 0;
void createlist(int value){
node *q,*tmp;
tmp=new(struct node);
tmp->info=value;
tmp->next=NULL;
if(start==NULL) start=tmp; /*if list is empty*/
else /*element inserted at end*/
{
q=start;
while(q->next!=NULL) q=q->next;
q->next=tmp;
}}
int getCount(node* head)
{
// Initialize count
// Initialize current
node* current = head;
while (current != NULL)
{
count++;
current = current->next;
}
return count;
}
int Split( node *start2, node **start1)
{
struct node *slow, *fast;
if(start2->next==NULL) /*only one element*/
return 0;
slow=fast=start2;
while(fast->next!=NULL && fast->next->next!=NULL)
{
slow = slow->next;
fast = fast->next->next;
}
*start1 = slow->next;
slow->next = NULL;
}//End of Split()//
int binarysearch(int number){
start2= start;
getCount(start2);
int midindex;
midindex = count/2;
node* temp;
temp = start2;
int middle= 1;
while(middle != midindex){
temp = temp->next;
middle++;
}
Split(start2, &start1);
if (temp->info == number){
cout<<"The number is at "<<middle <<"index"<<endl;
}
middle++;
if (temp->info<number){
node* temp1 = start1;
while(temp1->info != number){
temp1= temp1->next;
middle++;}
cout<<"The number exist at the "<< middle <<endl;
}
if (temp->info>number){
node* temp2 = start2;
int newcount =1;
while(temp2->info != number){
temp2= temp2->next;
newcount++;
}
cout<<"The number exist at the "<< newcount <<endl;
}
}
void display()
{
struct node *q;
if(start==NULL)
{
cout<<"List is empty"<<endl;
return;
}
q=start;
cout<<"List is:"<<endl;
while(q!=NULL)
{
cout<<q->info<<" ";
q=q->next;
}
cout<<endl;
}
int main(){
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Search the number "<<endl;
cout<<"3) Display "<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
createlist(val);
break;
}
case 2: {
int n;
cout<<"Enter the value you want to be searched"<<endl;
cin>>n;
binarysearch(n);
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}
I've tried compiling in Visual Studio and CodeBlocks and I've received different error messages from both. I've been working at trying to solve this one issue for several hours and would greatly appreciate the help. I'm just trying to write a simple linked list program.
This is my Header file:
#ifndef LINKED_LIST_H_INCLUDED
#define LINKED_LIST_H_INCLUDED
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
/*
* Node Declaration
*/
struct node
{
int info;
struct node *next;
}*start;
/*
* Class Declaration
*/
class linked_list
{
public:
node* create_node(int);
void insert_begin();
void insert_last();
void insert_pos();
void delete_pos();
void delete_begin();
void delete_last();
void display();
linked_list()
{
start = NULL;
}
};
#endif // LINKED_LIST_H_INCLUDED
Here is my implementation file:
#include <iostream>
#include<cstdio>
#include<cstdlib>
#include "linked_list.h"
using namespace std;
/*
* Create Node
*/
node *linked_list::create_node(int value)
{
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->info = value;
temp->next = NULL;
return temp;
}
}
/*
* Display all the elements of the linked list
*/
void linked_list::display()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = start;
cout<<"Elements of list are: "<<endl;
while (temp != NULL)
{
cout<<temp->info<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
/*
* Inserting at the beginning of the list
*/
void linked_list::insert_begin()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *p;
temp = create_node(value);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout<<"Element Inserted at beginning"<<endl;
}
/*
* Inserting Node at the end of the list
*/
void linked_list::insert_last()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s;
temp = create_node(value);
s = start;
while (s->next != NULL)
{
s = s->next;
}
temp->next = NULL;
s->next = temp;
cout<<"Element Inserted at last position"<<endl;
}
/*
* Insertion of node at the specified position
*/
void linked_list::insert_pos()
{
int value, pos, counter = 0;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s, *ptr;
temp = create_node(value);
cout<<"Enter the position at which node to be inserted: ";
cin>>pos;
int i;
s = start;
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos == 1)
{
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
ptr = start;
start = temp;
start->next = ptr;
}
}
else if (pos > 1 && pos <= counter)
{
s = start;
for (i = 1; i < pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else
{
cout<<"Position out of range"<<endl;
}
}
/*
* Deletion element at a given position
*/
void linked_list::delete_pos()
{
int pos, i, counter = 0;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the position of value to be deleted: ";
cin>>pos;
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start = s->next;
}
else
{
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos > 0 && pos <= counter)
{
s = start;
for (i = 1;i < pos;i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
{
cout<<"Position out of range"<<endl;
}
free(s);
cout<<"Element Deleted"<<endl;
}
}
/*
* Deletion of element at the beginning of the list
*/
void linked_list::delete_begin()
{
struct node *temp, *p;
temp = start;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
else
{
p = start;
start = temp;
p=start->next;
delete temp;
}
cout<<"Element deleted at beginning"<<endl;
}
/*
* Deletion of element at the end of the list
*/
void linked_list::delete_last()
{
struct node *p, *s;
s = start;
while (s->next != NULL)
{
p = s;
s = s->next;
}
p->next = NULL;
delete s;
cout<<"Element deleted at last position"<<endl;
}
And Here is my driver file:
#include <iostream>
#include<cstdio>
#include<cstdlib>
#include "linked_list.h"
using namespace std;
main()
{
int choice, nodes, element, position, i;
linked_list linlist;
start = NULL;
while (1)
{
cout<<endl<<"---------------------------------"<<endl;
cout<<endl<<"Linked List Homework Menu"<<endl;
cout<<endl<<"---------------------------------"<<endl;
cout<<"1.Insert Node at the Beginning of the List"<<endl;
cout<<"2.Insert Node at the Last Position in the List"<<endl;
cout<<"3.Insert Node at Specified Position"<<endl;
cout<<"4.Delete Node at Specified Position"<<endl;
cout<<"5.Delete Node at the Last Position in the List"<<endl;
cout<<"6.Delete Node at the Beginning of the List"<<endl;
cout<<"7.Display Linked List"<<endl;
cout<<"8.Exit "<<endl;
cout<<"Please enter a selection : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node at the Beginning: "<<endl;
linlist.insert_begin();
cout<<endl;
break;
case 2:
cout<<"Inserting Node at the Last Position: "<<endl;
linlist.insert_last();
cout<<endl;
break;
case 3:
cout<<"Inserting Node at a Specific Position:"<<endl;
linlist.insert_pos();
cout<<endl;
break;
case 4:
cout<<"Deleting Node at a Specific Position: "<<endl;
linlist.delete_pos();
break;
case 5:
cout<<"Deleting Node at the Last Position: "<<endl;
linlist.delete_last();
break;
case 6:
cout<<"Deleting Node at the Beginning: "<<endl;
linlist.delete_begin();
break;
case 7:
cout<<"Display the linked list"<<endl;
linlist.display();
cout<<endl;
break;
case 8:
cout<<"Exiting Program... Goodbye!"<<endl;
exit(1);
break;
default:
cout<<"Option not viable"<<endl;
}
}
}
The issue is encountered when the compiler gets to line 10 in the driver file. I'll put both error lists from Visual Studio and CodeBlocks below. I'm still grasping C++ so thanks in advance to anyone who decides to help!
Error Log
Other Error Log
The first error:
struct node
{
int info;
struct node *next;
}*start;
You created non-const variable in heaader file. Then this header was included in two source files.
Eventually, you have two variables called 'start' in two object files. That`s why linker throws an error "multiple definition of start".
As was mentioned in the comments, 'start' variable must be moved to linked_list class.
The second error:
main()
should be
int main()
#include <iostream>
#include <cstdlib>
using namespace std;
struct node
{
int data;
struct node* link;
};
struct node* front;
struct node* rear;
void insert()
{
struct node*temp;
temp = (struct node*)malloc(sizeof(struct node));
cin >> temp->data;
if (front == NULL)
{
front = rear = temp;
}
else
{
rear->link = temp;
rear = rear->link;
}
rear->link = front;
}
void del()
{
struct node* temp;
temp = front;
if (front == NULL)
cout << "Underflow";
else
{
front = front->link;
free(temp);
}
rear->link = front;
}
void disp()
{
struct node* temp;
temp = front;
if (front == NULL)
cout << "Empty";
else
{
do
{
cout << temp->data << "->";
temp = temp->link;
} while (temp != front);
}
rear->link = front;
}
int main()
{
int n;
bool run = true;
while (run)
{
cin >> n;
switch (n)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
disp();
break;
case 4:
run = false;
break;
}
}
return 0;
}
I am new to the concept.I wrote a code for insertion deletion and display of elements using queue implementing the concept of linked list..The program is working fine without any errors . But when the output is displayed . I need to display the output along with the first element I inserted..E.g: My input is
1
2
1
3
1
4
3
The output is 2->3->4->
but the output I need is 2->3->4->2->
I want to see the first element again at the last
All you have to do is just adding a single line after the do-while loop as follows:
do
{
cout << temp->data << "->";
temp = temp->link;
} while (temp != front);
cout<< front->data << "->";
assuming front is the head of your linked-list. Now I've a question for you, what you gonna do if there is a single entry? Since it is going to be displayed twice.
Simple enough, change this
do
{
cout<<temp->data<<"->";
temp=temp->link;
}
while(temp!=front);
to this
int first = temp->data;
do
{
cout<<temp->data<<"->";
temp=temp->link;
}
while(temp!=front);
cout<<first<<"->"; // print first element again
I'm struggling with the printing of a binary tree that I coded. It's a little program where you can type in different states and then it orders them alphabetically in the binary tree and afterwards it should print them out again, in alphabetic order.
How should I start with the listing function? I read in the internet that a recursive algorithm would be the best, but I don't really know how to do that with my setup.
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
int insertInt()
{
int x;
cin>>x;
while(cin.fail())
{
cout<<"Error! Please enter a valid integer: ";
cin.clear();
cin.ignore();
cin>>x;
}
return x;
}//closes insertInt
struct node
{
string info;
node* right;
node* left;
}; //closes node
class States
{
private:
node* start;
public:
void insert();
void delete();
void list();
void search();
States();
}; //closes States class
States::States()
{
start = new node;
start -> left = NULL;
start -> right = NULL;
start -> info = " ";
}
void States::insert()
{
string state;
char c;
node *temp, *p, *s;
p = start;
s = start;
temp = new node;
temp ->info = state;
cout<<"Please enter the state you want to add: ";
cin>>state;
if(s -> info == " ")
{
s -> info = state;
cout<<"Added state "<<state<<"to the list.\n";
cout<<"Ready to continue? (enter y)";
cin>>c;
return;
}//close if
else
{
while(true)
{
//moving pointer until next level is empty
if(s->info > temp->info && s->left != NULL)
{
s = s->left;
continue;
}//close if
if(s->info < temp->info && s->right != NULL)
{
s = s->right;
continue;
}//close if
//inserting the new node
if(s->info > temp->info && s-> left == NULL)
{
s -> left = temp;
break;
}//close if
if(s->info < temp->info && s->right == NULL)
{
s->right = temp;
break;
}//cloese if
}//close while loop
}//close else
}//close insert function
void States::list()
{
node *p, *s;
p = start;
s = start;
if(start->info == " ")
cout<<"Nothing to display!\n";
if(s->left == NULL)
cout<<"-"<<s->info<<endl;
}
void States::search()
{
string state;
cout<<"Please enter the state you're looking for: ";
cin>>state;
node *s, *temp;
s = start;
temp = new node;
temp ->info = state;
while(true)
{
if(s->info == state)
{
cout<<"Found your state!\n";
break;
}
if(s->info > temp->info)
{
if(s->left == NULL)
break;
else
s = s->left;
continue;
}//close if
if(s->info < temp->info)
{
if(s->right == NULL)
break;
else
s = s->right;
continue;
}//close if
}//close while loop
}//close search function
int main()
{
States s1;
int c;
int exit = 0;
while(exit == 0)
{
cout<<"----------------------------------------------------------------------\n";
cout<<"\t\tChoose one of the following options\n";
cout<<"\t\t\t\t(1) Add a state\n";
cout<<"\t\t\t\t(2) List states\n";
cout<<"\t\t\t\t(3) Search for state\n";
cout<<"\t\t\t\t(4) Delete state\n";
cout<<"\t\t\t\t(5) Exit\n";
cout<<"----------------------------------------------------------------------\n";
c = insertInt();
switch(c)
{
case 1:
s1.insert();
break;
case 2:
s1.list();
break;
case 3:
s1.search();
break;
case 4:
s1.delete();
break;
case 5:
exit = 1;
cout<<"Exiting...\n";
break;
default:
cout<<"Error. Please enter a valid integer.\n";
}//close switch
}//closes while loop
}//closes main
Say that node is a pointer of type Node* representing some node of your tree. Then inorder(Node*) is defined as follows:
void inorder(Node* node)
{
if (!node) // end the recursion if node == nullptr
return;
inorder(node->left); // display the left subtree
std::cout << node->info << " "; // display the current node
inorder(node->right); // display the right subtree
}
I have a C++ Code that just manipulates basic linked list - add a new node, delete a node and view the list
//UNIX Environment
#include <iostream>
#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
#define clear() printf("\033[H\033[J")
using namespace std;
struct node
{
int val;
node *next;
};
struct node* head = NULL;
void add_node(struct node *hea)
{
char f;
int val;
cout<<"Enter value : ";
cin>>val;
if(head==NULL)
{
head = new(struct node);
head->val = val;
head->next = NULL;
}
else
{
node *traverser = hea;
node *traverser_prev = NULL;
while(traverser!=NULL)
{
traverser_prev = traverser;
traverser=traverser->next;
}
traverser = new(struct node);
traverser->val = val;
traverser->next = NULL;
traverser_prev->next=traverser;
}
cout<<"\nEnter Y to return : ";
cin>>f;
while(f!='Y')
{
cout<<"Wrong entry.Enter again : ";
cin>>f;
}
return;
}
void view_list(struct node *hea)
{
char f;
node *traverser = hea;
while(traverser!=NULL)
{
cout<<traverser->val<<" ";
traverser=traverser->next;
}
cout<<"\nEnter Y to return : ";
cin>>f;
while(f!='Y')
{
cout<<"Wrong entry.Enter again : ";
cin>>f;
}
return;
}
void delete_node(node *hea)
{
char f;
int del_value;
cout<<"Enter value to be deleted :";
cin>>del_value;
node *traverser = hea;
cout<<"OK1\n";
node* traverser_prev = NULL;
cout<<"OK2\n";
while(traverser!=NULL)
{
if(traverser->val==del_value)
{
if(traverser==hea)
{
hea=traverser->next;
cout<<"Here cond1\n";
delete traverser;
}
else
{
traverser_prev->next = traverser->next;
cout<<"Here cond2\n";
}
}
else
{
traverser_prev = traverser;
traverser = traverser->next;
cout<<"Here cond3\n";
}
}
cout<<"\nEnter Y to return : ";
cin>>f;
while(f!='Y')
{
cout<<"Wrong entry.Enter again : ";
cin>>f;
}
return;
}
int main(int argc, char* argv[])
{
while(1)
{
clear();
int choice;
cout<<"POINTER BASICS\n";
cout<<"1. Add new element\n";
cout<<"2. View elements\n";
cout<<"3. Delete element\n";
cout<<"4. Exit\n";
cout<<"Enter choice: ";
cin>>choice;
switch(choice)
{
case 1: add_node(head);
break;
case 2: view_list(head);
break;
case 3: delete_node(head);
break;
default:break;
}
if(choice==4)
break;
}
}
However, whenever I try to delete node, it throws me a segmentation fault error with core dumped. What is the reason for the error?
One problem is that in the delete_node function you pass the list head by value, which means that the pointer is copied and inside the function you only modify the copy.
There are two solutions: Either don't use the argument at all and only use the global variable, or pass the argument by reference:
void delete_node(node*& hea)
Is it possible that when you are executing the line
traverser_prev->next = traverser->next;
traverser_prev == NULL?