0% found this document useful (0 votes)
84 views6 pages

Stack Implementation of Link List

This document contains code for implementing three different operations on linked lists: stack, queue, and search. The stack implementation uses a top pointer to add and remove nodes from the head of the list. The queue implementation uses front and rear pointers to add to the rear and remove from the front of the list. The search operation iterates through a linked list to find a target node and check if it exists in the list.

Uploaded by

pakalo papito
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views6 pages

Stack Implementation of Link List

This document contains code for implementing three different operations on linked lists: stack, queue, and search. The stack implementation uses a top pointer to add and remove nodes from the head of the list. The queue implementation uses front and rear pointers to add to the rear and remove from the front of the list. The search operation iterates through a linked list to find a target node and check if it exists in the list.

Uploaded by

pakalo papito
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Stack implementation of link list

#include <iostream>

using namespace std;

struct node{

int data;
node* link;

};

node* top=NULL;

void push(int x){


node* temp=new node();
temp->data=x;
temp->link=top;
top=temp;
}

void pop(){

node* temp;

if(top==NULL){

return;
}
temp=top;
top=top->link;

delete temp;

int main () {

push(56);
push(54);
push(45);

system("pause");

}
QUEUE IMPLEMENTATION OF LINK LIST
#include <iostream>
using namespace std;
struct node{

int data;
node* next;

};

node* front=NULL;
node* rear=NULL;

void enquene(int x){


node* temp=new node();
temp->data=x;
temp->next=NULL;
if(front==NULL&&rear==NULL){
front=rear=temp;
return;

}
rear->next=temp;
rear=temp;

}
void dequeue(){

node* temp=front;
if(front==NULL){
return;
}
if(front==rear){
front=rear=NULL;
}
else{

front =front->next;
}
delete temp;

}
int main () {

enquene(3);
enquene(5);
enquene(4);
dequeue();
system("pause");

}
SEARCH OPERATION OF LINK LIST
#include <iostream>

using namespace std;


struct node
{
int data ;
node* next;
};

node* head;

void insert(int data,int n) {

node * temp1 = new node();

temp1->data=data;

temp1->next=NULL;

if(n==1)
{

temp1->next=head;

head=temp1;

return;
}

node* temp2 = head;

for (int i=0; i<n-2;i++){

temp2=temp2->next;

temp1->next=temp2->next;

temp2->next=temp1;

void search(){
int count=0;
int n;
cout << "Enter element to search";
cin>>n;
if(head==NULL){
cout<<"List is empty"<<endl;

return;
}
else {

node* temp=head;

while(temp!=NULL){

if(temp->data==n){

cout <<"in "<<n<<" list"<<endl;


count++;
}
temp=temp->next;
}
if(count==0){

cout <<"Item not found "<<endl;


}
}

void Print () {

node* temp =head;

while (temp!=NULL) {
cout <<temp->data<<endl;

temp=temp->next;

void Delete(int n) {

node* temp1=head;

if(n==1){
head=temp1->next;

delete temp1;

return;

for(int i=0; i<n-2;i++){


temp1=temp1->next;
node* temp2=temp1->next;
temp1->next=temp2->next;
delete temp2;

int main ()
{
head=NULL;

insert(2,1);
insert(3,2);
insert(4,3);
insert(5,4);

search();
system("pause");
return 0;
}

You might also like