#include <cstdlib>
#include <iostream>
using namespace std;
class SLNode{
int element;
SLNode *next;
SLNode(int evalue, SLNode *nvalue){
element=evalue; next=nvalue;
}
friend class SinglyLinkedList;
};
class SinglyLinkedList
{
SLNode *head;
SLNode *tail;
public:
SinglyLinkedList();
~SinglyLinkedList();
bool IsEmpty();
void MakeEmpty();
int First();
int Last();
void InsertFirst(int item);
void InsertLast(int item);
void InsertBefore(int key, int item);
void InsertAfter(int key, int item);
void InsertPosition(int pos, int item);
bool FindItem(int item);
void DeleteFirst();
void DeleteLast();
void DeleteBefore(int item);
void DeleteAfter(int item);
void DeletePosition(int pos);
void DeleteItem(int item);
int Count();
void Print();
};
SinglyLinkedList::SinglyLinkedList(){
head=tail=NULL;
}
SinglyLinkedList::~SinglyLinkedList(){
MakeEmpty();
}
void SinglyLinkedList::MakeEmpty()
{
SLNode *curr=NULL;
while(head !=NULL){
curr=head;
head=head->next;
delete curr;
}
tail=NULL;
}
bool SinglyLinkedList::IsEmpty(){
if(head==NULL && tail==NULL)
return true;
return false;
}
void SinglyLinkedList::InsertFirst(int item)
{
SLNode *curr=new SLNode(item, head);
if(IsEmpty()==true)
tail=curr;
head=curr;
}
void SinglyLinkedList::InsertLast(int item)
{
SLNode *curr=new SLNode(item, NULL);
if(IsEmpty()==true)
head=curr;
else
tail->next=curr;
tail=curr;
}
void SinglyLinkedList::InsertBefore(int key, int item){
SLNode *prevcurr=NULL;
SLNode *nextcurr=head;
if(IsEmpty()==false){
while(nextcurr->element!=key && nextcurr!=NULL){
prevcurr=nextcurr;
nextcurr=nextcurr->next;
}
if(nextcurr==NULL)
cout<<"ugugdsun element oldsongvi";
if(nextcurr->element==key) {
SLNode *curr=new SLNode(item, nextcurr);
prevcurr->next=curr;
}
}
else
cout<<"SinglyLinkedList is empty"<<endl;
}
void SinglyLinkedList::InsertAfter(int key, int item) {
SLNode *prevCurr=NULL;
SLNode *nextCurr=head;
if(IsEmpty()==false){
while(prevCurr->element!=key && nextCurr!=NULL){
prevCurr=nextCurr;
nextCurr=nextCurr->next;
}
if(nextCurr==NULL)
cout<<"ugugdsun element oldsongvi";
if(prevCurr->element==key){
SLNode *curr=new SLNode(item, nextCurr);
prevCurr->next=curr;
}
}
else
cout<<"SinglyLinkedList is empty"<<endl;
}
void SinglyLinkedList::InsertPosition(int pos, int item)
{
if(IsEmpty()==false){
SLNode *prevCurr=NULL;
SLNode *nextCurr=head;
int i=1;
while(i!=pos && nextCurr!=NULL){
prevCurr=nextCurr;
nextCurr=nextCurr->next;
i++;
}
if(nextCurr==NULL)
cout<<"bairlal buruu";
if(i==pos) {
SLNode *curr=new SLNode(item, nextCurr);
prevCurr->next=curr;
}
}
else
cout<<"SinglyLinkedList is empty"<<endl;
}
void SinglyLinkedList::Print(){
if(IsEmpty()==false){
SLNode *curr=head;
cout<<"SinglyLinkedList:";
while(curr!=NULL){
cout<<curr->element<<"->";
curr=curr->next;
}
}
else
cout<<"SinglyLinkedList is empty"<<endl;
}
bool SinglyLinkedList::FindItem(int key){
if(IsEmpty()==false){
SLNode *curr=head;
while(curr!=NULL && curr->element!=key){
curr=curr->next;
}
if(curr->element==key) return true;
return false;
}
else
return false;
}
int SinglyLinkedList::Count(){
if(IsEmpty()==false){
SLNode *curr=head;
int counter=0;
while(curr!=NULL){
counter++;
curr=curr->next;
}
return counter;
}
else
return 0;
}
int SinglyLinkedList::First(){
if(IsEmpty()==true)
cout<<"SinglyLinkedList is empty";
return head->element;
}
int SinglyLinkedList::Last(){
if(tail==NULL)
cout<<"SinglyLinkedList is empty";
return tail->element;
}
void SinglyLinkedList::DeleteFirst(){
if(IsEmpty()==false){
SLNode *curr=head;
head=curr->next;
delete curr;
}
else
cout<<"SinglyLinkedList is empty"<<endl;
}
void SinglyLinkedList::DeleteLast(){
if(IsEmpty()==false){
SLNode *prevCurr=NULL;
SLNode *curr=head;
while(curr!=tail)
{
prevCurr=curr;
curr=curr->next;
}
if(curr==tail){
prevCurr->next=curr->next;
tail=prevCurr;
}
delete curr;
}
else
cout<<"SinglyLinkedList is empty"<<endl;
}
void SinglyLinkedList::DeleteBefore(int key){
if(IsEmpty()==false){
SLNode *prevCurr=NULL;
SLNode *curr=NULL;
SLNode *nextCurr=head;
while(nextCurr!=NULL && nextCurr->element!=key)
{
prevCurr=curr;
curr=nextCurr;
nextCurr=nextCurr->next;
}
if(nextCurr==NULL)
cout<<"key element oldsongvi"<<endl;
if(nextCurr==head)
cout<<"key elementiin umnu element bhgvi bna"<<endl;
else if(nextCurr->element==key){
if(curr==head)
head=nextCurr;
else
prevCurr->next=nextCurr;
delete curr;
}
}
else
cout<<"SinglyLinkedList is empty"<<endl;
}
void SinglyLinkedList::DeleteAfter(int key){
if(IsEmpty()==false){
SLNode *prevCurr=NULL;
SLNode *curr=head;
while(curr!=NULL && prevCurr->element!=key)
{
prevCurr=curr;
curr=curr->next;
}
if(curr==NULL)
cout<<"key element oldsongvi"<<endl;
if(prevCurr==tail)
cout<<"key elementiin hoino element bhgvvi bna"<<endl;
else if(prevCurr->element==key){
prevCurr->next=curr->next;
delete curr;
}
}
else
cout<<"SinglyLinkedList is empty"<<endl;
}
void SinglyLinkedList::DeleteItem(int key){
if(IsEmpty()==false){
SLNode *prevCurr=NULL;
SLNode *curr=head;
while(curr!=NULL && curr->element!=key)
{
prevCurr=curr;
curr=curr->next;
}
if(curr==NULL)
cout<<"key element oldsongvi"<<endl;
if(curr==tail)
tail=prevCurr;
if(curr==head)
head=prevCurr;
if(curr->element==key){
prevCurr->next=curr->next;
delete curr;
}
}
else
cout<<"SinglyLinkedList is empty"<<endl;
}
void SinglyLinkedList::DeletePosition(int pos){
if(IsEmpty()==false){
int i=0;
SLNode *prevCurr=NULL;
SLNode *curr=head;
while(curr!=NULL && i!=pos)
{
prevCurr=curr;
curr=curr->next;
i++;
}
if(curr==NULL)
cout<<"bairlal oldsongvi"<<endl;
if(i==pos)
if(curr==head)
head=curr->next;
else
prevCurr->next=curr->next;
if(curr==tail)
tail=prevCurr;
delete curr;
}
else
cout<<"SinglyLinkedList is empty"<<endl;
}
int main(int argc, char *argv[])
{
SinglyLinkedList list;
list.InsertFirst(21);
list.InsertFirst(24);
list.Print();
system("PAUSE");
return EXIT_SUCCESS;
}
No comments:
Post a Comment