鏈棧如何訪問
㈠ 問基礎問題:關於鏈棧的 入棧 出棧 操作 代碼,有幾處不懂
p是你新建立的一個節點,你給他的data部分復制為e,之後它的next指向棧頂指針後再把,棧頂指向p,你如果用p=p->next,就是讓你指會了自己本身。delete p是刪除p所指的節點。所以上面不能刪除。
㈡ c語言數據結構中鏈棧的問題
這個鏈棧應該就是一個用鏈表弄的後進先出的棧結構。top指針永遠指向棧的最上面的那個節點。
這個函數是新加一個節點到這個棧中,首先分配了空間給s,s是要新加入這個棧的那個節點。
s->next=top;就是讓s指向棧的最上面的那個元素。top=s;因為現在最上面的節點是s了,所以讓top指向s,使s成為棧頂指針。返回的就是棧的頂上那個節點的指針。這樣循環調用這個函數就可以不斷的添加新節點,加入的新節點總是指向前面的那個節點,同時新加入的節點成為頭節點。
㈢ 實現鏈式棧的基本操作:入棧、出棧、取棧頂元素、判定棧空、棧滿。
#include<stdio.h>
#include<malloc.h>
structstack
{
intnumber;
structstack*next;
};
structstack*top;//指向棧頂
structstack*p;//臨時指針
//創建鏈棧
voidcreate(intfirst_value)
{
p=(structstack*)malloc(sizeof(structstack));//申請內存空間
p->number=first_value;//給棧底賦值
p->next=NULL;//棧底的指針為空
top=p;//棧頂指向棧底
}
//進棧
voidinput(intvalue)
{
p=(structstack*)malloc(sizeof(structstack));//申請內存空間
p->number=value;//賦值
p->next=top;//指向原來的棧頂
top=p;//棧頂往上移動一位
}
//出棧
voidoutput()
{
p=top;//p=棧頂
top=top->next;//棧頂往下移動一位
free(p);//釋放p
}
//清空棧
voidclear()
{
while(top->next!=NULL)//如果不是棧底
output();//出棧
free(top);//釋放棧頂
}
//取棧頂元素
inttopElement()
{
returntop->number;
}
intmain()
{
inti;
create(0);
for(i=1;i<=10;i++)
input(i);
//clear();
printf(" 棧的內容: ");
for(p=top;;p=p->next)
{
printf("%d ",p->number);
if(p->next==NULL)
break;
}
printf("棧頂元素=%d ",topElement());
}
㈣ 鏈棧如何構造,鏈棧新結點如何進棧
鏈式棧就是用鏈式存儲結構表示一個棧,也就是指針域。
根據棧的性質,知道棧是先進後出,所以只需要設置一個棧頂指針,還是說點C++吧
先構造一個結構模板
template<class
ElemType>
typedef
struct
Node
//建立
{
ElemType
data;//數據成員,用於存儲
struct
Node*next;//指針成員,用於連接邏輯結構
}LNode;
1.構造一個空棧,只需要:
LNode*head;//定義棧頂指針
head=(LNode*)malloc(sizeof(LNode));//分配空間
head->next=NULL;//下一個為空
2.新節點:比如新節點數據位1
入棧操作:
LNode*p;
p->data=1;
p->next=head;
head=p;
總之有點類似C語言里的鏈表
還有什麼不清楚的可以追問
希望對你有所幫助!!!
㈤ 鏈棧的基本操作C語言
https://..com/question/310602180072679244.html
借題 求幫忙做一道題🙏
對不住了
㈥ 編程實現鏈棧的入棧和出棧操作。 在線等著你哦!
#include<stdio.h>
#include<stdlib.h>
typedefstructSnode
{
intdata;/*數據域*/
structSnode*next;/*指針域*/
}SNODE,*LinkStack;/*其中SNODE為鏈棧中的結點類型名,LinkStack為指向結點的指針類型名*/
//////////////////
LinkStackPush(LinkStacktop,inte)
/*將數據元素e壓入到鏈棧top中,使其成為新的棧項元素*/
{
LinkStackp;
p=(LinkStack)malloc(sizeof(SNODE));/*生成一個新的結點*/
if(!p)/*如果分配空間失敗,則函數返回"OVERFLOW"*/
printf("StackisOverflow ");
p->data=e;/*新結點的數據域賦值*/
p->next=top;/*修改鏈使新結點插入到鏈表的頭部,並成為新的棧頂元素*/
top=p;
returntop;
}
/////////////
LinkStackPop(LinkStacktop,int*e)
/*將鏈棧top中的棧頂元素從棧中刪除,並用e返回其值*/
{
LinkStackq;
if(!top)/*如果棧空,則函數返回ERROR*/
printf("StackisERROR ");
*e=top->data;/*將被刪的棧頂元素的值保存在e中*/
q=top;/*用q記下待刪的棧頂元素*/
top=q->next;
/*修改鏈使待刪結點從鏈中"卸下",此時被刪結點的後繼成為新的棧頂元素結點*/
free(q);/*釋放被刪結點的存儲空間*/
returntop;
}
/////////
LinkStackStack_display(LinkStacktop)
{
inte;
while(top)
{
e=top->data;
printf("%4d",e);
top=top->next;
}
returntop;
}
/////////////////////
voidmain()
{
LinkStacktop=0;
inti=0,n,e;
printf("pleaseinputthelength:");/*輸入幾個數*/
scanf("%d",&n);
printf("pleaseinputtheValue: ");/*輸入*/
while(i<n)
{
scanf("%d",&e);
top=Push(top,e);
i++;
}
printf("thestackis: ");
Stack_display(top);
printf("pleaseinputtheinsertnode:");
scanf("%d",&e);
top=Push(top,e);
printf("thestackafterpushis: ");
Stack_display(top);
top=Pop(top,&e);
printf("thepopvalueis:%d ",e);
printf("thestackafterpopis: ");
Stack_display(top);
}
調好了麻煩採納一下
㈦ 急!用c語言實現鏈棧的操作
typedef struct node
{ ElemType data;
struct node *next;
} LinkStack;
⑴置空棧
void InitLinkStack( LinkStack * & s)
{ s=NULL;
}
⑵判棧空
int IsEmptyLinkStack(LinkStack *s )
{ if(s==NULL)
return 1;
else
return 0;
}
⑶ 入棧/*將元素x插入鏈棧top的棧頂*/
void PushLinkStack(LinkStack* &s , ElemType x)
{ LinkStack *p;
p=malloc(sizeof(LinkStack)); /*生成新結點*s */
p->data=x;
p->next=s;
s=p;
}
⑷出棧/*刪除鏈棧top的棧頂結點*/
int PopLinkStack (LinkStack* & s, ElemType &x)
{ LinkStack *p;
if(s==NULL) return 0;
x = s->data; /*將棧頂數據存入*x */
p = s; /*保存棧頂結點地址*/
s = s->next; /*刪除原棧頂結點*/
free (p); /*釋放原棧頂結點*/
return 1; /*返回新棧頂指針*/
}
(5) 取棧頂元素
int GetLinkStackTop (LinkStack* s, ElemType &x)
{
if(s==NULL) return 0;
x = s->data; /*將棧頂數據存入*x */
return 1; /*返回新棧頂指針*/
}
主函數怎麼寫吧
㈧ c++實現鏈棧的基本操作
之前學數據結構的實驗我保存在了CSDN的博客上面,你感興趣可以去看下。上面都有代碼,沒有的。你可以再問,我可以幫你做。80372824http://blog.csdn.net/wenjx08
㈨ 鏈棧的基本操作問題
OK了,下面程序可以編譯,歡迎在線討論;共同進步。。。
#include <stdio.h>
#include <stdlib.h>
typedef struct STACK
{
int data;
struct STACK *next;
}Stack;
Stack* InitStack()
{
Stack *S;
if((S=(Stack *)malloc(sizeof(Stack)))==NULL)
return 0;
S->next=NULL;
return S;
}
void GetTop(Stack *S,int &e)
{
if(S->next==NULL)
{
printf("空棧...\n");
return;
}
e=S->next->data;
}
void Push(Stack *S)
{
int item;
Stack *p;
printf("請輸入要入棧的元素(輸入-1結束):\n");
while(1)
{
scanf("%d",&item);
if(item==-1)
break;
if((p=(Stack *)malloc(sizeof(Stack)))==NULL)
return;
p->data=item;
p->next=S->next;
S->next=p;
}
}
void Pop(Stack *S)
{
if(S->next==NULL)
{
printf("空棧...\n");
return;
}
S->next=S->next->next;
}
void OutputStack(Stack *S)
{
Stack *p;
p=S->next;
if(S->next==NULL)
{
printf("空棧...\n");
return;
}
while(p!=NULL)
{
printf("%4d",p->data);
p=p->next;
}
printf("\n");
}
void main()
{
Stack *S;
//int item;
S=InitStack();
S->next;
Push(S);
OutputStack(S);
//GetTop(&S,item);
//printf("%d\n",item);
//Pop(&S);
//OutputStack(&S);
}