Created
October 31, 2016 18:25
-
-
Save chermehdi/504d1991b427e14cdc81b3969971f69b to your computer and use it in GitHub Desktop.
simple implementation of likedlist
This file contains hidden or 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
// | |
// LinkedList.cpp.cpp | |
// McpcTraining | |
// | |
// Created by mehdi MAC on 23/09/2016. | |
// Copyright © 2016 mehdi MAC. All rights reserved. | |
// | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <list> | |
#include <map> | |
#include <set> | |
#include <deque> | |
#include <stack> | |
#include <bitset> | |
#include <algorithm> | |
#include <functional> | |
#include <numeric> | |
#include <utility> | |
#include <sstream> | |
#include <iostream> | |
#include <iomanip> | |
#include <cstdio> | |
#include <cmath> | |
#include <cstring> | |
#include <cstdlib> | |
#include <ctime> | |
#include <queue> | |
#define MAX 100005 | |
#define INF 9999999 | |
#define in(a,b) ( (b).find(a) != (b).end()) | |
#define clr(a,b) memset((a), (b), sizeof((a))) | |
#define pb push_back | |
#define all(a) a.begin(), a.end() | |
#define mp make_pair | |
/** | |
* | |
* @Author Mehdi Maick | |
*/ | |
using namespace std; | |
class Node{ | |
int data; | |
Node *next; | |
public : | |
Node(){ | |
data = -1; | |
next = NULL; | |
} | |
Node(int data){ | |
this->data = data; | |
next = NULL; | |
} | |
Node(int data, Node *next){ | |
this->data = data; | |
this->next = next; | |
} | |
int getData(){ | |
return this->data; | |
} | |
Node *getNext(){ | |
return next; | |
} | |
void setData(int data){ | |
this->data = data; | |
} | |
void setNext(Node *next){ | |
this->next = next; | |
} | |
}; | |
class LinkedList{ | |
Node *head; | |
int size; | |
public: | |
LinkedList(); | |
void addFront(int data); | |
void addBack(int data); | |
int removeBack(); | |
int removeFront(); | |
void print(); | |
int getSize(); | |
}; | |
int LinkedList::getSize(){ | |
return size; | |
} | |
LinkedList::LinkedList(){ | |
head = NULL; | |
size = 0; | |
} | |
void LinkedList::print(){ | |
Node * parc = head; | |
if(size == 0){ | |
cout << "empty List " << endl; | |
return; | |
} | |
for(int i = 0; i < size; i++){ | |
if(i > 0) | |
cout << "->"; | |
cout << parc->getData(); | |
parc = parc->getNext(); | |
} | |
cout << endl; | |
} | |
void LinkedList::addFront(int data){ | |
if(head == NULL){ | |
head = new Node(data); | |
}else{ | |
Node *n = new Node(data); | |
n->setNext(head); | |
head = n; | |
} | |
size++; | |
} | |
void LinkedList::addBack(int data){ | |
if(head == NULL){ | |
head = new Node(data); | |
}else{ | |
Node *parc = head; | |
for(int i = 0; i < size - 1; i++){ | |
parc = parc->getNext(); | |
} | |
parc->setNext(new Node(data)); | |
} | |
size++; | |
} | |
int LinkedList::removeBack(){ | |
int ret; | |
if(head == NULL){ | |
ret = -1; | |
}else{ | |
Node *parc = head; | |
for(int i = 0; i < size - 1; i++){ | |
parc = parc->getNext(); | |
} | |
ret = parc->getData(); | |
parc->setNext(NULL); | |
} | |
size--; | |
return ret; | |
} | |
int LinkedList::removeFront(){ | |
int ret; | |
if(head == NULL){ | |
ret = -1; | |
}else{ | |
ret = head->getData(); | |
head = head->getNext(); | |
size--; | |
} | |
return ret; | |
} | |
int main(int argc , char* argv[]) { | |
LinkedList list = *new LinkedList(); | |
list.addFront(2); | |
list.print(); | |
int f = list.removeFront(); | |
list.print(); | |
cout << f << endl; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment