Skip to content

Instantly share code, notes, and snippets.

@shade34321
Created March 20, 2013 03:32
Show Gist options
  • Save shade34321/5202104 to your computer and use it in GitHub Desktop.
Save shade34321/5202104 to your computer and use it in GitHub Desktop.
Simple Linked list
//
// linked_list.cpp
// Week_09_Linked_list
//
// Created by Shade Alabsa on 3/16/13.
// Copyright (c) 2013 Shade Alabsa. All rights reserved.
//
#include <string>
#include <iostream>
#include "linked_list.h"
//*****************************************************
//Copy constructor
//Takes in a linked list object and copies it to the new one
//
//*****************************************************
linked_list::linked_list(const linked_list &ll){
node *newNode; //the new node we're allocating
node *nodePtr; //where we are at in ll
node *pos; //where we are at in the curretn list
if(!ll.head){ //if ll isn't pointing to anything do nothing
head = NULL; //head is null end of story
}
else{ //otherwise make a new head node, copy over the value
head = new node;
head-> value = ll.head->value;
nodePtr = ll.head; //so we can keep up with where we are at in the ll list
pos = head; //so we can keep the linked list together with glue, well pointers, same thing both are sticky if used incorrectly
}
while(nodePtr->next){ //while ll has a next
nodePtr = nodePtr->next; //create a new node
newNode = new node; //initialize it
newNode->value = nodePtr->value; //set the value
pos->next = newNode; //lets make sure our list keeps up
}
}
//*****************************************************
// Append method
// Adds a node ot the end of the list, takes in the avlue it will store
//*****************************************************
void linked_list::appendNode(std::string val){
node *newNode; //the new node
node *nodePtr; //where we are at
newNode = new node; //creat, initialize, and set the new node
newNode->value = val;
newNode->next = NULL;
if(!head){ //if we don't have anything in the list
head = newNode; //it's our first node!
}
else{ //otherwise we start at the head
nodePtr = head;
while(nodePtr->next){ //iterate the list while it has a next node
nodePtr = nodePtr->next;
}
nodePtr->next = newNode; //we reached the end of the list, lets attach our node to it
}
}
//*****************************************************
// Insert Node method
// inserts a new node in the desired location. Takes in
// the position and value for the node
//*****************************************************
void linked_list::insertNode(std::string val, int pos){
node *newNode; //new ndoe
node *nodePtr; //node position
newNode = new node; //set and initializes the new node and it's value
newNode->value = val;
newNode->next = NULL;
if(!head){ //do we even have a list yet?
head = newNode; //if not now we do
return;
}
if(pos == 0){ //if we're supposed to put it at the beginning
newNode->next = head; //assign the next value as head
head = newNode; //reassign head
return;
}
//otherwise skip 1 and start at the beginning - we skip to 1 beacuase if we're to put it at position 1 then
//we start at position 1
int skip = 1;
nodePtr = head;
while(skip <= pos){ //while skip is less than or equal to the position we are searching for
if(nodePtr->next == NULL || skip == pos){ //if there isn't another node or we reached the psition
node *temp = nodePtr->next; //make a tmep node
nodePtr->next = newNode; //set the previous node's next as the new ndoe
newNode->next = temp; //set the new node's next as the previous's nodes next
return;
}
nodePtr = nodePtr->next; //otherwise we keep chugging through
skip++;
}
}
//*****************************************************
// Delete node method
// Deletes the node at the specified location
//*****************************************************
void linked_list::deleteNode(int pos){
node *nodePtr = head; //where we are at
node *previous = NULL; //previous node
if(!head){ //if we don't ahve a linked list what do we delete?
return; //nothing have a cookie!
}
if(pos == 0){ //if we delete the head node
nodePtr = head->next; //set where we are at as the next node
delete head; //delete head
head = nodePtr; //reset head
}
else{ //otherwise
for(int i = 0;i<pos;i++){ //we cycle thorugh the nodes
previous = nodePtr;
nodePtr = nodePtr->next;
}
previous->next = nodePtr->next; //set previous's next as curren'ts next
delete nodePtr; //delete current
}
}
//*****************************************************
// Display list method so I can make sure I'm doing it right
//
//*****************************************************
void linked_list::displayList() const{
node *nodePtr = head;
while(nodePtr){ //iterates through list and prints it out
std::cout << nodePtr->value << std::endl;
nodePtr = nodePtr->next;
}
}
//*****************************************************
// Destructor
// Blows up the linked list when we are done with it
//*****************************************************
linked_list::~linked_list(){
node *nodePtr = head;
node *next = NULL;
while(nodePtr != NULL){
next = nodePtr->next;
delete nodePtr;
nodePtr = next;
}
}
//
// linked_list.h
// Week_09_Linked_list
//
// Created by Shade Alabsa on 3/16/13.
// Copyright (c) 2013 Shade Alabsa. All rights reserved.
//
#include <string> //we are to use strings
#ifndef linked_list_h
#define linked_list_h
class linked_list{ //class
private:
struct node{
std::string value; //value the node holds
struct node *next; //pointer to point to the next node
};
node *head; //pointer for the head of the list
public:
linked_list(){ //default constructor
head = NULL;
}
linked_list(const linked_list &); //copy constructor
~linked_list(); //destructor
void appendNode(std::string); //appeand to the list
void insertNode(std::string, int); //insert into the list
void deleteNode(int); //delete a specific node
void displayList() const; //display the list
};
#endif
//
// main.cpp
// Week_09_Linked_list
//
// Created by Shade Alabsa on 3/16/13.
// Copyright (c) 2013 Shade Alabsa. All rights reserved.
//
#include <iostream>
#include "linked_list.h"
int main()
{
linked_list *list = new linked_list; //pointer to linked list, wanted to work more with pointers
list->appendNode("Shade"); //adding myself to the list
list->appendNode("Jessica"); //adding the lady to the list
list->appendNode("Charles"); //adding the professor to the list, was running out of names
list->appendNode("Jonathan"); //added a friend
list->displayList(); //check to make sure they are all inserted
std::cout << "--------------------" << std::endl; // so I can tell what's going on
list->insertNode("Mazen", 2); //inserted my brother after the lady and before you
list->displayList(); //check it
std::cout << "--------------------" << std::endl; //so I can see what's going on
list->deleteNode(2); //deleted my brother
list->displayList(); //checked it
linked_list *list_2 = list; //checking the copy constructor
list_2->displayList(); //it works!
delete list; //no memory leaks here, I think?
delete list_2; //see above comment
return 0;
}
Shade
Jessica
Charles
Jonathan
--------------------
Shade
Jessica
Mazen
Charles
Jonathan
--------------------
Shade
Jessica
Charles
Jonathan
Shade
Jessica
Charles
Jonathan
main(2017) malloc: *** error for object 0x7ffebac03920: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment