Tutorials, Free Online Tutorials,It Challengers provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, core java, sql, php, c language etc. for beginners and professionals.

Breaking

Data Structure - Stack Insert ,delete & display Using Linked List || Question of Pune University (MCA)

------------------------------------------ " Data Structure "---------------------------------------

STACK

  • Inserting at Beginning
  • Inserting at Last 
  • Inserting at Between
  • Delete at Beg.
  • Delete at Last
  • Delete at Position
#include<iostream>
using namespace std;
typedef struct N
{
int data;
N *next;
}NODE;
class linklist
{
NODE *head;
int elt;
public:
linklist()
{
head=NULL;
elt=0;
}
linklist(int n)
{
NODE *temp,*last;
int d;
head=NULL;
elt=0;
while(n>0)
{
temp=new NODE;
temp->next=NULL;
cout<<"Enter the Element: ";
cin>>d;
temp->data=d;
if(head==NULL)
{
head=temp;
last=temp;
elt++;
n--;
}
else
{
last->next=temp;
last=temp;
elt++;
n--;
}
}
}
  • Inserting at Beginning
void InsertAtBeg(int data)
{
NODE*temp;
temp=new NODE;
temp->data=data;
temp->next=NULL;
temp->next=head;
head=temp;
elt++;
}
  • Inserting at Last
void InsertAtLast(int data)
{
NODE*temp,*trv;
temp=new NODE;
temp->data=data;
trv=head;
while(trv->next!=NULL)
trv=trv->next;
trv->next=temp;
temp->next=NULL;

}
  • Inserting at Between
void InsertAtbetw()
{ char ch;
int data;
NODE*temp,*trv;
do
{
temp=new NODE();
temp->next=NULL;
cout<<"\nEnter data: ";
cin>>data;
temp->data=data;
if(data<head->data)
{
temp->next=head;
head=temp;
elt++;
}
else
{
trv=head;
while(data<trv->next->data&&trv->next!=NULL)

trv=trv->next;
temp->next=trv->next;
trv->next=temp;
elt++;


}
cout<<"Do you want to enter data: ";
cin>>ch;
}while(ch!='n');

}
  • Delete at Beginning
void deleteAtBeg()
{
NODE*temp;
temp=head;
head=head->next;
cout<<temp->data;
delete temp;
}
  • Delete at Last
void deleteAtLast()
{
NODE*temp,*trv;
trv=head;
while(trv->next->next!=NULL)
trv=trv->next;
temp=trv->next;
trv->next=NULL;
cout<<"\nDelete at last element: ";
cout<<temp->data;
delete temp;

}
  • Delete at Position
void deletepos(int pos)
{
NODE*temp,*trv;
int n;
if(pos<=0||pos>elt)
cout<<"Invalide position";
else
{
if(pos==1)
{
temp=head;
head=head->next;
temp->next=NULL;
delete temp;
}
else if(pos==elt)
{
trv=head;
while(trv->next->next!=NULL)
trv=trv->next;
temp=trv->next;
trv->next=NULL;
delete temp;
}
else
{
n=1;                     
trv=head;
while(trv&&n<pos-1)
{
trv=trv->next;
n++;
}
temp=trv->next;
trv->next=temp->next;
temp->next=NULL;
delete temp;
elt--;
}
}
}
  • Display Data
void display()
{
NODE*temp;
temp=head;
cout<<"\nElement is: "<<endl;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}

};
  • Main function
int main()
{
int p;
linklist L(5);
L.display();

/*cout<<"After Inserting at Begning:"<<endl;
L.InsertAtBeg(10);
L.display();

cout<<"After inserting at Last: "<<endl;
L.InsertAtLast(12);
L.display();

L.InsertAtbetw();
L.display();

cout<<"\nDelete element: ";
L.deleteAtBeg();
L.display();
L.deleteAtLast();
L.display();*/
cout<<"\nEnter position: ";
cin>>p;
L.deletepos(p);
L.display();
cin.get();
cin.get();
return 0;
}
  • Delete at Position Snapshot

No comments:

Post a Comment