Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- class Node{
- public:
- int data;
- Node *next;
- };
- class Linklist{
- public:
- Node *head;
- public:
- Linklist(){
- head = NULL;
- }
- void insert_at_first(int x){
- Node *newNode = new Node();
- newNode->data = x;
- newNode->next = head;
- head = newNode;
- }
- void insertValue(int index,int x){
- Node *temp = head;
- if( index < 0 && temp == NULL)
- {
- cout<<"Insert Can not possible";
- }
- int cont=0;
- while(index > cont && temp)
- {
- temp = temp->next;
- cont++;
- }
- Node *newNode = new Node();
- newNode->data = x;
- newNode->next = temp->next;
- temp->next = newNode;
- }
- void deleteValue( int key){
- Node *temp = head , *prev;
- if( temp != NULL && temp->data == key)
- {
- head = temp->next;
- delete(temp);
- return;
- }
- while( temp != NULL && temp->data != key)
- {
- prev = temp;
- temp = temp->next;
- }
- if(temp == NULL)
- {
- cout<<"Value is not found"<<endl;
- return;
- }
- prev->next = temp->next;
- delete(temp);
- }
- void display(){
- Node *temp = head;
- if(temp == NULL)
- {
- cout<<"Link List is Empty"<<endl;
- return;
- }
- while(temp != NULL)
- {
- cout<<temp->data<<" ";
- temp = temp->next;
- }
- cout<<endl;
- }
- };
- int main()
- {
- Linklist ll;
- cout<<"how many value you want to insert: ";
- int n;
- cin>>n;
- int value;
- cin>>value;
- ll.insert_at_first(value);
- for(int i=0;i<n-1;i++)
- {
- cin>>value;
- ll.insertValue(i,value);
- }
- ll.display();
- cout<<endl<<"which value you want to delete ";
- cin>>value;
- ll.deleteValue(value);
- ll.display();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement