Skip to content

Instantly share code, notes, and snippets.

@mirsahib
Last active September 6, 2017 10:19
Show Gist options
  • Save mirsahib/6326ec9f9173082a9f922e5704ab8d5f to your computer and use it in GitHub Desktop.
Save mirsahib/6326ec9f9173082a9f922e5704ab8d5f to your computer and use it in GitHub Desktop.
/*
*
*Created by: Mir Sahib
*Aug 18, 2017
*8:15:46 PM
*
*/
#include <iostream>
#include "list.h"
using namespace std;
typedef struct node{
int data;
node* next;
}* nodePtr;
List::List(){
head = NULL;
curr = NULL;
temp= NULL;
prev = NULL;
}
//general function
List::node *List::getHead(){
return head;
}
List::node *List::getPrev(){
return prev;
}
void List::setPrev(nodePtr newPrev){
prev = newPrev;
}
void List::setHead(nodePtr newHead){
head = newHead;
}
void List::addNode(int addData){
temp = new node;
temp->data = addData;
temp->next = NULL;
if(head!=NULL){
curr = head;
while(curr->next!=NULL){
curr = curr->next;
}
curr->next = temp;
}else{
head=temp;
}
}
void List::insertNodeToHead(int newData){
temp = new node;
temp->data = newData;
temp->next = head;
head = temp;
}
void List::deleteNthPosition(int pos){
int length = lengthOfTheList();
if(pos>length){
cout<<"Invalid index"<<endl;
}else{
if(pos==1){
temp = head;
head = temp->next;
delete temp;
}else{
curr = head;
for(int i=0;i<pos-2;i++){
curr=curr->next;
}
temp = curr->next;
curr->next = temp->next;
delete temp;
}
}
}
void List::insertToNthPosition(int pos,int newData){
temp = new node;
temp->data = newData;
temp->next = NULL;
if(pos==1){
temp->next=head;
head=temp;
}else{
curr = head;
for(int i=0;i<pos-2;i++){
curr=curr->next;
}
temp->next = curr->next;
curr->next=temp;
}
}
void List::reverseList(){
curr = head;
while(curr!=NULL){
temp = curr->next;
curr->next = prev;
prev = curr;
curr=temp;
}
head = prev;
}
//recursive function
void List::printRecurs(nodePtr newNode){
if(newNode!=NULL){
cout<<newNode->data<<" ";
printRecurs(newNode->next);
}
}
void List::reverseRecurs(nodePtr newNode){
if(newNode==NULL){
newNode = NULL;
}else if(newNode->next==NULL){
head = newNode;
}else{
temp = newNode->next;
newNode->next=NULL;
reverseRecurs(temp);
temp->next=newNode;
temp = newNode;
}
}
//miscellaneous function
int List::lengthOfTheList(){
temp = head;
int length = 0;
while(temp!=NULL){
temp=temp->next;
length++;
}
return length;
}
void List::printNode(){
temp = head;
cout<<"List is: ";
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
int main(){
int limit,num;
List myList;
cout<<"How many number to print"<<endl;
cin>>limit;
for(int i=0;i<limit;i++){
cout<<"Enter Number"<<endl;
cin>>num;
myList.insertNodeToHead(num);
myList.printNode();
}
myList.insertToNthPosition(1,14);
cout<<"Inserting node at nth position"<<endl;
myList.printNode();
cout<<"delete node at nth position"<<endl;
myList.deleteNthPosition(2);
myList.printNode();
cout<<"Reverse List"<<endl;
myList.reverseList();
myList.printNode();
cout<<"Recursive printing"<<endl;
myList.printRecurs(myList.getHead());
cout<<"\nRecursive reverse"<<endl;
myList.reverseRecurs(myList.getHead());
myList.printRecurs(myList.getHead());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment