MENU
Question -

Write the definition of a member function push() for a class Library in C++ to insert a book information in a dynamically allocated strack of books considering the following code is already written as a part of the program

struct book
{
int bookid;
char bookname[20];
book*next;
} ;
class Library
{
book*top;
public
Library()
{
top=NULL;
}
void push();
void pop();
void disp() ;
-Library();
};



Answer -

void Library: :push()
{
book*nptr;
nptr=new book;
cout<<"Enter values for bookid and bookname"; cin> >nptr->bookid;
gets(nptr->bookname);
nptr->next =NULL;
if (top==NULL)
top=nptr;
else
{
nptr->next=top,
top=nptr;
}
}

Comment(S)

Show all Coment

Leave a Comment

Free - Previous Years Question Papers
Any questions? Ask us!
×