Data structure experiment three "stacks and queues"

Source: Internet
Author: User

"Data structure" experiment three

Stacks and queues

First, the purpose of the experiment

consolidates stacks and queue data structures and learns to use stacks and queues.

1. Review the logical structure and restricted operating characteristics of stacks and queues, the physical storage structure of stacks and queues, and common operations.

2. Learn to use stack and queue knowledge to solve practical problems.

3. Further consolidate the procedure debugging method.

4. Further consolidate the template design.

Second, the experimental time

The preparation time is 5th thoughtful 6th week, the specific concentration experiment time is 6 weeks 2nd time lesson. 2 hours of schooling.

Iii. contents of the experiment

1. Choose the sequential or chained storage structure, define an empty stack class, and define the basic operation of stack, stack, and stack elements. Then, the given n data is validated in the main program, and the results of each operation are output.

2. Choose the sequential or chained storage structure, define an empty stack queue, and define the basic operation of stack, stack, and stack elements. Then, the given n data is validated in the main program, and the results of each operation are output.

3. The program implements a decimal number converted to a binary number. Required, to output a 10 progress number in the main program, output its corresponding 2 sequence of binary numbers.

One, the implementation of the chain stack

Source:

#include <iostream>
using namespace Std;

Template <class datatype>
struct Node
{
datatype data;
Node<datatype> *next;
};

Template <class datatype>
Class Linkstack
{
Public
Linkstack () {top=null;}
~linkstack () {while (top!=null) {node<datatype> *q=top;top=top->next;delete q;}};
void push (datatype x);
DataType pop ();
DataType GetTop () {if (top!=null) return top->data;}
int Empty () {if (top==null) return 1;else return 0;}
Private
node<datatype>* top;
};


Template <class datatype>
DataType Linkstack<datatype>::p op ()
{
if (top==null) throw "Xiayi";
DataType x=top->data;
node<datatype> * P=TOP;
top=top->next;
Delete p;
return x;
}


Template <class datatype>
void Linkstack<datatype>::p ush (datatype x)
{
node<datatype> * S=new node<datatype>;
s->data=x;
s->next=top;
Top=s;
}
void Main ()
{
Linkstack<int> T;
if (T.empty () ==1) cout<<endl<< "Stack is empty" <<endl;
else cout<<endl<< "stack non-empty" <<endl;
int r[5];
cout<<endl<< "Please assign 5 values to a stack:" <<endl;
for (int i =0;i<5;i++)
{
cin>>r[i];
}
cout<<endl;
if (T.empty () ==1) cout<<endl<< "Stack is empty" <<endl;
else cout<<endl<< "stack non-empty" <<endl;
for (int n =0;n<5;n++) T.push (R[n]);
cout<<endl<< "Stack top element:" <<t.gettop () <<endl;
cout<<endl<< "Perform a first stack operation:" <<endl;
T.pop ();
cout<<endl<< "Stack top element:" <<t.gettop () <<endl;
cout<<endl<< "Perform a first stack operation:" <<endl;
T.pop ();
cout<<endl<< "Stack top element:" <<t.gettop () <<endl;
}


Second, the implementation of sequential queue

Source:

#include <iostream>
using namespace Std;
const int stacksize=10;

Class Seqstack
{
Public
Seqstack () {top=-1;};
~seqstack () {};
void push (int x);
int pop ();
int GetTop () {if (top!=-1) return data[top];}
int Empty () {if (top==-1) return 1;else return 0;}
Private
int data[stacksize];
int top;
};

void Seqstack::p ush (int x)
{
if (top==stacksize-1) throw "Shangyi";
Data[++top]=x;
}

int Seqstack::p op ()
{
if (top==-1) throw "Xiayi";
int x=data[top--];
return x;
}
void Main ()
{
Seqstack S;
if (S.empty ()) cout<<endl<< "Stack is empty" <<endl;
else cout<<endl<< "stack non-empty" <<endl;
cout<< "Performing a stack operation on 15 and 10" <<endl;
S.push (15);
S.push (10);
cout<<endl<< "Stack top element:" <<endl;
Cout<<s.gettop () <<endl;
cout<<endl<< "Perform a stack operation once:" <<endl;
S.pop ();
cout<<endl<< "Stack top element:" <<endl;
Cout<<s.gettop () <<endl;
}



Third, decimal number into binary number

Source:

#include <iostream>
using namespace Std;

Template <class datatype>
struct Node
{
datatype data;
Node<datatype> *next;
};

Template <class datatype>
Class Linkstack
{
Public
Linkstack () {top=null;}
~linkstack () {while (top!=null) {node<datatype> *q=top;top=top->next;delete q;}};
void push (datatype x);
void Pop ();
DataType GetTop () {if (top!=null) return top->data;}
int Empty () {if (top==null) return 1;else return 0;}
Private
node<datatype>* top;
};


Template <class datatype>
void Linkstack<datatype>::p op ()
{
if (top==null) throw "Xiayi";
node<datatype> * P=TOP;
top=top->next;
Delete p;
}


Template <class datatype>
void Linkstack<datatype>::p ush (datatype x)
{
node<datatype> * S=new node<datatype>;
s->data=x;
s->next=top;
Top=s;
}

void Main ()
{
Linkstack<int> T;
cout<<endl<< "Please enter a decimal number:" <<endl;
int x, y, count=0;
cin>>x;
while (x!=0)
{
y=x%2;
T.push (y);
X=X/2;
++count;
}
if (T.empty () ==1) cout<<endl<< "The input data is wrong! "<<endl;
Else cout<<endl<< "corresponds to a binary number of:";
for (int i=0;i<count;i++)
{
Cout<<t.gettop ();
T.pop ();
}
cout<<endl<<endl;
}

Data structure experiment three "stacks and queues"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.