Delete a specific node in a Linked list using STL - c++

struct node
{
int data;
node* next;
};
class linklist
{
node* head;
public:
linklist(){ head = NULL;}
void Delete_a_Node( ? , ? ); \\ Not getting how to call this function
};
int main()
{
list<int> l1;
linklist ll1;
.
.
.
for (int i = 0; i < no; i++)
{
cin >> element;
l1.push_back(element);
}
cout << "Original list: " << endl;
list<int>::iterator p = l1.begin(); // Printing List elements.
while (p != l1.end()){
cout << *p << " ";
p++;
}
cout << endl << endl;
cout << "which node do you want to delete" << endl;
cin >> m;
ll1.Delete_a_Node(m, p); \\ Getting Error
_getch();
return 0;
}
Here ,I have created a list using list class. Now I want to delete specific node ( 2nd, or 3rd, or anything ) without using list class member functions. But not getting how to call Delete_a_Node() function.
How to pass Head ( in which address of starting node is stored ) node ??
I want to learn STL, so used class list and iterator purposely.

Related

C++ Read Access Violation in Singly Linked List

I'm writing a program about creating a list of country, there seems to no error in my code but when debugging, I get the error: Unhandled exception thrown: Read access violation p was 0xFFFFFFFFFFFFFFFF, after I input some values.
Can anyone give me a hint or find me the mistake?
#include <cstring>
#include <string>
using namespace std;
this is my struct Provice.
struct Province
{
int Code;
string Name;
int Pop;
float Area;
};
struct node
{
struct Province data;
node* next;
};
struct List
{
node* head;
node* tail;
};
void Init(List &l)
{
l.head = NULL;
l.tail = NULL;
}
void add_tail(List& l, node* p)
{
if (l.head == NULL)
{
l.head = p;
l.tail = p;
}
else
{
l.tail->next = p;
l.tail = p;
}
}
I think I got some problem when I create a node here without initializing value, is this right ?
void inputListProvinces(List& l)
{
int n;
cin >> n;
int i = 0;
while(i<n)
{
node* p = new node;
cin >> p->data.Code;
cin.ignore();
getline(cin, p->data.Name);
cin.ignore();
cin >> p->data.Pop;
cin >> p->data.Area;
add_tail(l, p);
i++;
}
}
And the error happens here, but I dont know how to fix.
void outputListProvinces(List& l)
{
node* p = l.head;
while (p != NULL)
{
cout << p->data.Code << '\t'; /*Unhandled exception thrown: read access violation.
p was 0xFFFFFFFFFFFFFFFF*/
cout << p->data.Name << '\t';
cout << p->data.Pop << '\t';
cout << p->data.Area << '\t';
cout << endl;
p = p->next;
}
}
void outputProvince(node* p)
{
cout << p->data.Code << '\t';
cout << p->data.Name << '\t';
cout << p->data.Pop << '\t';
cout << p->data.Area << '\t';
}
void outputProvincesMore1MillionPop(List& l)
{
node* p = l.head;
while (p != NULL)
{
if (p->data.Pop > 1000)
{
outputProvince(p);
cout << endl;
}
p = p->next;
}
}
node* findProMaxArea(List& l)
{
node* n = l.head;
node* p = l.head;
while (p != NULL)
{
if (p->data.Area > n->data.Area)
{
n = p;
}
p = p->next;
}
return n;
}
int main()
{
List L;
Init(L);
inputListProvinces(L);
cout << "List of provinces:" << endl;
cout << "ID\t|Province\t|Population\t|Area" << endl;
outputListProvinces(L);
cout << "Provinces with a population of more than 1 million:" << endl;
outputProvincesMore1MillionPop(L);
cout << "The largest province:" << endl;
node* p = findProMaxArea(L);
if (p) outputProvince(p);
return 0;
}
Mistake is node never initializes its next pointer. You can only count on it being NULL if you set it to NULL, and the last node in the list MUST be NULLor the program can't find the end of the List and marches off into the wacky world of Undefined Behaviour.
Safest Fix: Add a constructor to node to make sure next is always initialized.
struct node
{
struct Province data;
node* next;
node(node* n = NULL): next(n)
{
}
};
There are other fixes, like making sure l.tail->next = NULL; at the end of inputListProvinces, but I don't think it's really worth the reduced overhead given the slow-ness of the console IO.
And if you do that, then you should also roll Init into List as a constructor:
struct List
{
node* head;
node* tail;
List(): head(NULL), tail(NULL)
{
}
};
This should leave you with the problem of a poorly-placed cin.ignore() consuming a character you don't want consumed.
Side note: Replace NULL with nullptr if available to your compiler and target C++ Standard revision. nullptr removes the bugs that can result from NULL being a glorified 0.

I am writing an insert and remove function with switch statement in c++ , all other functions are working except insert,remove, kth item

This is an interactive test program for the List class as provided by the LinkedList.h.
The program responds to commands from the user to exercise commands as described in the menu() function.
The program will read and respond to commands that the user enters to manipulate an ordered list.
INPUT- The program reads commands to manipulate its List. These commands consist of a letter sometimes followed by an integer. For example, the command "i 25" tells the program to insert the value 25 into the List.
OUTPUT-The program writes instructions and a menu of commands to the terminal, it prompts for the user's input, and it outputs lists and elements of lists.
ERRORS-The program may assume that the input the user provides is correct; except that it will report if a position is beyond the end of a list (See the example below). Otherwise, it need not detect any errors.
This program responds to commands the user enters to manipulate an ordered list of integers, which is initially empty. In the following commands, k is a position in the list, and v is an integer.
e -- Re-initialize the list to be empty.
i v -- Insert the value v into the list.
r v -- Remove the value v from the list.
m -- Is the list empty?
l -- Report the length of the list.
p v -- Is the value v present in the list?
k k1 -- Report the k1th value in the list.
w -- Write out the list.
h -- See this menu.
q -- Quit.
header file
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <cstdlib> // Provides size_t
#include <iostream> // Provides ostream
using namespace std;
class List
{
public:
// TYPEDEF
typedef int Item; // What can go in a list
// CONSTRUCTORS
List( ) { first = NULL; } // Inline
List( const List& source ); // Copy constructor
// DESTRUCTOR
~List( );
// MODIFICATION MEMBER FUNCTIONS
void make_empty ( );
void insert ( const Item& entry );
void remove ( const Item& target );
void operator = ( const List& source );
// CONSTANT MEMBER FUNCTIONS
bool isEmptyList( ) const {return first==NULL;} // Inline
int length( ) const;
bool present ( const Item& target ) const;
Item kth (int k ) const;
void quit() const;
// FRIEND FUNCTION for the List class:
friend std::ostream& operator << ( std::ostream& out_s,
const List& l );
private:
// DATA MEMBERS
struct Node
{
Item data;
Node *next;
};
Node *first;
cpp file
#include "LinkedList.h"
#include <cstdlib>
#include <iostream>
#include <cassert>
using namespace std;
// Default constructor is inline.
// Copy constructor
List::List(const List& source)
{
Node* p; // Will traverse the source List.
Node* last; // Will always point to the new List's last Node.
if ( source.first == NULL ) // If the source list is empty ...
first = NULL;
else
{
first = get_node(source.first->data,NULL); // Copy the first Node.
last = first;
p = source.first->next;
while ( p != NULL ) // Copy remaining Nodes.
{
last->next = get_node(p->data,NULL);
last = last->next;
p = p->next;
}
}
}
// Destructor
List::~List( )
{
Node* temp;
while ( first != NULL )
{
temp = first;
first = first -> next;
delete temp;
}
} // end destructor
// Modification member functions
void List::make_empty ( )
{
Node* temp;
while ( first != NULL )
{
temp = first;
first = first -> next;
delete temp;
}
}
void List::insert ( const Item& entry )
{
Node *prev;
assert ( ! present(entry) );
if ( first == NULL || entry < first->data )
first = get_node(entry,first);
else
{
prev = first;
while ( prev->next != NULL && prev->next->data < entry )
prev = prev->next;
prev->next = get_node(entry,prev->next);
}
} // end insert
void List::remove ( const Item& target )
{
Node *temp;
Node *prev;
assert ( present(target) );
prev = first;
if ( prev->data == target )
{
first = first->next;
delete prev;
}
else
{
while ( prev->next != NULL && prev->next->data < target )
prev = prev->next;
temp = prev->next;
prev->next = temp->next;
delete temp;
}
} // end remove
// Constant member functions
int List::length( ) const
{
Node *p;
int k;
k= 0;
for(p = first; p != NULL; p = p->next)
{
++k;
p=p->next ;
}
return k;
} // end length
bool List::present ( const Item& target ) const
{
Node *p = first;
for(p!=NULL && p->data!=target;
p=p->next;) // The loop's body is empty.
{
return ( p != NULL );
}
}
void List::quit() const
{ return; }
List::Item List::kth ( int k ) const
{
Node *p;
int i;
assert ( 1 <= k && k <= length() );
p = first;
for (int i=1; i<k; ++i)
p = p->next;
return p->data;
}
// Friend Function
// This function writes out the list
ostream& operator << ( ostream& out_s, const List& l )
{
List::Node *p;
p = l.first;
while(p != NULL )
{
out_s << p->data << " ";
p = p->next;
}
return out_s; //return output stream.
}
// Private function
List::Node* List::get_node ( const Item& entry, Node* link )
{
Node *temp;
temp = new Node;
temp->data = entry;
temp->next = link;
return temp;
}
// PRIVATE FUNCTION
Node* get_node ( const Item& entry, Node* link );
};
#endif
main.cpp
#include "LinkedList.h"// ADT list operations
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cassert>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
char command; // Each command letter
int v; // The value to be inserted
char e, i, r, m, l, p, w, h, q;
int k; // position in the list
List n; // An ordered list.
void menu()
//Postcondition: A menu of commands and instructions
//for their use has been written to out.
{
cout << endl;
cout << "This program responds to commands the user enters to" << endl;
cout << "manipulate ordered LinkedList which are initially empty." << endl;
cout << "In the following commands, v is any integer, and" << endl;
cout << "integers from 0 to " << endl << endl;
cout << " e -- Re-initialize list n to be empty." << endl;
cout << " i v -- Insert the value v into list n." << endl;
cout << " r v -- Remove the value v from list n." << endl;
cout << " m -- Report is the List n empty." << endl;
cout << " l -- Report the length of the list n." << endl;
cout << " p v -- Is the value present in the list n." << endl;
cout << " k -- Report the kth value in the List n." << endl;
cout << " w -- Write out the contents of LinkedList n." << endl;
cout << " h -- See this menu." << endl;
cout << " q -- Quit the program." << endl << endl;
}
int main()
{
menu(); //show the menu
do
{
cout << "--> "; // Issue a prompt.
cin >> command; // Read a command letter.
switch ( command ) // Carry out the command.
{
case 'e': n.make_empty();
cout << "The list is empty." << endl;
break;
case 'i': cin >> v ;
n.insert(v);
cout << "The new item is inserted into list." << n << endl;
break;
case 'r': cin >> v ;
n.remove(v);
cout << "The new item is removed from the list." << n << endl;
break;
case 'm': if (n.isEmptyList() ==true){
cout << "The list is empty." << endl;
}
else{
cout << "The list is NOT empty." << endl;
}
break;
case 'l': n.length();
cout<< "The length of the list is "<< n.length()<<endl;
break;
case 'p': cin >> v ;
if (n.present(v)==true){
cout << "The value" << v << "is present in the list." << endl;
}
else{
cout << "The value" << v << "is Not present in the list." << endl;
}
break;
case 'k': cin >> v;
cout << "LinkedList " << setw(1) << n << " contains "
<< setw(1) << n.kth(k) << " items." << endl;
cout << "The " << v << "is Not present in the list." << endl;
break;
case 'w': cout << "List:" <<n<< endl;
break;
case 'h': menu();
cout << "The progam menu is shown." << endl;
break;
case 'q': n.quit();
cout << "The program is quiting." << endl;
break;
default: ; // Null statement for an incorrect command
}
} while (command != 'q');
}

C++ Need help accessing values inside of linked list

class Node {
public:
Node();
void setNext(Node*);
private:
void* item;
Node* next;
};
void Node::setNext(Node* n)
{
next = n;
}
class List {
public:
List();
void addFirst(void*);
void reset();
void* getCurItem();
private:
Node* head;
Node* current;
};
void List::addFirst(void* obj)
{
Node *newNode = new Node(obj);
newNode->setNext(head);
head = newNode;
}
void List::reset()
{
current = head;
}
void* List::getCurItem()
{
return current->getItem();
}
Polynomial::Polynomial(std::ifstream& input){
List *polyCo = new List();
List *polyEx = new List();
bool exit = false;
double coefficient=0;
int exponent=0;
while(!exit && input.good()){
input >> coefficient;
if(coefficient != -9999.99){
input >> exponent;
cout << "Exponent before: " << exponent << endl;
cout << "Coefficient before: " << coefficient << endl;
int *ExPtr = &exponent;
double *CoPtr = &coefficient;
cout << "Exponent: " << *(ExPtr) << endl;
cout << "Coefficient: " << *(CoPtr) << endl;
polyCo->addFirst(ExPtr);
polyEx->addFirst(CoPtr);
cout << polyCo->getCurItem() << endl; //SEG FAULT
}
}
polyEx->reset();
polyCo->reset();
}
I am reading numbers from a file into two separate linked lists. I am getting a segmentation fault when attempting to access the value in either of the linked list.
First, I'm going to outline how I think my addFirst() functions works, because I could just be implementing it incorrectly. When addFirst() is called, a new node is created, and the data being read in is set equal to the member of the new node. The existing node in the list is pushed forward, and the new node created becomes the head. The two nodes are also linked.
There could be something else entirely wrong besides my implementation of the function, but at least it is a good place to start, along with the source of the segmentation fault.

C++ Priority Queue Class with Linked List

I am having two issues with my c++ code (The test file is below):
I can't seem to figure out why its not breaking out of the while loop, when running, its stuck on the loop "7 versus 325".
So it should go into the next node of temp, which would be null, and then jump into the section where it adds it to the end of the queue. But its just looping and looping.
My second issue is with the the function I have commented out, queue1.back, whenever that is ran, it just errors out and gives what appears to be the address, but the .front() function works just fine.
The Test File I am working with is like this:
89 Alex
325 Rob
72 Joy
91 Bob
using namespace std;
class Person
{
friend class Pqueue;
public:
int priority;
string name;
};
class PQueue
{
friend class Person;
private:
//Structure for my linked list.
typedef struct node {
Person data;
struct node *next;
}Node, *NodePtr;
Node *head, *tail;
public:
//Prototype Functions
PQueue(void); //Initializer function
bool empty(void); //Test if empty
int size(void); //Return size
void enqueue(Person *); //Insert Node
void dequeue(void); //Remove Node
Person* front(void); //Access Next Node
Person* back(void); //Access last node
};
PQueue::PQueue()
{
head = NULL;
tail = NULL;
}
bool PQueue::empty(){
return (head == NULL);
}
void PQueue::enqueue(Person *myPerson){
NodePtr np = (NodePtr) malloc(sizeof(Node));
np->data = *myPerson;
np->next = NULL;
if(empty())
{
cout << "Making into creating the first node, of the linked list" <<endl;
head = np;
tail = np;
}
else { //Queue has more the one node
Node* temp = head;
if(np->data.priority > temp->data.priority) //If the priority is greater then the rest.
{
head = temp; //Saving my head pointer
head->data = np->data; //Assigning new Data to the head pointer
head->next = temp; //Assigning the rest of the linked list back into head.
cout << "Making into creating the first node again, having to reassign." <<endl;
}
else{
//Searching where to place the node.
while(temp->data.priority > np->data.priority) //Searching if the next priority is higher then the passed.
{
cout << "Inside the while loop: " << np->data.priority << " versus "<<temp->data.priority <<endl;
if(temp->next == NULL)
break;
temp = temp->next;
}
if(temp->next == NULL && np->data.priority < temp->data.priority) //Inserting at the end.
{
cout << "Making into creating the last node" <<endl;
tail->next = np;
cout << "Passing the function of creating the last node" <<endl;
}
else //Inserting into the middle of the function.
{
cout << "Inserting in the middle of the queue" <<endl;
np->next = temp->next;
temp->next = np;
}
}
}
}
void PQueue::dequeue(){
if(empty()){
cout << "\nAttempt to remove from an empty list." << endl;
exit(1);
}
Person hold = head->data;
NodePtr temp = head;
head=head->next;
if (head == NULL) tail = NULL;
free(temp);
}
Person* PQueue::front(){
//Person &temp = head->next->data;
//Person &temp = head->data;
Person &temp = head->data;
return &temp;
}
Person* PQueue::back(){
if(empty()){
cout << "\nNo entries in list." << endl;
exit(1);
}
Person &temp = tail->data;
return &temp;
}
int main() {
cout << "Starting main" << endl;
PQueue queue1; //Creating my queue.
cout << "Created Queue" << endl;
Person tempPerson;
ifstream inFile;
inFile.open("/tmp/temp");
cout << "going into while loop" << endl;
while (inFile >> tempPerson.priority >> tempPerson.name){
cout << "The priority is " << tempPerson.priority << " the name is " << tempPerson.name <<endl;
queue1.enqueue(&tempPerson);
}
//Testing Section, trying to get .front and .back to work.
Person *testPerson;
testPerson = queue1.front();
cout << "The TEST priority is " << testPerson->priority << " the TEST name is " << testPerson->name <<endl;
/**
Person *tailPerson;
testPerson = queue1.back();
cout << "The TEST priority is " << tailPerson->priority << " the TEST name is " << tailPerson->name <<endl;
**/
queue1.dequeue();
queue1.dequeue();
queue1.dequeue();
return 0;
}
When you add a new head entry to a non-empty list, you're mistakenly setting the next pointer to point right back at the node it's in, rather than setting it to point at the rest of the linked list like you intended.
head = temp; //Saving my head pointer
head->next = temp; //Assigning the rest of the linked list back into head.

c++ linked list deallocation error: Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

In LinkedList.h I have the following code:
#ifndef LINK_H
#define LINK_H
template<typename T>
struct Node
{
T data;
Node *link;
};
#endif
In my Main.cpp I have the following code:
int size = 1;
int data = 0;
cout << "How big for linked list? ";
cin >> size;
cout << endl;
Node<int> *head;
Node<int> *node = new Node<int>[size];
head = &node[0];
for (int i = 0; i < size; i++)
{
cout << "Input data for link " << i + 1 << ": ";
cin >> node[i].data;
if (i < size - 1)
node[i].link = &node[i + 1];
else
node[i].link = NULL;
}
Node<int> *ptr = head;
cout << "Linked list has current data: ";
while (ptr)
{
cout << ptr->data << " ";
ptr = ptr->link;
}
cout << endl << endl << "What data do you want to add to the front of the list? ";
cin >> data;
cout << endl;
//add
Node<int> *loc = new Node<int>;
loc->data = data;
loc->link = head;
head = loc;
cout << "Linked list has current data: ";
ptr = head;
while (ptr)
{
cout << ptr->data << " ";
ptr = ptr->link;
}
cout << endl << endl << "Deleting the data you just added." << endl;
//delete single
Node<int> *temp = new Node<int>;
temp = head;
head = head->link;
delete temp;
cout << "Linked list has current data: ";
ptr = head;
while (ptr)
{
cout << ptr->data << " ";
ptr = ptr->link;
}
cout << endl;
//delete entire list
ptr = head;
while (ptr)
{
cout << "/";
Node<int> *old = new Node<int>;
old = ptr;
ptr = ptr->link;
delete old;
}
cout << endl;
Upon attempting to deallocate this linked list, I get an error like the one in the title and my program crashes. It seems like c++ is trying to access memory it no longer has access to, and it has something to do with my loc pointer / adding a new link to the front of the list. Can anyone tell me where my code went wrong? The parts that say code removed are basically just couts to print what's in the linked list.
Edit: The error occurs after 2 iterations through the last while loop.
Edit2: It seems after I call the line:
delete temp;
head loses its link and returns garbage. Why?