?? 4-14.c
字號(hào):
#include "stdio.h"
#include "iostream.h"
#include "string.h"
#define StackSize 100 //假定預(yù)分配的棧空間最多為100個(gè)元素
#define MaxLength 100// 最大的字符串長度
typedef int DataType;//假定棧元素的數(shù)據(jù)類型為整數(shù)
typedef struct{
DataType data[StackSize];
int top;
}SeqStack;
// 置棧空
void Initial(SeqStack *S)
{//將順序棧置空
S->top=-1;
}
//判棧空
int IsEmpty(SeqStack *S)
{
return S->top==-1;
}
//判棧滿
int IsFull(SeqStack *S)
{
return S->top==StackSize-1;
}
//進(jìn)棧
void Push(SeqStack *S,DataType x)
{
if (IsFull(S))
{
printf("棧上溢"); //上溢,退出運(yùn)行
exit(1);
}
S->data[++S->top]=x;//棧頂指針加1后將x入棧
}
//出棧
DataType Pop(SeqStack *S)
{
if(IsEmpty(S))
{
printf("棧為空"); //下溢,退出運(yùn)行
return -1;
}
return S->data[S->top--];//棧頂元素返回后將棧頂指針減1
}
// 取棧頂元素
DataType Top(SeqStack *S)
{
if(IsEmpty(S))
{
printf("棧為空"); //下溢,退出運(yùn)行
exit(1);
}
return S->data[S->top];
}
void main(void)
{// 離線等價(jià)類問題
int n, r,i;
int a,b;
SeqStack *SeqStack;
//輸入n 和r
printf("Enter number of elements\n");
scanf("%d",&n);
if(n<2){
printf("Too few elements\n");
exit(l);
}
printf("Enter number of relations\n");
scanf("%d",&r);
if (r < 1) {
printf( "Too few relations\n");
exit(l);
}
//創(chuàng)建一個(gè)指向n個(gè)鏈表的數(shù)組
SeqStack = (SeqStack*)malloc(sizeof(Chain)*(n+1);
if(chain==NULL)
{
printf("Out of memory\n");
exit(1);
}
//輸入r個(gè)關(guān)系,并存入鏈表
for ( i= 1; i <= r; i++) {
printf("Enter next relation/pair\n");
scanf("%d %d",&a,&b);
Push(&chain[a],b) ;
Push(&chain[b],a) ;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -