Sie sind auf Seite 1von 7

1 and 2、

3、
#include <iostream> } else {
#include <string> cout<< "Stack is empty" <<endl;
using namespace std; return -1;
#define SIZE 20 }
class stack { }
private: int array[SIZE]; //variables //push() function
int top; void stack::push(int x) {
public: if(!isfull()) {
stack(); //constructors top++;
void push(int x); //methods array[top] = x;
int pop(); } else {
bool isempty(); cout<< "stack is full" << endl;
bool isfull(); }
int size(); }
int peek(); //pop() function
}; int stack::pop() {
//Constructor if(!isempty()) {
stack::stack() { top --;
array[SIZE ] = { }; return array[top+1];
top=-1; } else {
} cout<<"Stack is empty"<<endl;
bool stack::isempty(){ return 0;
if (top == -1) }
return true; }
else int main(){
return false; int i=0;
} string str1;
bool stack::isfull() { stack sta1;
if (top == SIZE-1) str1="question3";
return true; cout<< str1 <<endl;
else
return false; while(str1[i]!='\0'){
} sta1.push((int)str1[i]);
int stack::size() { i++;
return top+1; }
}
//peek() function while(!sta1.isempty()){
int stack::peek() { cout<< (char)sta1.pop();
if (!isempty()) { }
return array[top]; cout << endl;

return 0;
}
4、
#include <iostream> //enqueue() function
#include <string> void queue::enqueue(int x) {
using namespace std; if (!isfull()) {
#define SIZE 20 if (rear == SIZE - 1) {
class queue { rear = -1;
private: }
int array[SIZE]; rear++;
int front; array[rear] = x;
int rear; nItems++;
int nItems; } else
public: cout << " Queue is full. \
queue(); Cannot enqueue" << endl;
bool isempty(); }
bool isfull(); //dequeue() function
int size(); int queue::dequeue() {
void enqueue(int x); if (!isempty()) {
int dequeue(); int temp = array[front];
int peekfront(); front = (front + 1) % SIZE;
}; nItems--;
//Constructor return temp;
queue::queue() { } else {
front = 0; cout << "Queue is empty. \
rear = -1; Cannot dequeue" << endl;
nItems = 0; return -1;
array[SIZE] = { }; }
} }
//isempty() function // peekfront() function
bool queue::isempty() { int queue::peekfront() {
if (nItems == 0) if (!isempty()) {
return true; return array[front];
else } else {
return false; cout << "Queue is empty" <<
} endl;
//isfull() function return -1;
bool queue::isfull() { }
if (nItems == SIZE) }
return true; int main() {
else int i=0;
return false; string str1;
} queue que1;
//size() function str1="question4";
int queue::size() { cout<< str1 <<endl;
return nItems; while(str1[i]!='\0'){
} que1.enqueue((int)str1[i]);
i++;
}
while(!que1.isempty()){
cout<< (char)que1.dequeue() ;
}
return 0;
}

5、
We can simply think that a linked list is a chain connected by many nodes. Each node
consists of two parts, one is the data field for storing data elements, the other is the pointer
field for storing the address of the next node.
Speaking of objects, we need to know the concept of class.
Class is a description of a kind of thing, an abstract and conceptual definition.
Objects, which actually exist, are individuals of this type of thing, so they are also called
instances.
In short, I think that, to a certain extent, a class can be considered as a blueprint, and an
object is a real thing built from the blueprint. Class is abstract. The object is concrete.
As for the relationship between linked lists and objects, I think objects can be used as
elements in the data field of node.
6、
(1)、Iterative program. The complexity is O(n).
#include <iostream>
using namespace std;
int main(){
int n;
long f=1;
cout << "Please insert the number n:" ;
cin >> n;
//iterative, complexity O(n)
for (int i=1;i<=n;i++){
f=f*i;
}
cout << n << "!=" << f << endl;
return 0;
}

(2)、Recursive program. The complexity is O(n).


#include <iostream>
using namespace std;
//recursive, complexity O(n)
long factorial(int n1){
long sum=1;
if (n1 == 1){
return 1;
} else{
sum = n1*factorial(n1-1);
return sum;
}
}
int main(){
int n;
long f=1;
cout << "Please insert the number n:" ;
cin >> n;
f = factorial(n);
cout << n << "!=" << f << endl;
return 0;
}
7 and 8、

Das könnte Ihnen auch gefallen