0% found this document useful (0 votes)
27 views10 pages

DSA Lab11

The document describes code for implementing different data structures like linked lists, stacks and queues using C++. It includes functions to insert nodes in a linked list, implement push and pop operations in a stack, and enqueue and dequeue operations in a queue. Main functions test the implementations by performing sample operations on created objects.

Uploaded by

raonetflix1999
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)
27 views10 pages

DSA Lab11

The document describes code for implementing different data structures like linked lists, stacks and queues using C++. It includes functions to insert nodes in a linked list, implement push and pop operations in a stack, and enqueue and dequeue operations in a queue. Main functions test the implementations by performing sample operations on created objects.

Uploaded by

raonetflix1999
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/ 10

Name: Rao Humza

Roll No:02-132212-010

Task 1
#include <iostream>

using namespace std;

class node {

public:

int data;

node* next;

node(int value){

data=value;

next=NULL;

};

void inserttail(node* &head, int value)

node* n= new node(value);

if(head==NULL){
head=n;

return;

node* temp=head;

while(temp->next!=NULL)

temp=temp->next;

temp->next=n;

void display(node* head)

node* temp=head;

while(temp!=NULL){

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

temp=temp->next;

cout<<"Null"<<endl;

int main()
{

node* head=NULL;

inserttail(head, 21);

inserttail(head, 78);

inserttail(head, 65);

inserttail(head, 34);

inserttail(head, 56);

display(head);

}
Task 2

#include <iostream>

using namespace std;

struct node

int value;

node* next;

};

class Stack {

node* head;

int stacksize;

public:

Stack()

head=NULL;

stacksize=0;

void push(int element)

node* temp= new node();

temp->value=element;

temp->next=head;
head=temp;

cout<<"Element "<<element<<" push into the stack"<<endl;

stacksize++;

void pop()

if(head==NULL)

cout<<"Stack is empty"<<endl;

return;

node* temp=head;

head=temp->next;

temp->next=NULL;

delete temp;

cout<<"Element popped"<<endl;

stacksize--;

int top()

if(head==NULL)

cout<<"No top element, Stack is empty"<<endl;


return -1;

cout<<"Top element is: "<<head->value<<endl;

return head->value;

int size(){

cout<<"Size of Stack is: "<<stacksize<<endl;

return stacksize;

int empty(){

if(head==NULL)

cout<<"Stack is empty"<<endl;

return 1;

cout<<"Stack is not empty"<<endl;

return 0;

};

int main() {

Stack s;
s.empty();

s.push(34);

s.push(78);

s.push(91);

s.pop();

s.push(63);

s.top();

s.size();

s.empty();

return 0;

}
Task 3

#include<iostream>

using namespace std;

struct node {

int data;

node* next;

};

node* front = NULL;

node* rear = NULL;

void enqueue(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;

cout<<"Element Enqueue"<<endl;

void dequeue() {

node* temp = front;


if(front == NULL) {

cout<<"Queue is Empty\n";

return;

if(front == rear) {

front = rear = NULL;

else {

front = front->next;

delete temp;

cout<<"Element Dequeue"<<endl;

void print() {

node* temp = front;

while(temp != NULL) {

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

temp = temp->next;

cout<<endl;

int main(){

enqueue(3);
print();

enqueue(5);

print();

enqueue(7);

print();

dequeue();

print();

enqueue(9);

print();

return 0;

You might also like