永发信息网

数据结构算法变成程序

答案:1  悬赏:70  手机版
解决时间 2021-07-29 07:28

2、编程序实现单链表的各种基本操作,要求主程序采用菜单形式完成各种功能。

InitList DestoryList ListEmpty ListLenghth DispList GetElem LocteElem ListInsert ListDelete

创建线性表 (1,3,5,2,4,10)

插入元素8, 13; 20,12 求长度 查找元素5

删除元素 2 ; 10 求长度

3、编程序实现顺序栈的各种基本操作,要求主程序采用菜单形式完成各种功能。

InitStack ClearStack StackEmpty StackLenghth DispStack Push Pop GetTop

建栈 (1,3,5,2,4) 求长度

进栈8, 9 ; 显示栈中元素

出栈并显示栈中元素

4、编程序实现链栈的各种基本操作,要求主程序采用菜单形式完成各种功能。

InitStack ClearStack StackEmpty StackLenghth DispStack Push Pop GetTop

建栈 (1,3,5,2,4) 求长度

进栈8, 9 ; 显示栈中元素

出栈并显示栈中元素

5、编程序实现顺序队列的各种基本操作,要求主程序采用菜单形式完成各种功能。

InitQueue ClearQueue QueueEmpty QueueLenghth DispQueue EnQueue DeQueue

建立队列(1,3,5,2,4) 求长度;

入队 6

出队到对为空为止

6、编程序实现链栈队列的各种基本操作,要求主程序采用菜单形式完成各种功能。

InitQueue ClearQueue QueueEmpty QueueLenghth DispQueue EnQueue DeQueue

建立队列(1,3,5,2,4) 求长度;

入队 6

出队到对为空为止

7、编程序实现二叉树的各种基本操作,要求主程序采用菜单形式完成各种功能。

CreatBTNode FindNode LchildNode RchildNode Nodes LeafNodes

创建二叉树:

查找元素C显示其左孩子;
最佳答案

题一:


建立三个文件分别copy进去,按以下的划分。



#include <iostream>


#ifndef LIST
#define LIST


const int CAPACITY = 1024;
typedef int ElementType;


class List
{
public:


List();



bool empty() const;



void insert(ElementType item, int pos);


void erase(int pos);



void display(ostream & out) const;


private:

int mySize; // current size of list stored in myArray
ElementType myArray[CAPACITY]; // array to store list elements


}; //--- end of List class


//------ Prototype of output operator
ostream & operator<< (ostream & out, const List & aList);


#endif




#include <cassert>
using namespace std;


#include "List.h"


//--- Definition of class constructor
List::List()
: mySize(0)
{}


//--- Definition of empty()
bool List::empty() const
{
return mySize == 0;
}


//--- Definition of display()
void List::display(ostream & out) const
{
for (int i = 0; i < mySize; i++)
out << myArray[i] << " ";
}


//--- Definition of output operator
ostream & operator<< (ostream & out, const List & aList)
{
aList.display(out);
return out;
}


//--- Definition of insert()
void List::insert(ElementType item, int pos)
{
if (mySize == CAPACITY)
{
cerr << "*** No space for list element -- terminating "
"execution ***\n";
exit(1);
}
if (pos < 0 || pos > mySize)
{
cerr << "*** Illegal location to insert -- " << pos
<< ". List unchanged. ***\n";
return;
}


// First shift array elements right to make room for item


for(int i = mySize; i > pos; i--)
myArray[i] = myArray[i - 1];


// Now insert item at position pos and increase list size
myArray[pos] = item;
mySize++;
}


//--- Definition of erase()
void List::erase(int pos)
{
if (mySize == 0)
{
cerr << "*** List is empty ***\n";
return;
}
if (pos < 0 || pos >= mySize)
{
cerr << "Illegal location to delete -- " << pos
<< ". List unchanged. ***\n";
return;
}


// Shift array elements left to close the gap
for(int i = pos; i < mySize; i++)
myArray[i] = myArray[i + 1];


// Decrease list size
mySize--;
}


//以下是main函数文件:****/


#include <iostream>
using namespace std;


#include "List.h"


int main()
{
// Test the class constructor
List intList;
cout << "Constructing intList\n";


// Test empty() and output of empty list
if (intList.empty())
cout << "Empty List: \n"
<< intList << endl; // Test output of empty list


// Test insert()
for (int i = 0; i < 9; i++)
{
cout << "Inserting " << i << " at position " << i/2 << endl;
intList.insert(i, i/2); // -- Insert i at position i/2
//Test output
cout << intList << endl;
}
cout << "List empty? " << (intList.empty() ? "Yes" : "No") << endl;


cout << "\nTry to insert at position -1" << endl;
intList.insert(0, -1);
cout << "\nTry to insert at position 10" << endl;
intList.insert(0, 10);


// Test erase()
int index;
cout << endl;
while (!intList.empty())
{
cout << "Give an index of a list element to remove: ";
cin >> index;
intList.erase(index);
cout << intList << endl;
}
cout << "List is empty" << endl;


cout << "\nInserting " << CAPACITY<< " integers\n";
for (int i = 0; i < CAPACITY; i++)
intList.insert(i, i);
cout << "Attempting to insert one more integer:\n";
intList.insert(-1, 0);
}


题2:




#include <iostream>
using namespace std;


#include "Stack.h"


//--- Definition of Stack constructor
Stack::Stack()
: myTop(-1)
{}


//--- Definition of empty()
bool Stack::empty() const
{
return (myTop == -1);
}


//--- Definition of push()
void Stack::push(const StackElement & value)
{
if (myTop < STACK_CAPACITY - 1) //Preserve stack invariant
{
++myTop;
myArray[myTop] = value;
}
else
{
cerr << "*** Stack full -- can't add new value ***\n"
"Must increase value of STACK_CAPACITY in Stack.h\n";
exit(1);
}
}


//--- Definition of display()
void Stack::display(ostream & out) const
{
for (int i = myTop; i >= 0; i--)
out << myArray[i] << endl;
}


//--- Definition of top()
StackElement Stack::top() const
{
if ( !empty() )
return (myArray[myTop]);
else
{
cerr << "*** Stack is empty -- returning garbage value ***\n";
return (myArray[STACK_CAPACITY - 1]);
}
}


//--- Definition of pop()
void Stack::pop()
{
if ( !empty() )
myTop--;
else
cerr << "*** Stack is empty -- can't remove a value ***\n";
}

我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
天书奇谈怎么改啊?
DNF中哪种碎片最值钱?大概能卖多少
淇县鹤壁黄堆村商贸区建设指挥部在哪里啊,我
玉兰油的面霜…就是那种七重修护的,好用吗?
分数简便运算 1/9乘3/85 乘18乘51
当你一个人在外面的时候就在这个时候你的身上
抢车位几点开?
感谢让我们相遇的句子,关于“缘分”或“遇见
人为什么称之为人??
下载的IE7 用不成啊
问一下·新武林外传
请问大家!3250是智能机是吧?现在市价是多少
建始县恩施雪花啤酒(官店销售点)在什么地方啊
一首Hip HOp 的歌 开场有议论声的
昆明到成都的火车票价是多少钱
推荐资讯
DNF是今天维护还是明天?
《QQ炫舞的购物卷怎么用?要冲Q币刷吗?》
什么东西吃了对胃有伤害?该怎么做才能尽量减
一天洗一次头,可还有头屑。有什么办法没?
谁能介绍个广州白云区的律师事务所
石鼓区衡阳北京烤鸭怎么去啊,谁知道地址啊
谁告诉我《下一站巨星》什么时候出第二季啊!
“传统文化与家庭教育”的作文怎么写?
咸安区咸宁理华工程液化气直销点哪位知道具体
汝州市平顶山综合公司平价超市地址在什么地方
有关思乡的古诗文
从北京去广西还需要办护照吗?
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?