Skip to content

Instantly share code, notes, and snippets.

@mirsahib
Created October 12, 2017 05:07
Show Gist options
  • Save mirsahib/65748504e14c0d173763c30f48ee17bc to your computer and use it in GitHub Desktop.
Save mirsahib/65748504e14c0d173763c30f48ee17bc to your computer and use it in GitHub Desktop.
singly linked list
#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