亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

高手請進,一道關于數據結構題目,麻煩幫忙看看怎么解決!

高手請進,一道關于數據結構題目,麻煩幫忙看看怎么解決!

Cats萌萌 2022-01-14 15:11:50
根據下面給出的棧的存儲結構定義//定義棧的存儲結構typedef struct StackNode{ElemType data; //存放數據struct StackNode * next; //指向下一個結點}StackNode;typedef struct{StackNode * top; // 棧頂指針}LinkStack;實現下列操作:void InitStack(LinkStack &S); // 棧的初始化void Push(const int &item,LinkStack &S); // 入棧char Pop(LinkStack &S); // 出棧char GetTop(LinkStack &S); // 取棧頂元素int IsEmpty(LinkStack &S); // 判斷棧是否為空void MakeEmpty(LinkStack &S); // 清空棧2、 實驗要求(1)、用C語言編程實現上述實驗內容中的結構定義和算法。(2)、要有main()函數,并且在main()函數中使用檢測數據調用上述算法。不是的,我已經做出一半來了,老是出一大些錯誤,想要個現成的參照參照。
查看完整描述

2 回答

?
浮云間

TA貢獻1829條經驗 獲得超4個贊

//LStack.h

//堆棧的鏈式存儲結構

#include<malloc.h>
#include<process.h>

//定義結點結構體
typedef struct StackNode
{
ElemType data; //數據域
struct StackNode *next; //指針域
}StackNode;

//定義棧頂指針
typedef struct
{
StackNode *top; //棧頂指示器
}LinkStack;

//初始化
void InitStack(LinkStack &S)
{
if(NULL==(S.top=(StackNode *)malloc(sizeof(StackNode)))) //申請棧頂結點空間
exit(0); //申請失敗推出程序

S.top->next=NULL; //申請成功棧頂結點指針域初始化為空
S.top->data=NULL; //數據域初始化為NULL
}

//判斷棧是否為空
int isEmpty(LinkStack &S)
{
if(NULL==S.top->next&&NULL==S.top->data) //棧頂沒有后繼結點表明為空棧
return 0; //返回0

return 1; //非空時返回1
}

//入棧
int Push(const ElemType &item,LinkStack &S) //此處最好把item定義為抽象數據類型ElemType
{
StackNode *snp;

if(NULL==(snp=(StackNode *)malloc(sizeof(StackNode)))) //申請新結點空間
return 0;

snp->data=item;
snp->next=S.top;
S.top=snp;

return 1;
}

//出棧
int Pop(LinkStack &S)
{
ElemType reData;
StackNode *indexp;

if(!isEmpty(S)) //??胀顺?br/>return 0;

reData=S.top->data; //取出數據域存儲的數據
indexp=S.top; //保存當前棧頂結點
S.top=S.top->next; //棧頂變為top的后繼結點
free(indexp); //釋放原棧頂結點空間

return reData; //返回棧頂數據
}

//取棧頂數據
int GetTop(LinkStack &S)
{
ElemType reData;

if(!isEmpty(S)) //??胀顺?br/>return 0;

reData=S.top->data; //取出數據

return reData;
}

//清空棧
void MakeEmpty(LinkStack &S)
{
StackNode *p,*pp;

p=S.top;
while(p!=NULL)
{
pp=p;
p=p->next;
free(pp);
}
}

LStack.cpp

#include<stdio.h>

typedef int ElemType;

#include "LStack.h"

int main() //測試堆棧的鏈式存儲結構
{
LinkStack T;
int a[10]={1,3,5,7,9,11,13,15,17,19};

InitStack(T); //初始化棧
for(int i=0;i<10;i++)
Push(a[i],T); // 入棧

printf("棧頂數據=%d\n",GetTop(T)); //取棧頂數據

printf("出棧:");
while(isEmpty(T)) //棧不空時繼續出棧
{
printf("%-4d",Pop(T)); //出棧
}

MakeEmpty(T); //清空棧
}

 


查看完整回答
反對 回復 2022-01-17
?
寶慕林4294392

TA貢獻2021條經驗 獲得超8個贊

/// 按照你的要求寫出來了,并且改了兩個你的錯誤,,

/// 給分吧,,呵呵,祝你學習愉快
// 剛剛把2個C++的內存分配改成c的了

#include <stdio.h>
#include <malloc.h>
#define ElemType int

typedef struct StackNode
{
ElemType data; //存放數據
struct StackNode * next; //指向下一個結點
}StackNode;

typedef struct LinkStack
{
struct StackNode * top; // 棧頂指針
}LinkStack;

void InitStack(LinkStack &S); // 棧的初始化
void Push(const int &item,LinkStack &S); // 入棧
int Pop(LinkStack &S); // 出棧
int GetTop(LinkStack &S); // 取棧頂元素
int IsEmpty(LinkStack &S); // 判斷棧是否為空
void MakeEmpty(LinkStack &S); // 清空棧

// 打印堆棧內容
void PrintStack(LinkStack &S)
{
StackNode *pSN = S.top;
while (pSN->next != 0)
{
printf("%d " , pSN->data);
pSN = pSN->next;
}
printf("\n");
}

void main()
{
LinkStack Stack; // 棧頂
int iItem = 0 ,i = 0;
/// 初始化
InitStack(Stack);

// 壓入內容
for (i = 0 ; i < 10 ; i ++)
{
Push(i , Stack);
}
printf("棧內內容為:");
PrintStack(Stack);

// 彈出
iItem = Pop(Stack);
printf("\n彈出%d:" , iItem);
printf("\n棧內內容為:");
PrintStack(Stack);

// 彈出
iItem = Pop(Stack);
printf("\n彈出%d:" , iItem);
printf("\n棧內內容為:");
PrintStack(Stack);

//清空棧
MakeEmpty(Stack);
}

// 棧的初始化
void InitStack(LinkStack &S)
{
S.top = (struct StackNode*)malloc(sizeof(StackNode));
S.top->data = 0;
S.top->next = 0;
}

// 入棧
void Push(const int &item , LinkStack &S)
{
StackNode *pSN = (struct StackNode*)malloc(sizeof(StackNode));
pSN->data = item;
pSN->next = S.top;
S.top = pSN;
}

// 出棧
int Pop(LinkStack &S)
{
if (IsEmpty(S))
{
return 0;
}
StackNode *pSN = S.top;
int iRet = pSN->data;
S.top = pSN->next;
delete pSN;
return iRet;
}

// 取棧頂元素
int GetTop(LinkStack &S)
{
if (IsEmpty(S))
{
return 0;
}
return S.top->data;
}

// 判斷棧是否為空
int IsEmpty(LinkStack &S)
{
StackNode *pSN = S.top->next;
if (0 == pSN->next)
{
return 1;
}
return 0;
}

// 清空棧
void MakeEmpty(LinkStack &S)
{
while (!IsEmpty(S))
{
Pop(S);
}
if (S.top != 0)
{
delete S.top;
}
S.top = 0;
}



查看完整回答
反對 回復 2022-01-17
  • 2 回答
  • 0 關注
  • 290 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號