棧操作之棧鏈

來源:互聯網
上載者:User

標籤:棧鏈

資料結構:

棧是允許在同一端進行插入和刪除操作的特殊線性表。允許進行插入和刪除操作的一端稱為棧頂(top),另一端為棧底(bottom);棧底固定,而棧頂浮動;棧中元素個數為零時稱為空白棧。插入一般稱為進棧(PUSH),刪除則稱為退棧(POP)。棧也稱為後進先出表。

作業系統:

由編譯器自動分配釋放 ,存放函數的參數值,局部變數的值等。其操作方式類似於資料結構中的棧

棧使用的是一級緩衝, 他們通常都是被調用時處於儲存空間中,調用完畢立即釋放。

棧鏈模板代碼:

#define TRUE 1#define FALSE 0typedef struct node{StackElementType data;struct node *next;}LinkStackNode;typedef LinkStackNode *LinkStack;/*進棧操作。*/int Push(LinkStack top, StackElementType x)/* 將資料元素x壓入棧top中 */ {LinkStackNode *temp;temp=(LinkStackNode *)malloc(sizeof(LinkStackNode)); if(temp==NULL)   return(FALSE);   /* 申請空間失敗 */temp->data=x;temp->next=top->next;top->next=temp;   /* 修改當前棧頂指標 */ return(TRUE);}/*出棧操作。*/int Pop(LinkStack top, StackElementType *x){  /* 將棧top的棧頂元素彈出,放到x所指的儲存空間中 */LinkStackNode * temp;temp=top->next;if(temp==NULL)  /*棧為空白*/return(FALSE);top->next=temp->next;*x=temp->data;free(temp);   /* 釋放儲存空間 */return(TRUE);}

棧操作之棧鏈

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.