Skip to content
This repository has been archived by the owner on Oct 7, 2019. It is now read-only.

Commit

Permalink
added files
Browse files Browse the repository at this point in the history
  • Loading branch information
Anshika-G committed Oct 2, 2018
1 parent 188b068 commit e883605
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions data_structures/linked_lists/linked_list_implementation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <iostream>

using namespace std;

//Declare Node
struct Node{
int num;
Node *next;
};

//Declare starting (Head) node
struct Node *head=NULL;

//Insert node at start
void insertNode(int n){
struct Node *newNode=new Node;
newNode->num=n;
newNode->next=head;
head=newNode;
}

//Traverse/ display all nodes (print items)
void display(){
if(head==NULL){
cout<<"List is empty!"<<endl;
return;
}
struct Node *temp=head;
while(temp!=NULL){
cout<<temp->num<<" ";
temp=temp->next;
}
cout<<endl;
}

//delete node from start
void deleteItem(){
if(head==NULL){
cout<<"List is empty!"<<endl;
return;
}
cout<<head->num<<" is removed."<<endl;
head=head->next;
}
int main(){

display();
insertNode(10);
insertNode(20);
insertNode(30);
insertNode(40);
insertNode(50);
display();
deleteItem(); deleteItem(); deleteItem(); deleteItem(); deleteItem();
deleteItem();
display();
return 0;
}

0 comments on commit e883605

Please sign in to comment.