0% found this document useful (0 votes)
13 views14 pages

LLLL

The document contains C++ implementations for various data structures including Single Linked List, Doubly Linked List, Stack, and Queue. Each section provides methods for inserting, deleting, and displaying elements, along with example usage in the main function. The code demonstrates fundamental operations for managing these data structures effectively.

Uploaded by

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

LLLL

The document contains C++ implementations for various data structures including Single Linked List, Doubly Linked List, Stack, and Queue. Each section provides methods for inserting, deleting, and displaying elements, along with example usage in the main function. The code demonstrates fundamental operations for managing these data structures effectively.

Uploaded by

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

Single Linked List

#include<iostream>
using namespace std;

struct Node{
public:
int data;
Node *next;
};

class LinkedList{
public:
Node *head;
void insertatb(int value){
Node *newNode=new Node();
if (head==NULL){
newNode->next=NULL;
head=newNode;
head->data=value;
head->next=NULL;
}
else{
newNode->next=head;
newNode->data=value;
head=newNode;
}
}
void display(){
Node *temp;
temp=head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}

void deletedata(int pos){


int i;
Node *temp;
Node *prev;
temp=head;
for(int i=0;i<pos;i++){
prev=temp;
temp=temp->next;
}
prev->next=temp->next;
delete temp;
}
void insertat(int pos,int value){
Node* newNode=new Node();
newNode->data=value;
Node* temp=new Node();
Node* prev=new Node();
temp=head;
int i;
for(int i=0;i<pos;i++){
prev=temp;
temp=temp->next;
}
newNode->next=temp;
prev->next=newNode;
}
};

int main(){
LinkedList ls;
ls.insertatb(9);
ls.insertatb(45);
ls.insertatb(4);
ls.display();
ls.deletedata(2);
ls.display();
ls.insertat(1, 56);
ls.display();
}
Doubly Linked List

#include<iostream>
using namespace std;

struct Node{
public:
int data;
Node *next;
Node *prev;
};

class LinkedList{
public:
Node *head=NULL;
Node *tail=NULL;
void insertatb(int value){
Node *newNode=new Node();
newNode->prev=NULL;
newNode->data=value;
if (head==NULL){
tail=newNode;
}
else{
newNode->next=head;
newNode->prev=NULL;
head->prev=newNode;
}
head=newNode;
}
void display(){
Node *temp;
temp=head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}

void displayr(){
Node *temp;
temp=tail;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->prev;
}
cout<<endl;

}
void deletedata(int pos){
int i;
Node *temp;
temp=head;
for(int i=0;i<pos-1;i++){
temp=temp->next;
}
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
delete temp;
}
void insertat(int pos,int value){
Node* newNode=new Node();
newNode->data=value;
Node* temp=new Node();
temp=head;
int i;
for(int i=0;i<pos-1;i++){
temp=temp->next;
}
newNode->next=temp;
newNode->prev=temp->prev;
temp->prev->next=newNode;
temp->prev=newNode;
}
};

int main(){
LinkedList ls;
ls.insertatb(9);
ls.insertatb(45);
ls.insertatb(779);
ls.insertatb(67);
ls.display();
ls.displayr();
ls.deletedata(2);
ls.display();
ls.insertat(2,90);
ls.display();
ls.insertat(3,88);
ls.display();
ls.insertat(4,2);
ls.display();
}
Stacks

#include<iostream>
using namespace std;

struct Node{
public:
int data;
Node *next;
};

class stack{
public:
Node* top=NULL;
Node *newNode= new Node();
void push(int value){
Node *newNode=new Node();
newNode->data=value;
if(top==NULL){
newNode->next=NULL;
top=newNode;
}
else{
newNode->next=top;
top=newNode;
}}
void display(){
Node *temp=top;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}

void pop(){
top=top->next;
}
};

int main(){
stack st;
st.push(39);
st.push(68);
st.push(45);
st.display();
st.pop();
st.display();
st.pop();
st.display();
st.pop();
st.display();
}
QUEUE
#include<iostream>
using namespace std;

struct Node{
public:
int data;
Node *next;
};

class queue{
public:
Node* top=NULL;
Node* rear=NULL;
Node *newNode= new Node();
void enqueue(int value){
Node *newNode=new Node();
newNode->data=value;
if(top==NULL){
newNode->next=NULL;
top=newNode;
rear=newNode;
}
else{
rear->next=newNode;
rear=newNode;
}}
void display(){
Node *temp=top;
while(temp!=NULL){
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}

void dequeue(){
top=top->next;
}
};

int main(){
queue q;
q.enqueue(2);
q.enqueue(56);
q.display();
q.dequeue();
q.display();
}
Tress Implementation in C++

You might also like