本文目錄一覽:
- 1、(50分 高手進)漢諾塔問題的堆棧演算法(c語言))
- 2、c語言,我想學習堆棧,先進後出的規則我懂,我想知道具體過程,可以舉個例子最好,謝謝
- 3、誰能幫我說下C語言中的堆棧
- 4、C程序中如何使用堆棧
- 5、C語言問題,利用堆棧實現四則運算,謝謝高手幫我編寫出來
(50分 高手進)漢諾塔問題的堆棧演算法(c語言))
漢諾塔問題的非遞歸非堆棧演算法(一)
#i nclude iostream.h
#i nclude math.h
#define maxno 10000
int step_d,step_s,no;//定義將要行進的步數
void main()
{
cout”請輸入數字(1-64):”;
cinno;//獲取實際的塔片數
//初始化
int **p=new int*[3];
p[0]=new int[no+1];
p[1]=new int[no+1];
p[2]=new int[no+1];
p[0][0]=maxno;p[1][0]=maxno;p[2][0]=maxno;
for(int count=1;count=no;count++)
{
p[0][count]=no-count+1;
p[1][count]=0;
p[2][count]=0;
}
//初始化完畢
if(fmod(no,2)){step_s=2;step_d=1;}else {step_s=1;step_d=2;}//判斷奇數盤的步數和偶數盤的步數
int from,to;
from=0;
to=step_s+from;
p[0]=p[0][no];
while(*(p[0]) != *(p[1]))
{
cout”從柱:”from+1″ 到柱: “to+1endl;
*(++p[to])=*(p[from]);
*(p[from]–)=0;
//以下求得下一步將要From移動的柱子
switch(to)
{
case 2:
if(*(p[0]) *(p[1]))from=0;else from=1;
break;
case 1:
if(*(p[0]) *(p[2]))from=0;else from=2;
break;
case 0:
if(*(p[2]) *(p[1]))from=2;else from=1;
break;
}
//以下一行求得下一步將要移動到的柱子
if(fmod(*(p[from]),2))to=fmod(from+step_s,3);else to=fmod(from+step_d,3);
}
char c;
cinc;
}
漢諾塔問題的非遞歸非堆棧演算法(二)
前一種方法的/*原理:
如果把三個柱子圍成一個環,盤子總數為N,其移動的規律是:
如果N為偶數:奇數號盤每次2步;偶數號盤每次1步;
如果N為奇數:奇數號盤每次1步;偶數號盤每次2步;
至於下一步該移動哪個柱子上的盤子,通過大小和順序即可判斷。
以上可以通過數學證明,不贅述!
*/
以下是第二種演算法:
#i nclude iostream.h
#i nclude math.h
void main(){
int tt = 1,ff=1,fff=1,t;
cout”請輸入(1-64):”;
cin t;
cout”F:表示盤子的起始柱子既From的意思”endl;
cout”T:表示盤子的目標柱子既To的意思”endl;
cout”o:表示在這一步中沒有用到的柱子”endlendl;
for(int i1=1;i1=t;i1++,ff*=2 );
char **hand;
hand=new char*[ff + 1];
for(int i2=0;i2ff+1;i2++) hand[i2] = new char[4];
//char **hand=new char[ff + 1][ 4];
hand[0][1] = ”O”;
hand[0][2] = ”O”;
hand[0][3] = ”O”;
hand[1][1] = ”F”;
hand[1][2] = ”o”;
hand[1][3] = ”T”;
for(int i = 2;i= t;i++){
tt ++;
if(fmod(i,2) == 0){
hand[tt][ 1] = ”F”;
hand[tt][ 2] = ”T”;
hand[tt][ 3] = ”o”;
}
else{
hand[tt][ 1] = ”F”;
hand[tt][ 3] = ”T”;
hand[tt][ 2] = ”o”;
}
fff=1;
for(int h=1;h=i-1;h++,fff*=2);
for(int ii = 1;ii= fff – 1;ii++)
{tt ++;
if(fmod(i, 2)== 0){
hand[tt][ 1] = hand[ii][ 2];
hand[tt][ 2] = hand[ii][ 3];
hand[tt][ 3] = hand[ii][ 1];}
else{
hand[tt][ 1] = hand[ii][ 3];
hand[tt][ 2] = hand[ii][ 1];
hand[tt][ 3] = hand[ii][ 2];
}
}
}
if(fmod(t, 2) == 0){//調換位置
for(int i = 1;i=tt;i++){
hand[i][ 0] = hand[i][ 2];
hand[i][ 2] = hand[i][ 3];
hand[i][ 3] = hand[i][ 0];}
}
for(int u=1;uff;u++ )
coutu”: “hand[u][1]” “hand[u][2]” “hand[u][3]” “endl;
cinfff;
}
}
c語言,我想學習堆棧,先進後出的規則我懂,我想知道具體過程,可以舉個例子最好,謝謝
下面是一個棧的例子:
#include stdio.h
#define STACK_SIZE 100
class Stack {
private:
int *buf;
int top;
public:
Stack(){
buf = new int[STACK_SIZE];
top = 0;
}
void push(int val) {
buf[top++] = val;
}
bool isEmpty() {
return top == 0;
}
int pop() {
if(!isEmpty() ) {
top –;
return buf[top];
}
printf(“The STACK is empty now…”);
return -1;
}
~Stack() {
delete buf;
}
};
int main()
{
Stack stack;
int a[5] = {2, 4, 5, 1, 7};
for(int i=0; i5; i++)
stack.push(a[i]);
while(! stack.isEmpty() ) {
printf(“%d “, stack.pop());
}
printf(“\n”);
}
誰能幫我說下C語言中的堆棧
個人認為樓上的不懂C語言堆棧到底是怎麼回事,按樓上說法,只是大概講了下棧,沒有講堆.
要講C語言的堆棧,要從計算機的數據內存分配講起.
____________________
| Stack區(數組,指針,結構體,局部變數)
____________________
| Static變數(靜態變數,全局變數)
____________________
| Heep區(堆區)
____________________
| 代碼段
____________________
從上面示意圖中可看出整個內存分配,堆分配是在內存中按塊劃分,也就是相對與函數malloc,realloc,calloc.這3個函數為內存分配函數.而且需要手動調用free函數釋放資源,否則會造成大量的內存碎片.
如果樓主不相信可以自己寫一個死循環,內部調用malloc函數,創建N個內存塊,運行一段時間後,絕對會造成系統癱瘓,資源被耗盡.
棧區劃分為計算機自身劃分,即在函數或局部變數被調用時,系統自動為其分配棧,以後進先出為原則實現變數的保存,在函數調用完畢時,系統會自動釋放棧內資源,所以,棧可以說是短命的(生存周期只在調用過程中).
這裡只是粗略說了下堆和棧,另外再說下static–靜態區,全局變數或靜態變數存放於靜態區,只要代碼中存在靜態變數或全局變數,自動放於靜態區,靜態區存放的變數生存周期是整個程序結束時才釋放.
代碼段區,顧名思義存放的是程序代碼(暫時先這麼理解).
PS:本人原創,最近發現一些人盜用本人回答的問題.特此聲明.嘿嘿.
____________________ _________
補充:
我對於C#不是很熟悉,而且我也是從事C開發的,對於面向對象語言應用不是很熟.在這隻能給出C++的代碼.代碼有點長,不知道你能不能看的懂,才寫的.
#include iostream.h
#include stdlib.h
#include malloc.h
#include string.h
#include time.h
#include stdio.h
#include assert.h
/*
//基於數組的棧的實現
#define N 50
typedef struct Stack{
int top;
int A[N];
}*pStack;
//Pop出棧
int Pop(pStack pst)
{
int e;
if(pst-top == -1)
{
cout”Stack is empty!”endl;
return -1;
}
else
{
e = pst-A[pst-top];
pst-top–;
// cout”The element “e” is pop”endl;
return e;
}
}
//Push入棧
void Push(pStack pst)
{
int e;
if(pst-top == N-1)
{
cout”Stack is full!”endl;
}
else
{
cout”Input the push number:”;
cine;
pst-top++;
pst-A[pst-top] = e;
}
}
//清空棧
void empty(pStack pst)
{
pst-top = -1;
}
//判斷棧是否為空
int IsEmpty(pStack pst)
{
if(pst-top == -1)
{
return 0;
// cout”The Stack is empty!”endl;
}
else
{
return 1;
// cout”The Stack is not empty!”endl;
}
}
//判斷棧是否為滿
int IsFull(pStack pst)
{
if(pst-top == N-1)
{
return 0;
}
else
{
return 1;
}
}
//初始化棧
void InitStack(pStack pst)
{
pst-top = -1;
}
void main()
{
Stack S;
InitStack(S);
int n;
cout”How many times do you want to Push:”;
cinn;
for(int i=0; in; i++)
{
Push(S);
}
cout”How many times do you want to Pop:”;
cinn;
for(i=0; in; i++)
{
cout”The element “Pop(S)” is pop”endl;
}
cout”The Stack’s stutor:”endl;
if(IsEmpty(S) == 0)
{
cout”The Stack is empty!”endl;
}
else
{
cout”The Stack is not empty!”endl;
}
if(IsFull(S) == 0)
{
cout”The Stack is full!”endl;
}
else
{
cout”The Stack is not full!”endl;
}
empty(S);
cout”The Stack’s stutor:”endl;
if(IsEmpty(S) == 0)
{
cout”The Stack is empty!”endl;
}
else
{
cout”The Stack is not empty!”endl;
}
}
*/
typedef struct Stack{
Stack *prior;
Stack *next;
int element;
}*pStack;
//壓棧
void Push(pStack *pst)
{
if((*pst) == NULL)
{
pStack S = (pStack)malloc(sizeof(Stack));
(*pst) = S;
(*pst)-next = NULL;
(*pst)-prior = NULL;
cout”Input the PUSH data:”;
cin(*pst)-element;
}
else
{
pStack S = (pStack)malloc(sizeof(Stack));
(*pst)-next = S;
S-prior = (*pst);
S-next = NULL;
(*pst) = S;
cout”Input the PUSH data:”;
cin(*pst)-element;
}
}
//判斷是否為空
int IsEmpty(pStack pst)
{
if(pst == NULL)
{
cout”The Stack is empty!”endl;
return 1;
}
return 0;
}
//出棧
pStack Pop(pStack *pst)
{
if(IsEmpty((*pst)) == 1)
return (*pst);
pStack S = (*pst);
if((*pst)-prior == NULL)
{
cout”Out:”(*pst)-elementendl;
(*pst) = NULL;
free(S);
return (*pst);
}
else
{
cout”Out:”(*pst)-elementendl;
(*pst) = (*pst)-prior;
(*pst)-next = NULL;
free(S);
return (*pst);
}
}
//初始化棧
void InitStack(pStack pst)
{
pst = NULL;
}
void main()
{
pStack pS = NULL;
// InitStack(pS);
int n;
cout”How many times do you want to Push:”;
cinn;
for(int i=0; in; i++)
{
Push(pS);
}
pStack S;
S = Pop(pS);
}
C程序中如何使用堆棧
先從大家比較熟悉的棧說起,它是一種具有後進先出性質的數據結構,也就是說後存放的先取,先存放的後取。這就如同要取出放在箱子裡面底下的東西(放入的比較早的物體),首先要移開壓在它上面的物體(放入的比較晚的物體)。而堆就不同了,堆是一種經過排序的樹形數據結構,每個結點都有一個值。通常所說的堆的數據結構,是指二叉堆。堆的特點是根結點的值最小(或最大),且根結點的兩個子樹也是一個堆。由於堆的這個特性,常用來實現優先隊列,堆的存取是隨意,這就如同在圖書館的書架上取書,雖然書的擺放是有順序的,但是想取任意一本時不必像棧一樣,先取出前面所有的書,書架這種機制不同於箱子,可以直接取出想要的書。
下面就說說C語言程序內存分配中的堆和棧,這裡有必要把內存分配也提一下,一般情況下程序存放在Rom或Flash中,運行時需要拷到內存中執行,內存會分別存儲不同的信息。
內存中的棧區處於相對較高的地址以地址的增長方向為上的話,棧地址是向下增長的,棧中分配局部變數空間,堆區是向上增長的用於分配程序員申請的內存空間。另外還有靜態區是分配靜態變數,全局變數空間的;只讀區是分配常量和程序代碼空間的;以及其他一些分區。來看一個網上很流行的經典例子:
main.cpp
int a = 0; 全局初始化區
char *p1; 全局未初始化區
main()
{
int b; 棧
char s[] = “abc”; 棧
char *p2; 棧
char *p3 = “123456”; 123456\0在常量區,p3在棧上。
static int c =0; 全局(靜態)初始化區
p1 = (char *)malloc(10); 堆
p2 = (char *)malloc(20); 堆
}
堆和棧的第一個區別就是申請方式不同:棧(英文名稱是stack)是系統自動分配空間的,例如定義一個 char a;系統會自動在棧上為其開闢空間。而堆(英文名稱是heap)則是程序員根據需要自己申請的空間,例如malloc(10);開闢十個位元組的空間。由於棧上的空間是自動分配自動回收的,所以棧上的數據的生存周期只是在函數的運行過程中,運行後就釋放掉,不可以再訪問。而堆上的數據只要程序員不釋放空間,就一直可以訪問到,不過缺點是一旦忘記釋放會造成內存泄露。
C語言問題,利用堆棧實現四則運算,謝謝高手幫我編寫出來
我就喜歡害人:)
#includestdio.h
#includeconio.h
#includestdlib.h
#defineERR-1
#defineMAX100/*定義堆棧的大小*/
intstack[MAX];/*用一維數組定義堆棧*/
inttop=0;/*定義堆棧指示*/
intpush(inti)/*存儲運算數,入棧操作*/
{
if(topMAX)
{
stack[++top]=i;/*堆棧仍有空間,棧頂指示上移一個位置*/
return0;
}
else
{
printf(“Thestackisfull”);
returnERR;
}
}
intpop()/*取出運算數,出棧操作*/
{
intvar;/*定義待返回的棧頂元素*/
if(top!=NULL)/*堆棧中仍有元素*/
{
var=stack[top–];/*堆棧指示下移一個位置*/
returnvar;/*返回棧頂元素*/
}
else
printf(“Thestackisempty!\n”);
returnERR;
}
voidmain()
{
intm,n;
charl;
inta,b,c;
intk;
do{
printf(“\tAriothmaticOperatesimulator\n”);/*給出提示信息*/
printf(“\n\tPleaseinputfirstnumber:”);/*輸入第一個運算數*/
scanf(“%d”,m);
push(m);/*第一個運算數入棧*/
printf(“\n\tPleaseinputsecondnumber:”);/*輸入第二個運算數*/
scanf(“%d”,n);
push(n);/*第二個運算數入棧*/
printf(“\n\tChooseoperator(+/-/*//):”);
l=getche();/*輸入運算符*/
switch(l)/*判斷運算符,轉而執行相應代碼*/
{
case’+’:
b=pop();
a=pop();
c=a+b;
printf(“\n\n\tTheresultis%d\n”,c);
printf(“\n”);
break;
case’-‘:
b=pop();
a=pop();
c=a-b;
printf(“\n\n\tTheresultis%d\n”,c);
printf(“\n”);
break;
case’*’:
b=pop();
a=pop();
c=a*b;
printf(“\n\n\tTheresultis%d\n”,c);
printf(“\n”);
break;
case’/’:
b=pop();
a=pop();
c=a/b;
printf(“\n\n\tTheresultis%d\n”,c);
printf(“\n”);
break;
}
printf(“\tContinue?(y/n):”);/*提示用戶是否結束程序*/
l=getche();
if(l==’n’)
exit(0);
}while(1);
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/304859.html