Linked List Implementation using C++ Program

// Linked List Implementation using C++ Program
//#include <stdio.h>
#include <iostream>
using namespace std;
#include <bits/stdc++.h>
struct Node{
int num;
Node *next;
};
struct Node *head=NULL;
void insertNode(int n){
struct Node *newNode=new Node;
newNode->num=n;
newNode->next=head;
head=newNode;
}
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;
}
void deleteItem(){
if(head==NULL){
cout<<“List is empty!”<<endl;
return;
}
cout<<head->num<<” is removed.”<<endl;
head=head->next;
}
int main(){
system(“color 18”);
int ele,choice;
while(1){
cout<<“Choice(1:Ins,2:Disp,3:Del,0:Exit):”;
cin>>choice;
switch(choice){
case 0: exit(1); break;
case 1:
cout<<“Enter an element to insert:”;
cin>>ele;
insertNode(ele); break;
case 2: display(); break;
case 3: deleteItem(); break;
default:
cout<<“\nInvalid choice\n”;
break;
}
}
return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>