Skip to content

Instantly share code, notes, and snippets.

@zainulhasan
Created August 3, 2015 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zainulhasan/471d347bb316c0756a88 to your computer and use it in GitHub Desktop.
Save zainulhasan/471d347bb316c0756a88 to your computer and use it in GitHub Desktop.
/******************************
sLinkedList.cpp
Auther:Syed Zain Ul hasan
*****************************/
#include <iostream>
using namespace std;
//Node Structure
struct Node{
int data;
Node* next;
};
//Link List Class
class LinkList{
private:
Node* first;
Node* last;
int size;
public:
LinkList():size(0){
first = last = NULL;
};
//Inserts Item at the end
void push_back(int val){
//Make a Temp Node And Fill With Data
Node* tmp = new Node();
tmp->data = val;
tmp->next = NULL;
//If List is empty make temp Node as First
if (first == NULL){
first = tmp;
last = tmp;
}
else {//Insert Node into last
last->next = tmp;
last = tmp;
}
size++;
}
//Inserts the item at the start
void push_front(int val){
//Make a Temp Node And Fill With Data
Node* tmp = new Node();
tmp->data = val;
//If List is empty make temp Node as First
if (first == NULL){
tmp->data = NULL;
first = tmp;
last = tmp;
}
else {//insert First node into temp
tmp->next = first;
first= tmp;
}
size++;
}
//delete item from last
void pop_back(){
if (first == NULL)//means empty
return;
else
{
if (first == last)// only have one Item so
first = last = NULL;
else{
Node* tmp = first;
while (tmp->next != last)//Move one before Last Item
tmp = tmp->next;
delete last;
last = tmp;
}
size--;
}
}
//delete item from start
void pop_front(){
if (first == NULL)//means empty
return;
else{
if (first == last)// only have one Item so
first = last = NULL;
else{
Node* tmp = first->next;//simple first's next will becomes the first Node
delete first;
first = tmp;
}
size--;
}
}
void Remove(int data) {
Node *tmp = first;
if ( tmp == NULL )
return;
if ( tmp->next == NULL ) {
delete tmp;
first = NULL;
}
else {
Node *prev;
do {
if ( tmp->data == data ) break;
prev = tmp;
tmp = tmp->next;
} while ( tmp != NULL );
prev->next=tmp->next;
delete tmp;
}
}
void insert(int data, int pos)
{
Node* prev = new Node();
Node* curr = new Node();
Node* newNode = new Node();
newNode->data = data;
int tempPos = 0; // Traverses through the list
curr = first; // Initialize current to first;
if(first != NULL)
{
while(curr->next != NULL && tempPos != pos)
{
prev = curr;
curr = curr->next;
tempPos++;
}
if(pos==0)
{
push_front(data);
}
else if(curr->next == NULL && pos == tempPos+1)
{
push_back(data);
}
else if(pos > tempPos+1)
cout << " Invalid Position " << endl;
else
{
prev->next = newNode;
newNode->next = curr;
}
}
else
{
first = newNode;
newNode->next=NULL;
}
}
void displayAll(){
Node* tmp = first;
while (tmp!=last){
cout << tmp->data << " ";
tmp = tmp->next;
}
cout <<tmp->data<< endl;//last item
}
int Size(){
return size;
}
bool isEmpty(){
if (first == NULL)
return true;
else
return false;
}
};
int main(){
LinkList* l=new LinkList();
//Insert some Items at the end
l->push_back(5);
l->push_back(6);
l->push_back(7);
l->push_back(8);
//Insert some Items at the start
l->push_front(10);
l->push_front(11);
l->push_front(12);
l->push_front(13);
cout << "Items are : ";
l->displayAll();
//delete an item from last
l->pop_back();
//delete an item from front
l->pop_front();
cout << "Items after deleting from back & front : ";
l->displayAll();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment