Operating System - HP-UX
1752329 Members
5382 Online
108786 Solutions
New Discussion юеВ

Re: Implementing stack and Lucas series in C++

 
SOLVED
Go to solution
Srekandan CR
Occasional Advisor

Implementing stack and Lucas series in C++

Hi All,

Can anybody help me for these programs.
1) Write a C++ program to generate Lucas series

2) Write a C++ program to accept the student's information of UG
students in a college. Also include the functions for searching student
information and updating the existing information.

3) Write a C++ program to create a doubly linked list and support the
following operation:
i). Insertion at the rear end
ii). Insertion at the specific position
iii). Traversing the entire list and displaying the nodes.
iv). Counting the occurrence of duplicate nodes

4) Write a C++ program to implement Stack data structure
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: Implementing stack and Lucas series in C++

Hi:

I'm sorry but this sounds like a HOMEWORK exercise for a course you are taking.

Homework assignments are not what this Forum is about.

You will learn far more by trying yourself. Good luck.

...JRF...
Dennis Handly
Acclaimed Contributor
Solution

Re: Implementing stack and Lucas series in C++

>3) Write a C++ program to create a doubly linked list and support the following operation:

That's called template std::list.

>iii). Traversing the entire list and displaying the nodes.
>iv). Counting the occurrence of duplicate nodes

may help here.

>4) Write a C++ program to implement Stack data structure

That's called template std::stack.
OldSchool
Honored Contributor

Re: Implementing stack and Lucas series in C++

yahoo turns up numerous hits on "Lucas series", including almost exactly the same question (2 weeks ago) on ask.com.

Seems they responded w/ "do your own homework" there as well.
Srekandan CR
Occasional Advisor

Re: Implementing stack and Lucas series in C++

Thx. for everybody. Its here.

Lucas:

#include

void main(){
int L0,L1,temp,MAX;
L0=2;
L1=1;
cout <<" Enter the number of max no of values in series";
cin >> MAX ;
cout << LO << L1;

for(i=0;i temp=L0+L1;
L0=L1;
L1=temp;
cout << L1;
}
}





=============================================================


doubly link

#include

class node
{
public:
int value; //value stored in the node
node *next; //pointer to next node
node *prev; //pointer to previous node
};

class dlist
{
public:
node *front; //pointer to front of list
node *back; //pointer to back of list

dlist()
{
front=NULL;
back=NULL;
}

void insertFront(int value);



void insertBefore(int value,node *nodeB);
void insertAfter(int value,node *nodeA);


void printDListFront();

};



//insert a node before the front node
void dlist::insertFront (int value)
{
node *newNode;
if(this->front==NULL)
{
newNode=new node();
this->front=newNode;
this->back =newNode;
newNode->prev=NULL;
newNode->next=NULL;
newNode->value=value;

}
else
{
insertBefore(value,this->front );
}
}

//insert a node at specified position nodeB
void dlist::insertAfter(int value,node *nodeB)
{
node *newNode;
newNode=new node();
newNode->next= nodeB->next ;
newNode->prev =nodeB;
newNode->value =value;

if(nodeB->next==NULL)
{
cout<<"\n "<< endl;
this->back =newNode;
}
nodeB->next=newNode;
cout<<"2"< }






}

//Print the list from front
void dlist::printDListFront()
{
node* curr2;
curr2= this->front;
cout<<"\n-----\n";
cout<<"Queue\n";
cout<<"-----\n";
//cout<<"size:"< while(curr2!=NULL)
{
cout<<" |"<value<<"|";
curr2=curr2->next;
}
cout<}// print the Double Linked List from front




void main()
{
dlist *st ;
st= new dlist();
st->insertBack(8);
st->printDListFront ();
st->insertBack(5);
st->printDListFront ();
st->insertBack(6);
st->printDListFront ();
st->insertFront(1) ;
st->printDListFront ();
st->insertFront(3) ;
st->printDListFront ();
st->insertBack(7);
st->printDListFront ();
st->removeFront();
st->printDListFront ();
st->removeBack();
st->printDListFront ();
}



====================================================================================================================

STUDENT INFORMATION




include< iostream.h>
#include< conio.h>
class student
{
int count;
struct stud
{
int rollno;
char name[10];
float marks;
}s[10];

public:
void getcount(void);
void getdata(void);
void putdata(void);
void findranker(void);
};

void student::getcount(void)
{
cout< < "Enter no of Students:";
cin>>count;
}

void student::getdata(void)
{
int i;
for(i=0;i< count;i++)
{
cout< < "Name: ";
cin>>s[i].name;
cout< < "Roll no:";
cin>>s[i].rollno;
cout< < "marks:";
cin>>s[i].marks;
}
}

void student::putdata(void)
{
int i;
cout< < "_______________________________________"< < endl;
cout< < "Name\t"< < "Roll no.\t"< < "Marks"< < endl;
cout< < "---------------------------------------"< < endl;
for(i=0;i< count;i++)
cout< < s[i].name< < "\t"< < s[i].rollno< < "\t\t"< < s[i].marks< < endl;
cout< < "---------------------------------------";
}

void student::findranker(void)
{
int i,loc=0;
float top;
top=s[0].marks;
for(i=0;i< count;i++)
{
if(s[i].marks>top)
{
top=s[i].marks;
loc=i;
}

}
cout< < "\nThe Ranker Is:";
cout< < s[loc].name;
}
void main()
{
clrscr();
student s;
s.getcount();
s.getdata();
s.putdata();
s.findranker();
getch();
}

================================================================================================================

STACK OPERATIONS

#include
#include
#include
#include

class element
{
public:
int value;
element* next;
};//class element

class stack
{
public:
int size;
element* current;

stack()
{
size=0;
current=NULL;
}//default constructor

bool push(int,element*);
bool pop();
bool isEmpty();
int getStackSize();
void printStackSize();
void printStackElements(element*);
void printStackMenu();
};

bool stack::push(int ele,element* temp)
{
temp=new element;
if(current==NULL)
{
temp->next=NULL;
}
else
{
temp->next=current;
}
temp->value=ele;
current=temp;
printf("%d inserted\n\n",ele);
size++;
return false;
}

bool stack::pop()
{
if(isEmpty())
{
cout<<"\nStack is Empty\n";
return false;
}
else
{
cout<<"\n Element To POP :"<value;
cout<<"\n Before POP";
printStackElements(current);
current=current->next;
cout<<"\n After POP";
printStackElements(current);
size=size--;
}
return true;
}

bool stack::isEmpty()
{
if(getStackSize()==0)
return true;

return false;
}

int stack::getStackSize()
{
return size;
}//returns size of the stack

void stack::printStackSize()
{
cout<<"\nThe Size of the Stack:"<}//print the stack size

void stack::printStackElements(element* base)
{
element* curr2;
curr2= base;
cout<<"\n-----\n";
cout<<"STACK\n";
cout<<"-----\n";
while(curr2!=NULL)
{
cout<<" |"<value<<"|\n";
curr2=curr2->next;
}
}// print the stack

void stack::printStackMenu()
{
cout<<"Welcome to Stack \n";
cout<<"1.Push an element\n";
cout<<"2.Pop an element\n";
cout<<"3.Display Stack\n";
cout<<"4.Size Of Stack\n";
cout<<"5.Exit\n";
}

void main()
{
stack st;
char Option=0;
int val;
while(1)
{
st.printStackMenu();
cin>>Option;
switch(Option)
{
case '1':
cout<<"Enter a Number \n";
cin>>val;
st.push(val,st.current);
break;
case '2':
st.pop();
break;

case '3':
st.printStackElements(st.current);
break;

case '4':
st.printStackSize();

break;
case '5':
exit(0);
break;
}
}
}




Srekandan CR
Occasional Advisor

Re: Implementing stack and Lucas series in C++

-