日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

C語言用棧模擬實現(xiàn)隊列問題詳解_C 語言

作者:_奇奇 ? 更新時間: 2022-06-02 編程語言

題目描述

請你僅使用兩個棧實現(xiàn)先入先出隊列。隊列應(yīng)當(dāng)支持一般隊列支持的所有操作(push、pop、peek、empty)。

你只能使用標(biāo)準(zhǔn)的棧操作 —— 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。

題目鏈接

用棧實現(xiàn)隊列

思路分析

題目的意思是要用兩個棧來模擬實現(xiàn)一個隊列。僅可以用棧的基本功能實現(xiàn)隊列的基本功能。所以需要創(chuàng)建兩個棧。所以這兩個棧st1,st2可用一個結(jié)構(gòu)體包含。本質(zhì)就是用兩個后進(jìn)先出的棧,來模擬一個先進(jìn)先出的隊列。

在這里插入圖片描述

思路:

在這里插入圖片描述

1.st2這個棧用來壓棧,st1的作用:把st2的所有值壓到st1中,然后經(jīng)過st1出棧。這樣就達(dá)到了隊列先進(jìn)先出的性質(zhì)。

2.st2一直用來壓棧。如果st1為空則將st2里面的值全都轉(zhuǎn)移到st1,如果st1不為空,則繼續(xù)出棧,知道st1為空為止。

代碼實現(xiàn)

在這里插入圖片描述

ypedef char STDataType;

typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;

//初始化結(jié)構(gòu)體
void StackInit(ST* ps);
//銷毀結(jié)構(gòu)體
void StackDestroy(ST* ps);
//壓棧
void StackPush(ST* ps, STDataType x);
//出棧
void StackPop(ST* ps);
//得到棧頂?shù)闹?
STDataType StackTop(ST* ps);
//判斷棧是否為空
bool StackEmpty(ST* ps);
//得到棧的長度
int StackSize(ST* ps);


//初始化結(jié)構(gòu)體
void StackInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}
//銷毀結(jié)構(gòu)體
void StackDestroy(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;


}
//壓棧
void StackPush(ST* ps, STDataType x)
{

	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* new = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapacity);
		if (new == NULL)
		{
			printf("realloc fail\n");
			exit(-1);
		}
		ps->a = new;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}
void StackPop(ST* ps)
{
	assert(ps);
	assert(ps->top > 0);
	ps->top--;
}
STDataType StackTop(ST* ps)
{
	assert(ps);
    assert(ps->top>0);
	return ps->a[ps->top-1];
}
bool StackEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}
//得到棧的長度
int StackSize(ST* ps)
{
	assert(ps);
	return ps->top;
}



//創(chuàng)建了兩個棧
typedef struct
 {
    ST st1;
    ST st2;

} MyQueue;

//對兩個棧進(jìn)行初始化。
MyQueue* myQueueCreate() 
{
    MyQueue* newQueue = (MyQueue*)malloc(sizeof(MyQueue));
    assert(newQueue);
    StackInit(&newQueue->st1);
    StackInit(&newQueue->st2);

    return newQueue;

}

void myQueuePush(MyQueue* obj, int x) 
{
    assert(obj);
    StackPush(&obj->st2, x);

}

int myQueuePop(MyQueue* obj)
 {
     assert(obj);
     if(StackEmpty(&obj->st1))
     {
        while(!StackEmpty(&obj->st2))
        {
          StackPush(&obj->st1,  StackTop(&obj->st2));
            StackPop(&obj->st2);
        }
     }
        int top = 0;
     if(!StackEmpty(&obj->st1))
     {
         top = StackTop(&obj->st1);
         StackPop(&obj->st1);
     }    
    return top;
}

int myQueuePeek(MyQueue* obj) 
{
   assert(obj);
     if(StackEmpty(&obj->st1))
     {
        while(!StackEmpty(&obj->st2))
        {
          StackPush(&obj->st1,  StackTop(&obj->st2));
            StackPop(&obj->st2);
        }
     }
 
     if(!StackEmpty(&obj->st1))
     {
         return StackTop(&obj->st1);
     }
     return 0;
}

bool myQueueEmpty(MyQueue* obj)
{
    return StackEmpty(&obj->st1) && StackEmpty(&obj->st2);
}

void myQueueFree(MyQueue* obj) 
{
    StackDestroy(&obj->st1);
    StackDestroy(&obj->st2);
    free(obj);
}

原文鏈接:https://blog.csdn.net/qq2466200050/article/details/123843960

欄目分類
最近更新