棧的C語言實現

來源:互聯網
上載者:User
文章目錄
  • 數組實現
  • 參數傳值實現
  • 引用實現
  • 全域變數實現
  • 另一種數組實現

棧的C語言實現

簡述

棧是實現後進先出(FIFO)策略的一種基本資料結構。我們可以通過多種方式實現棧這種資料結構。

我們可以用下面的結構體來定義棧:

typedef struct stack {

         inttop;

         intkey[M];

} stack;

棧的一個屬性s.top指向最新插入的元素。棧的另外一個屬性s.key[]存放的是元素的關鍵字,包含的元素為s.key[1…s.top],其中s.key[1]是棧底元素,s.key[s.top]是棧頂元素。當s.top = 0時,棧空。

代碼實現

下面程式省略了對上溢和下溢的檢查。main()函數中的語句主要是棧的初始化和測試。

數組實現

最簡單的方法也許就是數組了,不用考慮參數傳遞問題。

原始碼如下:

# include <stdio.h>

# define M 100

int stackEmpty(int s[]);

void push(int s[], int x);

int pop(int s[]);

int main(void)

{

         ints[M];

         s[0]= 0;

         printf("stackEmpty- %d\n", stackEmpty(s));

         push(s,2);

         push(s,5);

         printf("stackEmpty- %d\n", stackEmpty(s));

         printf("pop- %d\n", pop(s));

         return0;

}

int stackEmpty(int s[])

{

         if(s[0]== 0) {

                   return1;

         }else {

                   return0;

         }

}

void push(int s[], int x)

{

         s[0]++;

         s[s[0]]= x;

}

int pop(int s[])

{

         if(s[0]== 0) {

                   return-1;

         }else {

                   returns[s[0]--];

         }

}

參數傳值實現

注意:在參數傳遞的過程中使用的是地址

原始碼如下:

# include <stdio.h>

# define M 100

typedef struct stack {

         inttop;

         intkey[M];

} stack;

int stackEmpty(stack s);

void push(stack * s, int x);

int pop(stack * s);

int main(void)

{

         stacks;

         s.top= 0;

         /*測試代碼 */

         push(&s,2);

         push(&s,3);

         printf("pop- %d\n", pop(&s));

         printf("stackEmpty- %d", stackEmpty(s));

}

int stackEmpty(stack s)

{

         if(s.top== 0) {

                   return1;

         }else {

                   return0;

         }

}

void push(stack * sp, int x)

{

         sp-> top += 1;

         sp-> key[sp -> top] = x;

}

int pop(stack * sp)

{

         if(sp-> top == 0) {

                   return-1;

         }else {

                   sp-> top--;

                   returnsp -> key[sp -> top + 1];

         }

}

引用實現

引用很好用,可惜C語言裡沒有。

原始碼如下:

# include <stdio.h>

# define M 100

typedef struct stack {

         inttop;

         intkey[M];

} stack;

int stackEmpty(stack s);

void push(stack & s, int x);

int pop(stack & s);

int main(void)

{

         stacks;

         s.top= 0;

         /*測試代碼 */

         push(s,2);

         push(s,3);

         printf("pop- %d\n", pop(s));

         printf("stackEmpty- %d", stackEmpty(s));

}

int stackEmpty(stack s)

{

         if(s.top== 0) {

                   return1;

         }else {

                   return0;

         }

}

void push(stack & s, int x)

{

         s.top+= 1;

         s.key[s.top]= x;

}

int pop(stack & s)

{

         if(s.top== 0) {

                   return-1;

         }else {

                   s.top--;

                   returns.key[s.top + 1];

         }

}

全域變數實現

全域變數可以一直儲存變數的值,不會因某個函數的結束而銷毀,並且不用作為參數傳遞給函數。

原始碼如下:

# include <stdio.h>

# define M 100

typedef struct stack {

         inttop;

         intkey[M];

} stack;

stack s;

int stackEmpty(void);

void push(int x);

int pop(void);

int main(void)

{

         s.top= 0;

         /*測試代碼 */

         push(2);

         push(3);

         printf("pop- %d\n", pop());

         printf("stackEmpty- %d", stackEmpty());

}

int stackEmpty(void)

{

         if(s.top== 0) {

                   return1;

         }else {

                   return0;

         }

}

void push(int x)

{

         s.top+= 1;

         s.key[s.top]= x;

}

int pop(void)

{

         if(s.top== 0) {

                   return-1;

         }else {

                   s.top--;

                   returns.key[s.top + 1];

         }

}

另一種數組實現

第一種數組實現的時候使用s[0]表示s.top,如果定義一個新變數stop,可以讓程式變得更加簡單易讀。

原始碼如下:

# include <stdio.h>

# define M 100

int s[M];

int stop;

//stop = 0;    //外部變數不能賦值嗎??

int stackEmpty(void);

void push(int x);

int pop();

int main(void)

{

         stop= 0;

         printf("stackEmpty- %d\n", stackEmpty());

         push(3);

         printf("stackEmpty- %d\n", stackEmpty());

         push(5);

         //printf("pop- %d - %d\n", pop(), pop());    //這是一個未定義行為

         printf("pop- %d\n", pop());

         printf("pop- %d\n", pop());

         printf("stackEmpty- %d\n", stackEmpty());

         return0;

}

int stackEmpty(void)

{

         if(stop== 0) {

                   return1;

         }else {

                   return0;

         }

}

void push(int x)

{

         s[++stop]= x;

}

int pop(void)

{

         returns[stop--];

}

小記

作為一種基本的資料結構,棧的實現還是挺簡單的。

(全文完)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.