『データ構造とアルゴリズム分析(c言語版)』学習ノート3スタック

1095 ワード

スタックの操作:push pop.
スタックのヘッダーにはデータが配置されていません.ヘッダーはスタックトップ、push、popはここで行います!
スタックのユニット構成
typedef struct node
{
	int element;
	node*next;
}node;
スタックを作成
CreateStack(void)
{
	node* S
	S=malloc(sizeof(node));
	if (s==NULL)printf("out of space!!!");
	*S.next=NULL;
	return S;
}
スタックが空かどうかを判断
isempty( node*S)//        
{
	return *S.next==NULL;//       1,    0 
}
スタック操作(push)
void push(int x,node*s)//sΪ±íÍ·µÄÖ¸Õë 
{
	node*temp;
	temp=malloc(sizeof(node));
	if(temp==NULL)printf("out of space!!!");
	*temp.element=x;
	*temp.next=*s.next;
	*s.next=temp;
}

スタックトップ要素を返す
top(node* s)
{
	if (isempty(s)) {printf("empty stack!!!");return 0;}
	else return *(*s.next).element;
}

スタックアウト操作(pop)
pop(node*s)
{
	node* temp;
	if(isempty(s))printf("empty stack!!!");
	else
	{
		temp=*s.next;
		*s.next=*temp.next;
		free(temp);
	}
} 
Nullスタック・オペレーション
MakeEmpty(node*s)
{
	while(*s.next!=NULL)
	pop(s);
}