Created
October 12, 2017 05:07
-
-
Save mirsahib/65748504e14c0d173763c30f48ee17bc to your computer and use it in GitHub Desktop.
singly linked list
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
#include <iostream> | |
using namespace std; | |
class node{ | |
int data; | |
node* next; | |
} | |
class List{ | |
private: | |
node* head; | |
node* curr; | |
node* temp; | |
public: | |
List(){ | |
head = NULL; | |
curr = NULL; | |
temp = NULL; | |
} | |
void addFirst(int val){{ | |
temp = new node; | |
temp->data = val; | |
temp->next = head; | |
head = temp; | |
} | |
void addLast(int val){ | |
temp = new node; | |
temp->data = val; | |
temp->next = NULL; | |
if(head == NULL){ | |
head = temp; | |
}else{ | |
curr = head; | |
while(curr->next!=NULL){ | |
curr = curr->next; | |
} | |
curr->next = temp; | |
} | |
} | |
void addAt(int index,int val){ | |
temp = new node; | |
temp->data = val; | |
temp->next = NULL; | |
} | |
void print(){ | |
temp = head; | |
while(temp!=NULL){ | |
cout<<temp->data<<" "; | |
temp = temp->next; | |
} | |
} | |
}; | |
int main(){ | |
List myList; | |
int element; | |
for(int i=0;i<5;i++){ | |
cin>>element; | |
myList.addLast(element); | |
} | |
myList.print(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment