0% found this document useful (0 votes)
57 views

Linked List: Add and Delete Node From Begining

The document discusses implementations of common data structures like linked lists, stacks, and queues using linked lists. It includes code to add and remove nodes from the beginning and end of linked lists, push and pop nodes from a stack, and insert and remove nodes from a queue. Functions are defined with methods to manipulate the structures and traverse the nodes. A menu driver is included to test the different operations.

Uploaded by

Devansh Gupta
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)
57 views

Linked List: Add and Delete Node From Begining

The document discusses implementations of common data structures like linked lists, stacks, and queues using linked lists. It includes code to add and remove nodes from the beginning and end of linked lists, push and pop nodes from a stack, and insert and remove nodes from a queue. Functions are defined with methods to manipulate the structures and traverse the nodes. A menu driver is included to test the different operations.

Uploaded by

Devansh Gupta
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/ 13

Linked List: Add and delete node from begining

#include<iostream.h>
#include<conio.h>
struct node
{
int info;
node *next;
};
class list
{
node *start;
public:
list()
{
start=NULL;
}
void addnodebeg();
void deletenodebeg();
void traversenode();
};
void list :: addnodebeg()
{
node *nodeptr;
nodeptr=new node;
cout<<"Enter info"<<endl;
cin>>nodeptr->info;
nodeptr->next=start;
start=nodeptr;
cout<<"Node added."<<endl;
}
void list :: deletenodebeg()
{
if(start==NULL)
{
cout<<"Can not delete. List is empty."<<endl;
}
else
{
node *temp;
temp=start;
cout<<"Deleted info is"<<" "<<temp->info<<endl;
start=temp->next;
delete temp;
}
}
void list :: traversenode()
{
if(start==NULL)
{
cout<<"List is empty, Can not display"<<endl;
}
else
{
struct node *temp;
for(temp=start;temp!=NULL;temp=temp->next)
{
cout<<temp->info<<endl;
}
}
}
void main()
{
clrscr();
list l;
int ch;
char ans;
do
{
cout<<"Press 1. Add node to the begining"<<endl;
cout<<"Press 2. Delete node from the begining"<<endl;
cout<<"Press 3. Display node"<<endl;
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
l.addnodebeg();
break;
case 2:
l.deletenodebeg();
break;
case 3:
l.traversenode();
break;
default:
cout<<"Invalid no. entered."<<endl;
break;
}
cout<<"Wish to continue."<<endl;
cin>>ans;
}while(ans=='Y'||ans=='y');
getch();
}

__________________________________________________________________________________________________
__________________________________________________________________________________________________
Linked List: Adding and Deleting Node from the end
#include<iostream.h>
#include<conio.h>
struct node
{
int info;
node *next;
};
class list
{
node *start, *last;
public:
list()
{
start=last=NULL;
}
void addnodelast();
void deletenodelast();
void traversenode();
};
void list :: addnodelast()
{
node *nodeptr;
nodeptr=new node;
cout<<"Enter info"<<endl;
cin>>nodeptr->info;
nodeptr->next=NULL;
if(start==NULL)
{
start=nodeptr;
}
else
{
last->next=nodeptr;
}
last=nodeptr;
cout<<"Node added to the list."<<endl;
}
void list :: deletenodelast()
{
if(start==NULL)
{
cout<<"Can not delete. List is empty."<<endl;
}
else
{
node *temp;
temp=start;
temp->next!=last;
temp=temp->next;
cout<<"Deleted info is"<<" "<<last->info<<endl;
delete last;
last=temp;
last->next=NULL;
}
}
void list :: traversenode()
{
if(start==NULL)
{
cout<<"List is empty, Can not display"<<endl;
}
else
{
struct node *temp;
for(temp=start;temp!=NULL;temp=temp->next)
{
cout<<temp->info<<endl;
}
}
}
void main()
{
clrscr();
list l;
int ch;
char ans;
do
{
cout<<"Press 1. Add node to the end"<<endl;
cout<<"Press 2. Delete node from the end"<<endl;
cout<<"Press 3. Display node"<<endl;
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
l.addnodelast();
break;
case 2:
l.deletenodelast();
break;
case 3:
l.traversenode();
break;
default:
cout<<"Invalid no. entered."<<endl;
break;
}
cout<<"Wish to continue."<<endl;
cin>>ans;
}while(ans=='Y'||ans=='y');
getch();
}
__________________________________________________________________________________________________
__________________________________________________________________________________________________
Stack as a Linked List or Linked Stack
#include<iostream.h>
#include<conio.h>
struct node
{
int info;
node *next;
};
class stack
{
node *top;
public:
stack()
{
top=NULL;
}
void push();
void pop();
void display();
};
void stack :: push()
{
node *nodeptr;
nodeptr=new node;
cout<<"Enter info"<<endl;
cin>>nodeptr->info;
nodeptr->next=top;
top=nodeptr;
cout<<"Node pushed."<<endl;
}
void stack :: pop()
{
if(top==NULL)
{
cout<<"Can not pop. Stack is empty."<<endl;
}
else
{
node *temp;
temp=top;
cout<<"Popped info is"<<" "<<temp->info<<endl;
top=temp->next;
delete temp;
}
}
void stack :: display()
{
if(top==NULL)
{
cout<<"Can not display"<<endl;
}
else
{
node *temp;
for(temp=top;temp!=NULL;temp=temp->next)
{
cout<<temp->info<<endl;
}
}
}
void main()
{
clrscr();
stack l;
int ch;
char ans;
do
{
cout<<"Press 1. Push"<<endl;
cout<<"Press 2. Pop"<<endl;
cout<<"Press 3. Display "<<endl;
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
l.push();
break;
case 2:
l.pop();
break;
case 3:
l.display();
break;
default:
cout<<"Invalid no. entered."<<endl;
break;
}
cout<<"Wish to continue."<<endl;
cin>>ans;
}while(ans=='Y'||ans=='y');
getch();
}
__________________________________________________________________________________________________
__________________________________________________________________________________________________
Queue as a linked list or Linked Queue
#include<iostream.h>
#include<conio.h>
struct node
{
int info;
node *next;
};
class queue
{
node *front,*rear;
public:
queue()
{
front=rear=NULL;
}
void insert();
void remove();
void display();
};
void queue :: insert()
{
node *nodeptr;
nodeptr=new node;
cout<<"Enter info"<<endl;
cin>>nodeptr->info;
nodeptr->next=NULL;
if(front==NULL)
{
front=nodeptr;
}
else
{
rear->next=nodeptr;
}
rear=nodeptr;
cout<<"Node inserted."<<endl;
}
void queue :: remove()
{
if(front==NULL)
{
cout<<"Can not delete."<<endl;
}
else
{
node *temp;
temp=front;
front=temp->next;
cout<<"Deleted info is"<<" "<<temp->info<<endl;
delete temp;
}
}
void queue :: display()
{
if(front==NULL)
{
cout<<"Can not display"<<endl;
}
else
{
node *temp;
for(temp=front;temp!=NULL;temp=temp->next)
{
cout<<temp->info<<endl;
}
}
}
void main()
{
clrscr();
queue l;
int ch;
char ans;
do
{
cout<<"Press 1. Insert"<<endl;
cout<<"Press 2. Delete"<<endl;
cout<<"Press 3. Display "<<endl;
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
l.insert();
break;
case 2:
l.remove();
break;
case 3:
l.display();
break;
default:
cout<<"Invalid no. entered."<<endl;
break;
}
cout<<"Wish to continue."<<endl;
cin>>ans;
}while(ans=='Y'||ans=='y');
getch();
}
__________________________________________________________________________________________________
__________________________________________________________________________________________________
Stack as an Array
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
const int size=10;
int stack[size],i,top,ch;
char ans;
top=-1;
do
{
cout<<"Press 1 to push"<<endl;
cout<<"Press 2 to pop"<<endl;
cout<<"Press 3 to display"<<endl;
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
top=top+1;
if(top>size-1)
{
cout<<"Stack overflow"<<endl;
top=top-1;
}
else
{
cout<<"Enter element"<<endl;
cin>>stack[top];
cout<<"Element pushed"<<endl;
}
break;
case 2:
if(top==-1)
{
cout<<"Underflow"<<endl;
}
else
{
cout<<"Passed info is "<<stack[top];
top = top-1;
}
break;
case 3:
if(top==-1)
{
cout<<"Underflow can't display"<<endl;
}
else
{
for(i=top;i>=0;i--)
{
cout<<stack[i]<<" ";
}
}
break;
default:
cout<<"Invalid choice"<<endl;
break;
}
cout<<"Wish to continue."<<endl;
cin>>ans;
}while(ans=='Y'||ans=='y');
getch();
}
__________________________________________________________________________________________________
__________________________________________________________________________________________________
Queue as an array
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr()
const int size=10;
int queue[size],i,front,rear,ch;
char ans;
front=rear=-1;
do
{
cout<<"Press 1 to insert"<<endl;
cout<<"Press 2 to delete"<<endl;
cout<<"Press 3 to display"<<endl;
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
rear=rear+1;
if(front==-1)
{
front=0;
}
if(rear>size-1)
{
cout<<"Cannot insert"<<endl;
}
else
{
cout<<"Enter element"<<endl;
cin>>queue[rear];
cout<<"Element pushed"<<endl;
}
break;
case 2:
if(front==-1)
{
cout<<"Cannot delete. Queue is empty."<<endl;
}
else
{
cout<<"Deleted info is "<<queue[front]<<endl;
front=front+1;
}
if(front>rear)
{
front=rear=-1;
}
break;
case 3:
if(front==-1)
{
cout<<"Queue is empty"<<endl;
}
else
{
for(i=front;i<=rear;i++)
{
cout<<queue[i]<<endl;
}
}
break;
default:
cout<<"Invalid choice"<<endl;
break;
}
cout<<"Wish to continue."<<endl;
cin>>ans;
}while(ans=='Y'||ans=='y');
getch();
}
__________________________________________________________________________________________________
__________________________________________________________________________________________________
Circular Queue as an array
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
const int size=10;
int c[size],i,front,rear,ch;
char ans;
front=rear=-1;
do
{
cout<<"Press 1 to insert"<<endl;
cout<<"Press 2 to delete"<<endl;
cout<<"Press 3 to display"<<endl;
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
if((front==0 && rear==size-1)||front==rear+1)
{
cout<<"Can not insert"<<endl;
break;
}
else if(rear==-1)
{
front=rear=0;
}
else if(rear==size-1&&front>0)
{
rear=0;
}
else
{
rear++;
}
cout<<"Enter element"<<endl;
cin>>c[rear];
cout<<"Element Inserted"<<endl;
break;
case 2:
if(front==-1)
{
cout<<"Cannot delete. Queue is empty."<<endl;
}
else
{
cout<<"Deleted info is "<<c[front]<<endl;
if(front==rear)
{
front=rear=-1;
}
else if(front==size-1&&rear>=0)
{
front=0;
}
else
{
front++;
}
}
break;
case 3:
if(rear>=front)
{
for(i=0;i<front;i++)
{
cout<<"*";
}
for(i=front;i<=rear;i++)
{
cout<<c[i]<<" ";
}
for(i=rear+1;i<size;i++)
{
cout<<"*";
}
}
else
{ for(i=0;i<=rear;i++)
{
cout<<c[i]<<" ";
}
for(i=rear+1;i<front;i++)
{
cout<<"*";
}
for(i=front;i<size;i++)
{
cout<<c[i];
}
}
break;
default:
cout<<"Invalid choice"<<endl;
break;
}
cout<<"Wish to continue."<<endl;
cin>>ans;
}while(ans=='Y'||ans=='y');
getch();
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy