国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

數(shù)據(jù)結(jié)構(gòu)與算法系列之習(xí)題練習(xí)

這篇具有很好參考價值的文章主要介紹了數(shù)據(jù)結(jié)構(gòu)與算法系列之習(xí)題練習(xí)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

數(shù)據(jù)結(jié)構(gòu)與算法系列之習(xí)題練習(xí)

?? ?? 博客:小怡同學(xué)
?? ?? 個人簡介:編程小萌新
?? ?? 如果博客對大家有用的話,請點贊關(guān)注再收藏 ??

力扣習(xí)題

  1. 括號匹配問題。
  2. 用隊列實現(xiàn)棧。
  3. 用棧實現(xiàn)隊列。
  4. 設(shè)計循環(huán)隊列。

  1. 有效的括號
    //用棧來實現(xiàn)
    //左括號進(jìn)棧 右括號出棧并銷毀如果不匹配則return


#include <stdbool.h>
#include <assert.h> 
#include <stdlib.h>
#include <stdio.h>
typedef char SDatetype;
typedef struct Stack
{
	SDatetype* a;
	int top;
	int capacity;
}Stack;

void StackInit(Stack* pq);
void StackDestory(Stack* pq);
void StackPush(Stack* pq, SDatetype x);
void StackPop(Stack* pq);
bool StackEmpty(Stack* pq);
SDatetype StackTop(Stack* pq);
int StackSize(Stack* pq);
 bool StackEmpty(Stack* pq)
{
	assert(pq);
	return pq->top == 0;
}
void StackInit(Stack* pq)
{
	assert(pq);
	pq->a = NULL;
	pq->capacity = 0;
	pq->top = 0;
}
void StackDestory(Stack* pq)
{
	assert(pq);
	pq->capacity = 0;
	pq->top = 0;
	free(pq->a);
	pq->a =NULL;
}
void StackPush(Stack* pq, SDatetype x)
{
	assert(pq);
	if (pq->top == pq->capacity)
	{
		int newcapacity = (pq->top == 0 ? 4 : (pq->capacity) * 2);
		pq->a = (SDatetype*)realloc(pq->a,sizeof(SDatetype) * newcapacity);
		if (pq->a == NULL)
		{
			perror("malloc fail");
			exit(-1);
		}
		pq->capacity = newcapacity;
	}

	pq->a[pq->top++] = x;

}

void StackPop(Stack* pq)
{
	assert(pq);
	assert(!StackEmpty(pq));
	pq->top--;
}

SDatetype StackTop(Stack* pq)
{
	assert(pq);
	assert(!StackEmpty(pq));
	return pq->a[pq->top-1];
}

int StackSize(Stack* pq)
{
	assert(pq);
	return pq->top;
}
bool isValid(char * s){

Stack pq;
StackInit(&pq);
    while(*s)
    {
        if(*s== '(' || *s == '[' || *s == '{')
        {
           StackPush(&pq, *s);//進(jìn)棧
        }
        else
        {
			if(StackEmpty(&pq))//如果只有一個左括號 無法匹配則return
			{
				StackDestory(&pq);
				return false;
			}
            char tmp = StackTop(&pq);
			StackPop(&pq);
            if( (tmp != '('  && *s == ')' )||
			    (tmp != '[' && *s == ']'  )||
				(tmp != '{' && *s == '}'))//如果有一對不匹配則return
            {
				StackDestory(&pq);//return之前需要銷毀棧 以免內(nèi)存泄漏
                return false;
            }
        }

        s++;
    }

	bool tmp = StackEmpty(&pq);//當(dāng)棧里還有元素時則匹配失敗
	StackDestory(&pq);
	return tmp;
}

225. 用隊列實現(xiàn)棧(力扣)

//設(shè)置兩個隊列,入棧向有元素的隊列加入, 出棧(因為棧上后進(jìn)先出)所以需要把有元素的隊列依次移入另一個隊列 再刪除

typedef int QDatetype;
typedef struct Qnode
{
	int* next;
	QDatetype x;
}Qnode;
typedef struct Queue
{
	Qnode* head;
	Qnode* tail;
}Queue;


typedef struct {
    Queue Qtwo;
    Queue Qone;
} MyStack;


void QueueInit(Queue* pq)
{
	assert(pq);

	pq->tail = pq->head = NULL;

}
void QueueDestory(Queue* pq)
{
	assert(pq);
	while (pq->head)
	{
		Qnode* next = pq->head->next;
		free(pq->head);
		pq->head = next;
	}

}
void QueuePush(Queue* pq,	QDatetype q)
{
	assert(pq);
	Qnode* newnode = (Qnode*)malloc(sizeof(Qnode));
	newnode->next = NULL;
	newnode->x = q;

	if (pq->head == NULL)
	{
		pq->head = pq->tail = newnode;
		
	}
	else
	{
		pq->tail->next = newnode;
		pq->tail = newnode;
	}
}


bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->head == NULL;
}

void QueuePop(Queue* pq)
{
	assert(pq);
	assert(!QueueEmpty(pq));
	if(pq->head  == pq->tail)
	{
		free(pq->head);
		pq->head = pq->tail= NULL;
	}
else
{
Qnode* next = pq->head->next;
	free(pq->head);
	pq->head = next;
}	
}

QDatetype QueueTop(Queue* pq)
{
	assert(pq);
		assert(!QueueEmpty(pq));
	return pq->head->x;
}


int QueueSize(Queue* pq)
{
	assert(pq);
	Qnode* head = pq->head;
	int size = 0;
	while (head)
	{
		size++;
		head =  head->next;
	}
	return size;
}


QDatetype QueueTail(Queue* pq)
{
	assert(pq);
		assert(!QueueEmpty(pq));
	return pq->tail->x;
}

//創(chuàng)造一個由隊列創(chuàng)建的棧
MyStack* myStackCreate() {

     MyStack* pq = (MyStack*)malloc(sizeof(MyStack)); 
		 //向堆申請空間不會隨棧的銷毀而銷毀
     QueueInit(&pq->Qtwo);
     QueueInit(&pq->Qone);
		 return pq;
}

bool myStackEmpty(MyStack* obj) {
    return QueueEmpty(&obj->Qone) && QueueEmpty(&obj->Qtwo);
}

void myStackPush(MyStack* obj, int x) {

Queue* one = &obj-> Qone;
Queue* two = &obj-> Qtwo;

if(!QueueEmpty(one))
{
     QueuePush(one, x);
}
else
{
    QueuePush(two,x);
}
}

int myStackPop(MyStack* obj) {

Queue* noempty = &obj-> Qone;
Queue* empty = &obj-> Qtwo;


if(!QueueEmpty(empty) )
{
	empty = &obj->Qone;
	noempty = &obj->Qtwo;
}

while(QueueSize(noempty) >1)
{
    QueuePush(empty, QueueTop(noempty));
    QueuePop(noempty);
}

int x =  QueueTop(noempty);
 QueuePop(noempty);
return x;


}

int myStackTop(MyStack* obj) {
Queue* one = &obj-> Qone;
Queue* two = &obj-> Qtwo;
if(!QueueEmpty(one))
{
    return QueueTail(one);
}
else
{
    return QueueTail(two);
}
}


void myStackFree(MyStack* obj) {

    QueueDestory(&obj->Qone);
    QueueDestory(&obj->Qtwo);
		free(obj);
    }

myStackPop函數(shù)的功能實現(xiàn)圖

數(shù)據(jù)結(jié)構(gòu)與算法系列之習(xí)題練習(xí)

232. 用棧實現(xiàn)隊列(力扣)

//與用隊列實現(xiàn)棧的思想不同, 這里的棧分別是push棧 和pop棧 (專門用來插入數(shù)據(jù)和刪除數(shù)據(jù))


#include <stdbool.h>
#include <assert.h> 
#include <stdlib.h>
#include <stdio.h>
typedef int SDatetype;
typedef struct Stack
{
	int* a;
	int top;
	int capacity;
}Stack;

 bool StackEmpty(Stack* pq)
{
	return pq->top == 0;
}
int StackSize(Stack* pq)
{
    return pq->top;
}
void StackInit(Stack* pq)
{
	assert(pq);
	pq->a = NULL;
	pq->capacity = 0;
	pq->top = 0;
}
void StackDestory(Stack* pq)
{
	assert(pq);
	pq->capacity = 0;
	pq->top = 0;
	free(pq->a);
}

void StackPush(Stack* pq, SDatetype x)
{
	assert(pq);
	if (pq->top == pq->capacity)
	{
		int newcapacity = (pq->top == 0 ? 4 : (pq->capacity) * 2);
		pq->a = (SDatetype*)realloc(pq->a,sizeof(SDatetype) * newcapacity);
		if (pq->a == NULL)
		{
			perror("malloc fail");
			return;
		}
		pq->capacity = newcapacity;
	}

	pq->a[pq->top++] = x;

}



void StackPop(Stack* pq)
{
	assert(pq);
	assert(!StackEmpty(pq));
	pq->a[pq->top--];
}

SDatetype StackTop(Stack* pq)
{
	assert(pq);
	assert(!StackEmpty(pq));
	return pq->a[pq->top-1];
}
typedef struct {
    Stack push;
    Stack pop;
} MyQueue;


MyQueue* myQueueCreate() {
    MyQueue* pq =(MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&pq->push);
    StackInit(&pq->pop);
	return pq;
}


bool myQueueEmpty(MyQueue* obj) {
	return  StackEmpty(&obj->push) && StackEmpty(&obj->pop);
}


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


//這個函數(shù)主要功能實現(xiàn)的是 出隊的元素是誰
//有兩種情況:
//1.pop棧中有元素,直接出隊
//2.pop棧中無元素, 需要把push棧中移入到pop棧里
int myQueuePeek(MyQueue* obj) {
    if(StackEmpty(&obj->pop))
    {
        while(!StackEmpty(&obj->push))
        {
          StackPush(&obj->pop ,StackTop(&obj->push));
          StackPop(&obj->push);
        }
    }
    return StackTop(&obj->pop);  
}



int myQueuePop(MyQueue* obj) {

    int front = myQueuePeek(obj);
    StackPop(&obj->pop);
    return front; 
}

void myQueueFree(MyQueue* obj) {
	StackDestory(&obj->push);
	StackDestory(&obj->pop);
	free(obj);
}

myQueuePop主要功能圖

數(shù)據(jù)結(jié)構(gòu)與算法系列之習(xí)題練習(xí)

622. 設(shè)計循環(huán)隊列

//主要是用順序表存儲數(shù)據(jù)
//不用鏈表實現(xiàn)是因為1.找尾不容易找到,2,鏈表是否存滿不好判斷

typedef struct {

int front;
int rear ;
int k;
int* a;
} MyCircularQueue;


//公開辟(k+1)個空間 判斷是否判空
MyCircularQueue* myCircularQueueCreate(int k) {
 MyCircularQueue* pq = ( MyCircularQueue*)malloc(sizeof( MyCircularQueue));
 pq->front = pq->rear= 0;
 pq->k = k;
 pq->a = (int*)malloc(sizeof(int)*(k+1));
 return pq;
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    return obj->rear  == obj->front ;
    
}
//rear+1 = front則鏈表滿了
bool myCircularQueueIsFull(MyCircularQueue* obj) {

return (obj->rear +1) % (obj->k+1) == (obj->front);
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
if (!myCircularQueueIsFull(obj))
{
    obj->a[obj->rear] = value;
    obj->rear = (++obj->rear % (obj->k+1));//rear++之后下標(biāo)不能大于k+1,所以%(k+1)
    return true;
}
return false;
}


bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    if(myCircularQueueIsEmpty(obj))
    {
        return false;
    }
    else
    {
        ++obj->front;
        obj->front = obj->front%(obj->k+1);
        return true;
    }
}

int myCircularQueueFront(MyCircularQueue* obj) {

     if(!myCircularQueueIsEmpty(obj))
{
    return obj->a[obj->front];
}
else
{
    return -1;
}
}

int myCircularQueueRear(MyCircularQueue* obj) {
    if(!myCircularQueueIsEmpty(obj))
    {
        return obj->a[(obj->rear-1 + obj->k+1)%(obj->k+1)] ;
        //rear--之后下標(biāo)不能小于0
    }
    else
    {
        return -1;
    }
}

void myCircularQueueFree(MyCircularQueue* obj) {
free(obj->a);
obj->a = NULL;
free(obj);
obj = NULL;
}

數(shù)據(jù)結(jié)構(gòu)與算法系列之習(xí)題練習(xí)
數(shù)據(jù)結(jié)構(gòu)與算法系列之習(xí)題練習(xí)文章來源地址http://www.zghlxwxcb.cn/news/detail-514664.html

到了這里,關(guān)于數(shù)據(jù)結(jié)構(gòu)與算法系列之習(xí)題練習(xí)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 【數(shù)據(jù)結(jié)構(gòu)】第二章課后練習(xí)題——線性結(jié)構(gòu)

    【數(shù)據(jù)結(jié)構(gòu)】第二章課后練習(xí)題——線性結(jié)構(gòu)

    1、線性表是 一個有限序列,可以為空 2、鏈表中最常用的操作是在最后一個元素之后插入一個元素和刪除最后一個元素,則采用 單循環(huán)鏈表 存儲方式最節(jié)省運算時間 3、若某線性表中最常用的操作實在最后一個元素之后插入一個元素和刪除第一個元素,則采用 僅有尾結(jié)點的

    2024年02月07日
    瀏覽(25)
  • 【數(shù)據(jù)結(jié)構(gòu)】“單鏈表”的練習(xí)題

    【數(shù)據(jù)結(jié)構(gòu)】“單鏈表”的練習(xí)題

    ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???? ?? ?? ?? 個人主頁 :阿然成長日記 ??點擊可跳轉(zhuǎn) ?? 個人專欄: ??數(shù)據(jù)結(jié)構(gòu)與算法??C語言進(jìn)階 ?? 不能則學(xué),不知則問,恥于問人,決無長進(jìn) ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? 題目要求: 給你單鏈

    2024年02月14日
    瀏覽(21)
  • 數(shù)據(jù)結(jié)構(gòu)——二叉樹練習(xí)題

    數(shù)據(jù)結(jié)構(gòu)——二叉樹練習(xí)題

    目錄 單值二叉樹? 相同的樹? 另一棵樹的子樹 二叉樹的前序遍歷 ?二叉樹的構(gòu)造及遍歷 給大家推薦一款刷題,找工作的好網(wǎng)站——??途W(wǎng) 牛客網(wǎng) - 找工作神器|筆試題庫|面試經(jīng)驗|實習(xí)招聘內(nèi)推,求職就業(yè)一站解決_牛客網(wǎng) ? 思路:根節(jié)點跟左子樹比較,若相等則繼續(xù)比,一

    2024年02月11日
    瀏覽(22)
  • 【數(shù)據(jù)結(jié)構(gòu)(四)】鏈表經(jīng)典練習(xí)題

    【數(shù)據(jù)結(jié)構(gòu)(四)】鏈表經(jīng)典練習(xí)題

    ?博主主頁: 33的博客? ??文章專欄分類:數(shù)據(jù)結(jié)構(gòu)?? ??我的代碼倉庫: 33的代碼倉庫?? ?????? 關(guān)注我?guī)銓W(xué)更多數(shù)據(jù)結(jié)構(gòu)知識 在上一篇文章中博主已經(jīng)介紹了鏈表的基礎(chǔ)知識,什么是鏈表,如何實現(xiàn)一個鏈表,以及LinkedList的操作方法,那么在這篇文章中通過一些鏈

    2024年04月22日
    瀏覽(23)
  • 【數(shù)據(jù)結(jié)構(gòu)】“單鏈表”的練習(xí)題(二)

    【數(shù)據(jù)結(jié)構(gòu)】“單鏈表”的練習(xí)題(二)

    ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???? ?? ?? ?? 個人主頁 :阿然成長日記 ??點擊可跳轉(zhuǎn) ?? 個人專欄: ??數(shù)據(jù)結(jié)構(gòu)與算法??C語言進(jìn)階 ?? 不能則學(xué),不知則問,恥于問人,決無長進(jìn) ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? 前言: 最近在刷題的

    2024年02月13日
    瀏覽(28)
  • 【數(shù)據(jù)結(jié)構(gòu)】“單鏈表”的練習(xí)題(一)

    【數(shù)據(jù)結(jié)構(gòu)】“單鏈表”的練習(xí)題(一)

    ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???? ?? ?? ?? 個人主頁 :阿然成長日記 ??點擊可跳轉(zhuǎn) ?? 個人專欄: ??數(shù)據(jù)結(jié)構(gòu)與算法??C語言進(jìn)階 ?? 不能則學(xué),不知則問,恥于問人,決無長進(jìn) ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? 題目要求: 給你單鏈

    2024年02月12日
    瀏覽(30)
  • 力扣(LeetCode)數(shù)據(jù)結(jié)構(gòu)練習(xí)題(2)

    力扣(LeetCode)數(shù)據(jù)結(jié)構(gòu)練習(xí)題(2)

    今天又寫了兩道關(guān)于鏈表的練習(xí)題,來給大家分享一下。鞏固一下上一篇學(xué)到的鏈表知識,題目可以然我們更清楚的認(rèn)識鏈表。 目錄 給你單鏈表的頭節(jié)點?head?,請你反轉(zhuǎn)鏈表,并返回反轉(zhuǎn)后的鏈表 給你單鏈表的頭結(jié)點?head?,請你找出并返回鏈表的中間結(jié)點。如果有兩個中

    2024年02月21日
    瀏覽(27)
  • 【數(shù)據(jù)結(jié)構(gòu)】順序表詳解(附leetcode練習(xí)題)

    【數(shù)據(jù)結(jié)構(gòu)】順序表詳解(附leetcode練習(xí)題)

    ??個人主頁:fighting小澤 ??作者簡介:目前正在學(xué)習(xí)C語言和數(shù)據(jù)結(jié)構(gòu) ??博客專欄:數(shù)據(jù)結(jié)構(gòu) ???歡迎關(guān)注:評論????點贊????留言???? 線性表(linear list)是n個具有相同特性的數(shù)據(jù)元素的有限序列。 線性表是一種在實際中廣泛使用的數(shù)據(jù)結(jié)構(gòu),常見的線性表:順

    2023年04月27日
    瀏覽(23)
  • 數(shù)據(jù)結(jié)構(gòu)之鏈表練習(xí)與習(xí)題詳細(xì)解析

    數(shù)據(jù)結(jié)構(gòu)之鏈表練習(xí)與習(xí)題詳細(xì)解析

    個人主頁:點我進(jìn)入主頁 專欄分類:C語言初階? ? ??C語言程序設(shè)計————KTV? ? ? ?C語言小游戲? ? ?C語言進(jìn)階 C語言刷題? ? ? ?數(shù)據(jù)結(jié)構(gòu)初階 歡迎大家點贊,評論,收藏。 一起努力,一起奔赴大廠。 目錄 1.前言 2.習(xí)題解析 2.1習(xí)題一 2.2習(xí)題二 2.3習(xí)題三 2.4習(xí)題四 2.

    2024年02月05日
    瀏覽(29)
  • 【數(shù)據(jù)結(jié)構(gòu)】時間復(fù)雜度---OJ練習(xí)題

    【數(shù)據(jù)結(jié)構(gòu)】時間復(fù)雜度---OJ練習(xí)題

    目錄 ??時間復(fù)雜度練習(xí) ??面試題---消失的數(shù)字 題目描述 題目鏈接:面試題 17.04. 消失的數(shù)字 ??解題思路 ??思路1: malloc函數(shù)用法? ??思路2: ??思路3: ???如果有不了解時間復(fù)雜度的請移步上一篇文章:【數(shù)據(jù)結(jié)構(gòu)】初識 題目描述 數(shù)組 nums 包含從 0 到 n 的所有整數(shù),

    2024年02月16日
    瀏覽(24)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包