#include \"LinkedList.h\" #include <iostream> #include <stdlib.h> using namespac
ID: 3716756 • Letter: #
Question
#include "LinkedList.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
//----------------------------------------------------
LinkedList::LinkedList(){
this->head = NULL;
}
LinkedList::~LinkedList(){
cout<<"List destroyed";
}
//---------------------------------------------------------
void LinkedList::appendNode(double item){
struct node *s;
struct node* temp = new (struct node);
temp->data = item;
temp->next=NULL;
s = this->head;
while(s->next != NULL)
s = s->next;
s->next = temp;
}
//-----------------------------------------------------------
void LinkedList::insertAt(double item, int pos){
int counter = 0;
struct node *s, *ptr;
struct node* temp = new (struct node);
temp->data = item;
temp->next=NULL;
int i;
s = this->head;
while(s!= NULL)
{
s = s->next;
counter++;
}
if(pos == 1)
{
if(this->head == NULL)
{
this->head = temp;
this->head->next = NULL;
}
else
{
ptr = this->head;
this->head = temp;
this->head->next = ptr;
}
}
else if(pos>1 && pos<= counter)
{
s = this->head;
for (i = 1; i<pos;i++)
{
ptr = s;
s= s->next;
}
ptr->next = temp;
temp->next = s;
}
else
{
cout<<"out of range";
}
}
//---------------------------------------------
void LinkedList :: removeFrom(int pos)
{
int i,counter = 0;
if(this->head == NULL)
return;
struct node *s,*ptr;
s=this->head;
if(pos == 1)
this->head = s->next;
else
{
while(s!=NULL)
{ s = s->next;
counter++;
}
if(pos>0 && pos<=counter)
{
s= this->head;
for(i=1;i<pos;i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
cout<<"position out of range";
free (s);
}
}
//--------------------------------------------------------------
int LinkedList::locate(double value){
int pos = 0;
bool flag = false;
if (this->head == NULL)
{
cout<<"List is empty"<<endl;
return 0;
}
struct node *s;
s = this->head;
while (s != NULL)
{
pos++;
if (s->data == value)
{
flag = true;
return(pos);
}
s = s->next;
}
if (!flag)
return (-1);
}
//---------------------------------------------------------------
void LinkedList::reverseList(){
struct node *ptr1, *ptr2, *ptr3;
if (this->head == NULL)
{
cout<<"List is empty"<<endl;
return;
}
if (this->head->next == NULL)
{
return;
}
ptr1 = start;
ptr2 = ptr1->next;
ptr3 = ptr2->next;
ptr1->next = NULL;
ptr2->next = ptr1;
while (ptr3 != NULL)
{
ptr1 = ptr2;
ptr2 = ptr3;
ptr3 = ptr3->next;
ptr2->next = ptr1;
}
this->head = ptr2;
}
//-----------------------------------------------------------------------
void LinkedList::displayList(){
struct node *temp;
if (this->head == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = this->head;
cout<<"Elements of list are: "<<endl;
while (temp != NULL)
{
cout<<temp->data<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
// **********************************************************************
This is giving a segmenation fault. The debugger says the problem is in void LinkedList::appendNode(double item). Was just wondering why exactly this is giving a segmentation fault and how it can by fixed.
Header file:
#ifndef LinkedList_H
#define LinkedList_H
class LinkedList
{
public:
struct node
{
double data;
struct node *next;
}*start;
node* head;
LinkedList();
~LinkedList();
void appendNode(double);
void insertAt(double,int);
void removeFrom(int);
int locate(double);
void displayList();
void reverseList();
};
#endif
main file
#include "LinkedList.h"
#include <iostream>
using namespace std;
int main()
{
LinkedList example;
example.appendNode(2);
example.appendNode(4);
example.appendNode(6);
example.insertAt(8, 0);
example.displayList();
example.reverseList();
example.displayList();
example.removeFrom(99);
example.insertAt(10, -1);
cout << "calling locate …" << example.locate(5) << endl;
example.removeFrom(1);
example.insertAt(7, 1);
example.displayList();
return 0;
}
Explanation / Answer
// File Name LinkedList.h
#ifndef LinkedList_H
#define LinkedList_H
// Class LinkedList definition
class LinkedList
{
public:
// Defines a structure for node
struct node
{
// To store data
double data;
// Pointer to next node
struct node *next;
}*start;// Head pointer
// Creates a head node
node* head;
// Prototype of member functions
LinkedList();
~LinkedList();
void appendNode(double);
void insertAt(double,int);
void removeFrom(int);
int locate(double);
void displayList();
void reverseList();
};// End of class
#endif
----------------------------------------------------------------
// File Name LinkedList.cpp
#include "LinkedList.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
//----------------------------------------------------
// Defines constructor
LinkedList::LinkedList()
{
this->head = NULL;
}// End of constructor
// Defines destructor
LinkedList::~LinkedList()
{
cout<<" List destroyed";
}// End of destructor
//---------------------------------------------------------
// Function to add a node at the end
void LinkedList::appendNode(double item)
{
// Declares a temporary node
struct node *s;
// Creates a new node to insert
struct node* temp = new (struct node);
// Assigns data and next pointing to null
temp->data = item;
temp->next=NULL;
// Temporary node points to head
s = head;
// Checks if s is NULL then it is the first node
if(s == NULL)
{
// New node node next points to head
temp -> next = head;
// head points to new node
head = temp;
}// End of if condition
// Otherwise
else
{
// Loops till end
while(s->next != NULL)
// Move next node
s = s->next;
// Temporary node next points to new node
s->next = temp;
}// End of else
}// End of function
//-----------------------------------------------------------
// Function to add a node at specified position
void LinkedList::insertAt(double item, int pos)
{
// Declares a temporary node
struct node *ptr;
// Creates a new node to insert
struct node* temp = new (struct node);
// Assigns data and next pointing to null
temp->data = item;
temp->next=NULL;
// Loop variable
int c;
// Temporary node points to head
ptr = head;
// Checks if s is NULL then it is the first node
if(ptr == NULL)
{
// New node node next points to head
temp->next = head;
// head points to new node
head = temp;
}// End of if condition
// Otherwise
else
{
// Loops up to entered position
for(c = 0, ptr = head; c < pos - 1; c++)
{
// Move to next node
ptr = ptr->next;
// Checks if ptr is null node not found
if(ptr == NULL)
{
cout<<" Position not found. ";
return;
}// End of if condition
}// End of for loop
// New node next is pointing to previous node next
temp->next = ptr->next;
// Previous node next pointing to new node
ptr->next = temp;
}// End of else
}// End of function
//---------------------------------------------
// Function to delete a node from specified position
void LinkedList :: removeFrom(int pos)
{
// Loops variable
int i;
// For number of nodes
int counter = 0;
// Checks if head is null then there is no node to delete
if(head == NULL)
return;
// Declares a two temporary node s for current node ptr for previous node
struct node *s, *ptr;
// temporary node s points to head
s = head;
// Checks if position is one
if(pos == 1)
// head is pointing to temporary node s next
head = s->next;
// Otherwise
else
{
// Loops till end to count how many nodes are available
while(s!=NULL)
{
// Move to next node
s = s->next;
// Increase the counter by one
counter++;
}// End of if condition
// Checks if position is greater than 0 and less than or equals to number of nodes in linked list
if(pos > 0 && pos <= counter)
{
// Temporary node s points to head
s = head;
// Loops till position given as parameter
for(i = 1; i < pos; i++)
{
// Temporary pointer ptr points to s to keep track of previous node
ptr = s;
// Move to next node
s = s->next;
}// End of for loop
// Temporary node ptr next is points to temporary node s next
ptr->next = s->next;
}// End of if condition
// Other display node not found
else
cout<<"position out of range";
// Release the memory
free (s);
}// End of else
}// End of function
//--------------------------------------------------------------
// Function to search a node
int LinkedList::locate(double value)
{
// Initializes the position to 0
int pos = 0;
// Initializes the flag to false for not found
bool flag = false;
// Checks if head is null then empty list
if (head == NULL)
{
cout<<"List is empty"<<endl;
return 0;
}// End of if condition
// Declares a temporary node
struct node *s;
// Temporary not s points to head
s = head;
// Loops till end of the list
while (s != NULL)
{
// Increase the found position by one
pos++;
// Checks if the temporary node data is equals to the parameter data
if (s->data == value)
{
// Set the flag value to true for found
flag = true;
// Return the found position
return(pos);
}// End of if condition
// Move to next node
s = s->next;
}// End of while loop
// Checks if flag value is not true return -1
if (!flag)
return (-1);
}// End of function
//---------------------------------------------------------------
// Function to reverse a linked list
void LinkedList::reverseList()
{
// Declares a temporary node for current and previous
struct node* prev = NULL;
// Current node points to head
struct node* current = head;
// Next node is null
struct node* next = NULL;
// Loops till end of the list
while (current != NULL)
{
// Stores next position node address
next = current->next;
// Current position points to previous
current->next = prev;
// Previous points to current
prev = current;
// Current points to next
current = next;
}// End of while loop
// Head is pointing to previous node
head = prev;
}// End of function
//-----------------------------------------------------------------------
// Function to display the linked list
void LinkedList::displayList()
{
// Declares a temporary node
struct node *temp;
// Checks if head is null then empty list
if (head == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}// End of if condition
// Temporary node points to head
temp = head;
cout<<"Elements of list are: "<<endl;
// Loops end of the linked list
while (temp != NULL)
{
// Displays the data
cout<<temp->data<<"-> ";
// Move to next node
temp = temp->next;
}// End of while loop
cout<<"NULL"<<endl;
}// End of function
----------------------------------------------------------
// File Name LinkedListMain.cpp
#include "LinkedList.cpp"
#include <iostream>
using namespace std;
// main function definition
int main()
{
// Creates a linked list
LinkedList example;
// Calls the function to appends the nodes
example.appendNode(2);
example.appendNode(4);
example.appendNode(6);
example.appendNode(12);
cout<<" After append (2, 4, 3, 12) ";
// Calls the function to display list
example.displayList();
cout<<endl;
// Calls the function to insert a node at position
example.insertAt(8, 2);
cout<<" After insert at 8 at 2 position ";
// Calls the function to display list
example.displayList();
cout<<endl;
// Calls the function to reverse the linked list
example.reverseList();
cout<<" After reverse linked list ";
// Calls the function to display list
example.displayList();
cout<<" After delete 99 ";
// Calls the function to remove a node
example.removeFrom(99);
cout<<" After insert 10 at -1 index position ";
// Calls the function to insert a node at position
example.insertAt(10, -1);
// Calls the function to search a node
cout << "calling locate -> " << example.locate(5) << endl;
// Calls the function to remove a node
example.removeFrom(1);
cout<<" After delete 1 ";
// Calls the function to insert a node at position
example.insertAt(7, 1);
cout<<" After insert 7 at 1 index position ";
// Calls the function to display list
example.displayList();
return 0;
}// End of function
Sample Output:
After append (2, 4, 3, 12)
Elements of list are:
2-> 4-> 6-> 12-> NULL
After insert at 8 at 2 position
Elements of list are:
2-> 4-> 8-> 6-> 12-> NULL
After reverse linked list
Elements of list are:
12-> 6-> 8-> 4-> 2-> NULL
After delete 99
position out of range
After insert 10 at -1 index position
calling locate -> -1
After delete 1
After insert 7 at 1 index position
Elements of list are:
10-> 7-> 6-> 8-> 4-> 2-> NULL
List destroyed
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.