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

Data Structure Programs

This document defines a C++ template class for an ordered linked list. The class includes functions for insertion, deletion, checking if an element is in the list, and displaying the list. It uses a Node template class to define the nodes of the linked list, with data and next pointer fields. The main function demonstrates a menu-driven program that uses the linked list class to add and remove elements and display the list.

Uploaded by

Sakshi Bachhety
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)
42 views

Data Structure Programs

This document defines a C++ template class for an ordered linked list. The class includes functions for insertion, deletion, checking if an element is in the list, and displaying the list. It uses a Node template class to define the nodes of the linked list, with data and next pointer fields. The main function demonstrates a menu-driven program that uses the linked list class to add and remove elements and display the list.

Uploaded by

Sakshi Bachhety
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/ 5

ORDERED LINKED LIST

#include<iostream>
using namespace std;

template<class T>
class Node
{
public:
T info;
Node *next;

Node()
{
info=0;
next=0;
}

Node(T x,Node *n=0)
{
info=x;
next=n;
}
};

template<class T>
class Slist
{
Node<T> *head,*tail;

public:

Slist()
{
head=0;
tail=0;
}

int isempty()
{
if(head==0)
return 1;

else
return 0;
}
void insertion(T);
void deletion(T);
T isinlist(T);
void display();
// ~Slist();
};




template<class T>
void Slist<T>::insertion(T x)
{
Node<T> *p=new Node<T>(x);
Node<T> *prev=0,*temp=head;

if(isempty())
head=tail=p;

while(temp!=0 && temp->info<=x && temp!=tail)
{
prev=temp;
temp=temp->next;
}

if(temp!=0)
{
if(prev==0 && x<temp->info)
{
p->next=head;
head=p;
}
else if(temp==tail && x>temp->info)
{
temp->next=p;
tail=p;
}
else
{
p->next=prev->next;
prev->next=p;
}
}
}


template<class T>
T Slist<T>::isinlist(T x)
{
Node<T> *temp=head;

while(temp!=0)
{
if(temp->info==x)
return 1;
else
temp=temp->next;
}

return 0;
}

template<class T>
void Slist<T>::deletion(T x)
{
Node<T> *prev,*cur;
Node<T> *temp=head;


if(head==tail && x==head->info)
{
delete head;
head=tail=0;
cout<<"\n\nNODE DELETED";
}

else if(x==head->info)
{
temp=head;
head=head->next;
delete temp;
cout<<"\n\nNODE DELETED";
}


else
{
prev=head;
cur=head->next;

while(cur!=0 && cur->info!=x)
{
prev=cur;
cur=cur->next;
}

if(cur!=0)
{
prev->next=cur->next;

if(cur==tail)
tail=prev;

delete cur;
cout<<"\n\nNODE DELETED";
}

else
cout<<"\nNOT FOUND";
}
}



template<class T>
void Slist<T>::display()
{
Node<T> *temp;

temp=head;

while(temp!=0)
{
cout<<temp->info;
temp=temp->next;
}
}

void main()
{
Slist<int> l1,l2,l3;
int c,num;
char ans,a;

do{

// clrscr();

cout<<"\n\n-------------SINGLE LINKED LIST---------------\n";
cout<<"\n1. CHECK IF LINKED LIST IS EMPTY OR NOT ";
cout<<"\n2. ADD AN ELEMENT ";
cout<<"\n3. DELETE AN ELEMENT ";
cout<<"\n4. TO CHECK IF ELEMENT IS IN LIST OR NOT ";
cout<<"\n5. DISPLAY LISNKED LIST ";
cout<<"\n\n ENTER YOUR CHOICE :";
cin>>c;

switch(c)
{
case 1: if(l1.isempty())
cout<<"\nLINKED LIST IS EMPTY";
else
cout<<"\nLINKED LIST IS NOT EMPTY";
break;

case 2: cout<<"\n\nENTER THE ELEMENT YOU WANT TO ADD IN LIST :";
cin>>num;

l1.insertion(num);

break;


case 3: if(!l1.isempty())
{
cout<<"\n\nENTER THE ELEMENT WHICH YOU WANT TO
DELETE ";
cin>>num;

l1.deletion(num);



}
else
cout<<"\nLIST IS EMPTY";

break;


case 4: if(!l1.isempty())
{
cout<<"\n\nENTER THE ELEMENT WHICH YOU WANT TO
SEARCH ";
cin>>num;

if(l1.isinlist(num))
cout<<"\nELEMENT FOUND";
else
cout<<"\nNOT FOUND";

}
else
cout<<"\nLIST IS EMPTY";

break;


case 5: if(!l1.isempty())
{
cout<<"\n\nLINKED LIST IS \n:";
l1.display();
}
else
cout<<"\nLIST EMPTY";

break;

default: cout<<"\n\nOPTION NOT PRESENT";
}

cout<<"\n\nDO YOU WISH TO CONTINUE??";
cin>>ans;
}while(ans=='y' || ans=='Y');

}

POLYNOMIAL
#include<iostream>
using namespace std;

class Node
{
public:
int c,power;
Node *next;

Node()
{
c=0;
power=0;
next=0;
}

Node(int x,int y,Node *n=0)
{
c=x;
power=y;

You might also like