-
-
Save delp/8552913 to your computer and use it in GitHub Desktop.
Just trying to get some practice at using Git, reading and fixing other people's code, and refreshing myself at Data Structures. Fixed the segfault and a few errors including:
-assignment in conditional
-new node placed in head instead of in the iterators spot. Added a display function to give a visual representation of the list. I always like h…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstddef> | |
//singly linked list of integers | |
using namespace std; | |
struct node { | |
int data; | |
node *next; | |
}; | |
class linked_list { | |
public: | |
linked_list(); | |
~linked_list(); | |
void add(int value); | |
bool remove(int value); | |
void display(); | |
private: | |
node *head; | |
}; | |
linked_list::linked_list() { | |
head = NULL; | |
} | |
void linked_list::display() | |
{ | |
if(head != NULL) | |
{ | |
node *iter = head; | |
while(iter != NULL) | |
{ | |
cout << iter -> data << " -> "; | |
iter = iter -> next; | |
} | |
cout << endl; | |
} | |
else | |
{ | |
cout << "List is empty" << endl; | |
} | |
} | |
linked_list::~linked_list() { | |
node *iter = head; | |
while(iter!=NULL) | |
{ | |
node *temp = iter; | |
iter = iter->next; | |
delete temp; | |
} | |
} | |
/* | |
* adds the value to the end of the list | |
*/ | |
void linked_list::add(int value) { | |
cout << "add called" << endl; //!** | |
node *temp = new node; | |
temp->data = value; | |
temp->next = NULL; | |
if(head==NULL) | |
{ | |
cout << "head null" << endl; //!** | |
head = temp; | |
} | |
else { | |
cout << "Head not null" << endl; //!** | |
node *iter = head; | |
while(iter->next!=NULL) { | |
iter = iter->next; | |
} | |
iter->next = temp; | |
} | |
cout << "add finished" << endl; //!** | |
display(); | |
} | |
/* | |
* removes the first occurence of the specified value from the list | |
* returns true if the value was removed | |
* returns false otherwise | |
*/ | |
bool linked_list::remove(int value) { | |
cout << "Remove called." << endl; //!** | |
if(head==NULL) { | |
cout << "Linked List empty, can't remove" << endl; //!** | |
display(); | |
return false; | |
} | |
if(head->data == value) { | |
cout << "Head contains value" << endl; | |
node *temp = head; | |
head = head->next; | |
delete temp; | |
display(); | |
return true; | |
} | |
node *iter = head; | |
while(iter->next!=NULL) { | |
if(iter->next->data==value) { | |
node *temp = iter->next; | |
iter->next = iter->next->next; | |
delete temp; | |
display(); | |
return true; | |
} | |
iter = iter->next; | |
} | |
cout << "number not found, can't remove" << endl; //!** | |
display(); | |
return false; | |
} | |
int main() | |
{ | |
linked_list frank; | |
//a simple test | |
frank.add(1); | |
frank.add(2); | |
frank.add(3); | |
frank.add(4); | |
frank.remove(1); | |
frank.remove(3); | |
frank.remove(4); | |
frank.remove(27); | |
frank.remove(2); | |
frank.remove(12); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment