You are on page 1of 11

//Q.

NO 1:SINGLY LINK LIST


#include <iostream>

using namespace std;

struct node

{ int data;

node *next;

};

class list{

node *head;

node *tail;

public:

list()

head=NULL;

tail=NULL;

//insert node code

void insert()

int item;

cout<<"Enter item to add in node\n";

cin>>item;

node *temp=new node;

temp->data=item;

temp->next=NULL;

if(head==NULL)

{
head=temp;

tail=temp;

else{

tail->next=temp;

tail=temp;

// insert at first node

void insertfirst()

int item;

cout<<"enter node->data part to insert at first\n";

cin>>item;

node *temp=new node;

temp->data=item;

temp->next=head;

head=temp;

//insert node at last code

void insertlast()

int item;

cout<<"\nEnter item to add node at Last\n";

cin>>item;

node *temp,*temp1;
temp=new node;

temp1=head;

while(temp1->next!=NULL)

temp1=temp1->next;

temp1->next=temp;

temp->data=item;

temp->next=NULL;

//insert position code is here

void insertposition()

int item,p;

cout<<"Enter position to add node\n";

cin>>p;

cout<<endl;

cout<<"Enter item to add in node\n";

cin>>item;

node *temp=new node;

temp->data=item;

node *cur;

node *pre;

cur=head;

for(int i=1;i<p;i++)

pre=cur;

cur=cur->next;

}
temp->next=cur;

pre->next=temp;

//display nodes code

void display()

node *temp;

temp=head;

if(head!= NULL)

while(temp!=NULL)

cout<<temp->data<<"--->";

temp=temp->next;

//reverse list through iterations

void displaybackward()

node* current = head;

node *prev = NULL, *next = NULL;

while (current!= NULL) {

// Store next

next = current->next;

// Reverse current node's pointer

current->next = prev;
// Move pointers one position ahead.

prev = current;

current = next;

head = prev;

//UPDATE POSITION CODE

void updatenode()

int item,p;

cout<<"Enter position to update node\n";

cin>>p;

cout<<endl;

cout<<"Enter item to update in node\n";

cin>>item;

node *temp;

temp=head;

for(int i=1;i<p;i++)

temp=temp->next;

temp->data=item;

//delete start node code

void deletestart()

{ if(head==NULL)

{
cout<<"list emty\n"; }

else

{ head=head->next; }

//Delete end code

void deletend()

node *cur,*temp;

temp=head;

while(temp->next!=NULL)

cur=temp;

temp=temp->next;

cur->next=NULL;

//delete node at position

void deleteposition()

int p;

cout<<"enter position to delete node\n";

cin>>p;

cout<<endl;

node *cur,*pre;

cur=head;

for(int i=1;i<p;i++)

{pre=cur;

cur=cur->next;
}

pre->next=cur->next;

//FIND NODE CODE

void searchnode()

int item;

cout<<"enter node data to find\n";

cin>>item;

node *temp,*cur;

temp=head;

if(head==NULL)

cout<<"list is empty\n";

else

while(temp!=NULL)

{ if(temp->data==item)

{ cout<<"Node found in List\n";

return;

//cur=temp;

temp=temp->next;

cout<<"Node does not exist in list..\n";

}
}

//sort list code

void sortList() {

struct node *temp = head,

*cur = NULL;

int t;

if(head == NULL) {

cout<<"list is empty"<<endl;

return; }

else {

while(temp != NULL) {

cur = temp->next;

while(cur != NULL) {

if(temp->data > cur->data) {

t = temp->data;

temp->data = cur->data;

cur->data = t; }

cur = cur->next; }

temp = temp->next; }}}

};

int main()

list l;

int choice;

int n;

do{
cout<<"PRESS 1 TO INSERT NODE\nPRESS 2 TO INSERT NODE AT FIRST"<<endl;

cout<<"PRESS 3 TO INSERT NODE AT LAST\nPRESS 4 TO INSERT NODE AT POSITION"<<endl;

cout<<"PRESS 5 TO DELETE NODE FROM FIRST\nPRESS 6 TO DELETE NODE FROM LAST"<<endl;

cout<<"PRESS 7 TO DELETE NODE FROM POSITION \nPRESS 8 TO FIND ANY NODE"<<endl;

cout<<"PRESS 9 TO SORT LIST \n PRESS 10 TO UPDATE NODE "<<endl;

cout<<"PRESS 11 TO DISPLAY FORWARD\nPRESS 12 TO DISPLAY BACKWARD";

cout<<"Enter Your choice\n";

cin>>choice;

switch(choice)

case 1:

cout<<"\nHOW MANY TIMES YOU WANT TO INSERT NODE\n";

cin>>n;

for(int i=1;i<=n;i++)

l.insert();

break;

case 2:

l.insertfirst();

break;

case 3:
l.insertlast();

break;

case 4:

l.insertposition();

break;

case 5:

l.deletestart();

break;

case 6:

l.deletend();

break;

case 7:

l.deleteposition();

break;

case 8:

l.searchnode();

break;
case 9:

l.sortList();

break;

case 10:

l.updatenode();

break;

case 11:

l.display();

break;

case 12:

l.displaybackward();

break;

default:

cout<<"invalid entry"<<endl;

}while(choice!=0);

return 0;

You might also like