c語言用鏈表寫入棧函數(shù) 鏈表實現(xiàn)棧c語言

如何用C語言創(chuàng)建一個鏈棧,并進行操作

1 思路: 主要是鏈表的插入和刪除操作

創(chuàng)新互聯(lián)公司是一家專業(yè)提供龍灣企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、H5場景定制、小程序制作等業(yè)務(wù)。10年已為龍灣眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進行中。

2 代碼

#includestdio.h

#includestdlib.h

typedef?struct?node

{

int??data;

struct?node?*next;

}node_type;

void?push(node_type*?stack,?int?elem){

node_type*node?=?(node_type*)malloc(sizeof(node_type));

node-data?=?elem;

node-next?=?stack;

stack?=?node;

}

int?pop(node_type*?stack){

int?elem?=?stack-data;

node_type*node?=?stack;

stack?=?stack-next;

free(node);

return?elem;

}

bool?IsEmpty(node_type*?stack){

return?stack?==?NULL;

}

void?display(node_type*stack){

while?(stack){

printf("%d?",?stack-data);

stack?=?stack-next;

}

puts("");

}

void?destroy(node_type*stack){

while?(!IsEmpty(stack)){

pop(stack);

}

}

int?main(){

puts("(1)?建立空鏈棧");

node_type*stack?=?NULL;

puts("\n(2)?調(diào)用進棧函數(shù),將從鍵盤輸入的數(shù)據(jù)元素逐個進棧,輸入0結(jié)束;");

int?num;

scanf("%d",?num);

while?(num?!=?0){

push(stack,?num);

scanf("%d",?num);

}

puts("\n(3)?顯示進棧后的數(shù)據(jù)元素");

display(stack);

puts("\n(4)?調(diào)用兩次出棧函數(shù),顯示出棧后的數(shù)據(jù)元素");

if?(!IsEmpty(stack))

printf("%d\n",?pop(stack));

if?(!IsEmpty(stack))

printf("%d\n",?pop(stack));

destroy(stack);

getchar();

getchar();

return?0;

}

3 運行效果

用C語言中的鏈表如何實現(xiàn)棧?

struct point{

point *last;

int data;

};

int main(){

cin n;

point *top = NULL;

for(int i = 1 ; i = n ; i++){

scanf("%d" , x);

point *p = new point;

p - data = x; //入棧

p - last = top;

top = p; // 將頭指針指向最后一個

}

while (top != NULL){//判斷棧是否為空

cout top - data endl; //輸出棧頂元素

top = top - last; //將頭指針向下移動

}

}

C語言單鏈表實現(xiàn)棧 菜鳥求教

NODE * x = head; NODE * y = 0;

int i = 0;

for(i = 0; i 8; i++) x = x-next;

y = x-next;

x-next = y-next;

free(y);

數(shù)據(jù)結(jié)構(gòu)實驗(用c語言寫) 棧的基本操作

//順序棧

#includestdio.h

#includestdlib.h

#includemalloc.h

#define STACK_INIT_SIZE 100;

#define STACKINCREMENT 10;

typedef struct

{

int *base;

int *top;

int stacksize;

}SqStack;

typedef int ElemType;

int InitStack(SqStack S) //為棧S分配存儲空間,并置S為空棧

{

int size = STACK_INIT_SIZE;

S.base=(int *)malloc(size*sizeof(ElemType));

if(!S.base)

return 0;

S.top=S.base; //置棧S為空棧

S.stacksize=STACK_INIT_SIZE;

return 1;

}

int GetTop(SqStack S,int e) //若棧不空,則用e返回S的棧頂元素

{

if(S.top==S.base) return 0;

e=*(S.top-1);

return 1;

}

int Push(SqStack S, int e) /*進棧函數(shù),將e插入棧S中,并使之成為棧頂元素*/

{ if(S.top-S.base=S.stacksize) /*棧滿,追加存儲空間*/

{

int stackinvrement = STACKINCREMENT;

S.base=(ElemType *) realloc(S.base,(S.stacksize+stackinvrement)*sizeof(ElemType));

if(!S.base)

return 0; /*存儲分配失敗*/

S.stacksize+=STACKINCREMENT;

}

*S.top++=e;

return 1;

}

int Pop(SqStack S,int e)/*出棧函數(shù),若棧S不空,則刪除S的棧頂元素,用e返回其值*/

{ if(S.top==S.base) return 0;

e=*--S.top;

return 1;

}

void OutputStack(SqStack S)

{int *q;

q=S.top-1;

for(int i=0;iS.top-S.base;i++)

{

printf("%3d ",*q);q--;}

}

void main()

{

int a,b,c ;

char m;

SqStack s;

InitStack(s);

printf("請輸入要進棧的元素個數(shù)是:");

scanf("%d",a);

printf("\n請輸入要進棧的%d個元素:",a);

for(b=0;ba;b++) {

scanf("%d",c);

Push(s,c); }

do { printf("\n");

printf("*********** 1.輸出棧的元素**********\n");

printf("*********** 2.取棧頂元素************\n");

printf("*********** 3.刪除棧頂元素**********\n");

printf("*********** 4.退出程序**********\n");

printf("\n請選擇一個字符:");

getchar();

scanf("%c",m);

switch(m) {

case '1': printf("\n輸出的棧為:");

OutputStack(s);

break;

case '2': GetTop(s,c);

printf("\n棧頂元素為:%d",c);

printf("\n輸出的棧為:");

OutputStack(s);

break;

case '3': Pop(s,c);

printf("\n刪除的棧頂元素:%d",c);

printf("\n輸出的棧為:");

OutputStack(s);

printf("\n");

break;

case '4':break;

default: printf("輸入的數(shù)字有錯,請重新選擇!\n"); break;

}

}while(m!='4');

}

//鏈棧

#includestdio.h

#includestdlib.h

typedef struct SNode

{

int data;

struct SNode *next;

}SNode,*LinkStack;

LinkStack top;

LinkStack PushStack(LinkStack top,int x) //入棧

{

LinkStack s;

s=(LinkStack)malloc(sizeof(SNode));

s-data=x;

s-next=top;

top=s;

return top;

}

LinkStack PopStack(LinkStack top) //退棧

{

LinkStack p;

if(top!=NULL)

{

p=top;

top=top-next;

free(p);

printf("退棧已完成\n");

return top;

}

else printf("棧是空的,無法退棧!\n"); return 0;

}

int GetStackTop(LinkStack top) //取棧頂元素

{

return top-data;

}

bool IsEmpty()//bool取值false和true,是0和1的區(qū)別,bool只有一個字節(jié),BOOL為int型,bool為布爾型

{

return top==NULL ? true:false;

}

void Print()

{

SNode *p;

p=top;

if(IsEmpty())

{

printf("The stack is empty!\n");

return;

}

while(p)

{

printf("%d ", p-data);

p=p-next;

}

printf("\n");

}

void main()

{

int x,a,b;

char m;

do { printf("\n");

printf("###############鏈棧的基本操作##################\n");

printf("××××××××1.置空?!痢痢痢痢痢痢痢痢痢羂n");

printf("××××××××2.進?!痢痢痢痢痢痢痢痢痢痢羂n");

printf("××××××××3.退?!痢痢痢痢痢痢痢痢痢痢羂n");

printf("××××××××4.取棧頂元素××××××××\n");

printf("××××××××5.退出程序×××××××××\n");

printf("##############################################\n");

printf("\n請選擇一個字符:");

scanf("%c",m);

switch(m){

case '1':

top=NULL;

printf("\n棧已置空!");

break;

case '2':

printf("\n請輸入要進棧的元素個數(shù)是:");

scanf("%d",a);

printf("\n請輸入要進棧的%d個元素:",a);

for(b=0;ba;b++) {

scanf("%d",x);

top=PushStack(top,x); }

printf("進棧已完成!\n");

printf("\n輸出棧為:");

Print();

break;

case '3':

printf("\n操作之前的輸出棧為:");

Print();

top=PopStack(top);

printf("\n操作過后的輸出棧為:");

Print();

break;

case '4':

printf("\n輸出棧為:");

Print();

if(top!=NULL)

printf("\n棧頂元素是:%d\n",GetStackTop(top));

else

printf("\n棧是空的,沒有元素!");

break;

case '5':break;

default:

printf("\n輸入的字符不對,請重新輸入!");

break;

}

getchar();

}while(m!='5');

}

C語言數(shù)據(jù)結(jié)構(gòu)實現(xiàn)鏈棧的入棧、出棧、刪除與插入

1、棧(stack)又名堆棧,它是一種運算受限的線性表。

其限制是僅允許在表的一端進行插入和刪除運算。這一端被稱為棧頂,相對地,把另一端稱為棧底。向一個棧插入新元素又稱作進棧、入?;驂簵?,它是把新元素放到棧頂元素的上面,使之成為新的棧頂元素;從一個棧刪除元素又稱作出?;蛲藯?,它是把棧頂元素刪除掉,使其相鄰的元素成為新的棧頂元素。

2、例程:

#include?stdio.h

#include?stdlib.h

#define?Max?100

typedef?char?T;

typedef?struct?MyStack

{

T?aa[Max];

unsigned?int?p;

}?stack;

//創(chuàng)建空棧

stack*?createEmptyStack()

{

stack*?st?=?(stack?*)malloc(sizeof(stack));

int?i=0;

for(i=0;iMax;i++)

st-aa[i]=0;

st-p=0;

return?st;????

};

//棧判空

int?isEmpty(const?stack*?st)

{

if(st-p==0)?return?1;

else?????return?0;

};

//求棧的大小

unsigned?int?size(const?stack*?st)

{

return?st-p;

};

//push操作

void?push(stack*?st,const?T?a)

{

st-p=st-p+1;

if(st-p==Max)

{

printf("棧滿\n");

st-p--;

return;

}

st-aa[st-p]=a;????

};

//pop操作

T?pop(stack*?st)

{

if(isEmpty(st))

{

printf("???);

return?NULL;

}

char?t=st-aa[st-p];

st-p=st-p-1;?

printf("%c?",t);

return?t;?????

};

//棧銷毀

void?destroy(stack*?st)

{

free(st);

};

int?main()

{

stack*?st?=?createEmptyStack();

if(isEmpty(st))?printf("MyStack?is?empty\n");

else?printf("MyStack?is?not?empty\n");

push(st,'a');

push(st,'b');

push(st,'c');

push(st,'d');

push(st,'e');?

printf("%d\n",size(st));

while(!isEmpty(st))?pop(st);

destroy(st);

system("pause");

return?0;

}

用鏈表存儲堆棧內(nèi)容,出棧入棧函數(shù)~~我快崩潰了!

1、main函數(shù)默認是int,需要一個return 0;

改成void,少一個警告。

void main()

2、typedef應(yīng)用錯誤。

typedef struct node * list_pointer;

typedef struct node{

int key;

list_pointer *link;

};改成:

typedef struct node * list_pointer;

struct node{

int key;

list_pointer link;//已經(jīng)是指針

};

3、list_pointer已經(jīng)被定義為指針,不用再次指針:

main中:

lp=(list_pointer *)malloc(sizeof(node));

ptr=(list_pointer *)malloc(sizeof(node));

改為:

lp=(list_pointer)malloc(sizeof(node));

ptr=(list_pointer)malloc(sizeof(node));

add中:

p=(list_pointer *)malloc(sizeof(node));//因為你還沒有定義

改為

list_pointer p=(list_pointer)malloc(sizeof(node));

4、null在C中沒定義,只能用NULL,這是預(yù)定義過的

if(b-link==null)return(null);

——〉if(b-link==NULL)return(NULL);

5、deletes中p沒有定義:

else

{

list_pointer p;

p=b-link;

x=p-key;

b-link=p-link;

free(p);

return(x);

}

6、deletes定義返回類型不對,應(yīng)該為:

int deletes(list_pointer b);

7、如今是沒錯了,但是算法本身就有不可原諒的錯誤!

#includestdio.h

#includestdlib.h

#define Max 100

typedef struct node * list_pointer;

struct node{

int key;

list_pointer link;

};

void add(list_pointer a,int n);

int deletes(list_pointer b);

void main()

{

// int i;

list_pointer ptr,lp;

lp=(list_pointer)malloc(sizeof(node));

ptr=(list_pointer)malloc(sizeof(node));

add(ptr,5);

printf("%d",deletes(ptr));

free(ptr);

add(lp,6);

printf("%d",deletes(lp));

free(ptr);

}

void add(list_pointer a,int n){

list_pointer p=(list_pointer)malloc(sizeof(node));

p-key=n;

p-link=a-link;

a-link=p;

}

int deletes(list_pointer b){

int x;

if(b-link==NULL)return(NULL);

else

{

list_pointer p;

p=b-link;

x=p-key;

b-link=p-link;

free(p);

return(x);

}

}

當前文章:c語言用鏈表寫入棧函數(shù) 鏈表實現(xiàn)棧c語言
文章轉(zhuǎn)載:http://muchs.cn/article26/dooogcg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作定制開發(fā)、全網(wǎng)營銷推廣、外貿(mào)建站網(wǎng)站設(shè)計公司、動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)