Self referential classes are those classes which have at least one data member referring to their respective class.A Self-referential class is a class that contains a reference to an object that has the same class type.
– A self-referential class typically has a
variable definition that is the same type as
the Class.
– This type of class can be used to create
data structures like linked lists, stacks, etc.
– null is used to indicate the end of the data
structure.
example:
class A
{
private:
int data;
class A *next;
//here the 'next' pointer is referring the same class of which it is a member.
};
Solution
/*
Problem Description:
Implement stack/linked list using self-referential-class
submitted by
Arjun Mishra
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
class list
{
private:
int data;
class list *next;
public:
void initialize(int x)
{
data=x;
next=NULL;
}
void assign(list *a)
{
next=a;
}
int display()
{
return data;
}
void makenull()
{
next=NULL;
}
list * next_address()
{
return this->next;
}
}*head,*tail,*tp;
void push()
{
int temp;
printf("Enter the data\n");
scanf("%d",&temp);
tp=(class list*)malloc(sizeof(class list));
tp->initialize(temp);
if(head==NULL)
{
head=tp;
tail=tp;
}
else
{
tail->assign(tp);
tail=tp;
tail->makenull();
}
}
void pop()
{
if(head==NULL)
{
cout<<"cannot delete!the list is empty!\n";
return;
}
if(head->next_address()==NULL)
{
tp=head;
free(tp);
head=NULL;
tail=NULL;
}
else
{
tp=head;
while(tp->next_address()!=tail)
{
tp=tp->next_address();
}
tail=tp;
tp=tp->next_address();
tail->makenull();
free(tp);
}
}
void traverse()
{
if(head==NULL)
{
printf("List is empty!\n");
return;
}
tp=head;
while(tp!=NULL)
{
printf("%d\n",tp->display());
tp=tp->next_address();
}
}
int main()
{
x:
int ch;
char f;
printf("Menu:\n\n");
printf("Press 1 to push\n");
printf("Press 2 to pop\n");
printf("Press 3 to traverse\n");
printf("Press 0 to exit\n");
scanf("%d",&ch);
switch(ch)
{
case 0:
return 0;
case 1:
push();
goto x;
case 2:
pop();
goto x;
case 3:
traverse();
goto x;
default:
printf("Wrong Choice!\n");
goto x;
}
return 0;
}
– A self-referential class typically has a
variable definition that is the same type as
the Class.
– This type of class can be used to create
data structures like linked lists, stacks, etc.
– null is used to indicate the end of the data
structure.
example:
class A
{
private:
int data;
class A *next;
//here the 'next' pointer is referring the same class of which it is a member.
};
Solution
/*
Problem Description:
Implement stack/linked list using self-referential-class
submitted by
Arjun Mishra
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
class list
{
private:
int data;
class list *next;
public:
void initialize(int x)
{
data=x;
next=NULL;
}
void assign(list *a)
{
next=a;
}
int display()
{
return data;
}
void makenull()
{
next=NULL;
}
list * next_address()
{
return this->next;
}
}*head,*tail,*tp;
void push()
{
int temp;
printf("Enter the data\n");
scanf("%d",&temp);
tp=(class list*)malloc(sizeof(class list));
tp->initialize(temp);
if(head==NULL)
{
head=tp;
tail=tp;
}
else
{
tail->assign(tp);
tail=tp;
tail->makenull();
}
}
void pop()
{
if(head==NULL)
{
cout<<"cannot delete!the list is empty!\n";
return;
}
if(head->next_address()==NULL)
{
tp=head;
free(tp);
head=NULL;
tail=NULL;
}
else
{
tp=head;
while(tp->next_address()!=tail)
{
tp=tp->next_address();
}
tail=tp;
tp=tp->next_address();
tail->makenull();
free(tp);
}
}
void traverse()
{
if(head==NULL)
{
printf("List is empty!\n");
return;
}
tp=head;
while(tp!=NULL)
{
printf("%d\n",tp->display());
tp=tp->next_address();
}
}
int main()
{
x:
int ch;
char f;
printf("Menu:\n\n");
printf("Press 1 to push\n");
printf("Press 2 to pop\n");
printf("Press 3 to traverse\n");
printf("Press 0 to exit\n");
scanf("%d",&ch);
switch(ch)
{
case 0:
return 0;
case 1:
push();
goto x;
case 2:
pop();
goto x;
case 3:
traverse();
goto x;
default:
printf("Wrong Choice!\n");
goto x;
}
return 0;
}
No comments:
Post a Comment