Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 170 additions & 0 deletions Linkedlist/linklistumarsalman.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
class list
{
private:
node *head, *tail;
public:
list()
{
head=NULL;
tail=NULL;
}
void createnode(int value)
{
node *temp=new node;
temp->data=value;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
temp=NULL;
}
else
{
tail->next=temp;
tail=temp;
}
}
void display()
{
node *temp=new node;
temp=head;
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->next;
}
}
void insert_start(int value)
{
node *temp=new node;
temp->data=value;
temp->next=head;
head=temp;
}
void insert_position(int pos, int value)
{
node *pre=new node;
node *cur=new node;
node *temp=new node;
cur=head;
for(int i=1;i<pos;i++)
{
pre=cur;
cur=cur->next;
}
temp->data=value;
pre->next=temp;
temp->next=cur;
}
void delete_first()
{
node *temp=new node;
temp=head;
head=head->next;
delete temp;
}
void delete_last()
{
node *current=new node;
node *previous=new node;
current=head;
while(current->next!=NULL)
{
previous=current;
current=current->next;
}
tail=previous;
previous->next=NULL;
delete current;
}
void delete_position(int pos)
{
node *current=new node;
node *previous=new node;
current=head;
for(int i=1;i<pos;i++)
{
previous=current;
current=current->next;
}
previous->next=current->next;
}

void search(int key)
{
int pos=1;
bool found=false;
node *temp = new node;
temp=head;
while(temp!=NULL)
{
if(temp->data==key)
{
cout<<"\nFound at position:"<<" "<<pos;
found=true;
break;
}

temp=temp->next;
pos++;

}
if(found!=true){cout<<"\nElement doesn't exist in LinkedList";}
}
};
int main()
{
list obj;
obj.createnode(25);
obj.createnode(50);
obj.createnode(90);
obj.createnode(40);
cout<<"\n--------------------------------------------------\n";
cout<<"---------------Displaying All nodes---------------";
cout<<"\n--------------------------------------------------\n";
obj.display();
cout<<"\n--------------------------------------------------\n";
cout<<"-----------------Inserting At End-----------------";
cout<<"\n--------------------------------------------------\n";
obj.createnode(55);
obj.display();
cout<<"\n--------------------------------------------------\n";
cout<<"----------------Inserting At Start----------------";
cout<<"\n--------------------------------------------------\n";
obj.insert_start(50);
obj.display();
cout<<"\n--------------------------------------------------\n";
cout<<"-------------Inserting At Particular--------------";
cout<<"\n--------------------------------------------------\n";
obj.insert_position(5,60);
obj.display();
cout<<"\n--------------------------------------------------\n";
cout<<"----------------Deleting At Start-----------------";
cout<<"\n--------------------------------------------------\n";
obj.delete_first();
obj.display();
cout<<"\n--------------------------------------------------\n";
cout<<"-----------------Deleing At End-------------------";
cout<<"\n--------------------------------------------------\n";
obj.delete_last();
obj.display();
cout<<"\n--------------------------------------------------\n";
cout<<"--------------Deleting At Particular--------------";
cout<<"\n--------------------------------------------------\n";
obj.delete_position(4);
obj.display();
cout<<"\n--------------------------------------------------\n";
cout<<"--------------Searching for an element--------------";
cout<<"\n--------------------------------------------------\n";
obj.search(50);
system("pause");
return 0;
}