溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)中的棧該怎么理解

發(fā)布時(shí)間:2022-01-04 00:22:55 來(lái)源:億速云 閱讀:118 作者:柒染 欄目:開發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)中的棧該怎么理解,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

    棧的鏈?zhǔn)綄?shí)現(xiàn)

    主要內(nèi)容

    (1) 棧包含7個(gè)元素,依次是67,3,88,6,1,7,0,采用尾插入法創(chuàng)建 棧,為該棧設(shè)置兩個(gè)指針,一個(gè)bottom和一個(gè)top指針;

    (2) 入棧函數(shù)push,該函數(shù)完成向棧中插入元素的功能,利用push函數(shù),將數(shù)字-9插入到棧內(nèi),并將棧里的元素遍歷;

    (3) 出棧函數(shù)pop,該函數(shù)完成從棧中刪除元素的功能,利用pop函數(shù),刪除此時(shí)棧里面的3個(gè)元素,并遍歷棧;

    (4) 函數(shù)length,求出此時(shí)棧內(nèi)元素的個(gè)數(shù)。

    代碼實(shí)現(xiàn):

    #include<stdio.h>
    #include<stdlib.h>
    struct node
    {
    	int date;
    	struct node *next;
    };
    struct stack
    {
    	struct node *bottom;
    	struct node *top;
    }s;
    struct stack *creat(struct stack *s);              //創(chuàng)建棧
    void  push(struct stack *s,int e);   //入棧
    void print(struct stack *s);       //打印輸出
    void pop(struct stack *s);         //出棧
    void length(struct stack *s);      //輸出棧的長(zhǎng)度
    int main()
    {
    	struct stack *s;
    	int e;
    	s=creat(s);
    	push(s,67);
    	push(s,3);
        push(s,88);
        push(s,6);
        push(s,1);
        push(s,7);
        push(s,0);
        printf("初始棧元素為:");
        print(s);
        printf("\n");
        printf("\n");
        push(s,-9);
        printf("插入元素后:");
        print(s);
        printf("\n");
        printf("\n");
        pop(s);
        pop(s);
        pop(s);
        printf("刪除元素后:");
        print(s);
        printf("\n");
        printf("\n");
        length(s);
    	return 0;
    }
    struct stack *creat(struct stack *s)
    {
    	s=(struct stack *)malloc(sizeof(struct stack ));
    	s->bottom=s->top=(struct node *)malloc(sizeof(struct node));
    	s->top->next=NULL;
        s->bottom->next=NULL;
    	return s;
    }
    void  push(struct stack *s,int e)//進(jìn)棧
    {
        struct node *p;
        p=(struct node *)malloc(sizeof(struct node));
    	p->date=e;
    	p->next=NULL;
    	s->top->next=p;
    	s->top=p;
    }
    void pop(struct stack *s)// 出棧
    {
       struct node *p,*q;
       p=s->bottom;
       while(p->next!=NULL)
         {
              q=p;
              p=p->next;
              
         }
        q->next=NULL;
        s->top=q;
    }
    void print(struct stack *s)//打印輸出
    {
        struct node *p = s->bottom->next;
    	while(p!=NULL)
    	{
    		printf("%4d",p->date);
    		p=p->next;
    	}
    }
    
    void length(struct stack *s)//計(jì)算長(zhǎng)度
    {
       struct node *p=s->bottom->next;
       int i=0;
       while(p!=NULL)
        {
            i++;
            p=p->next;
        }
       printf("此時(shí)棧的長(zhǎng)度為:%4d",i);
    }

    上述就是小編為大家分享的C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)中的棧該怎么理解了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

    向AI問(wèn)一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI