『壹』 《大話數據結構》中,關於c語言實現鏈棧程序的一些困惑
這破程序啊,或許是程序問題。你的理解是對的吧。
『貳』 C語言數據結構中鏈棧的問題
這個鏈棧應該就是一個用鏈表弄的後進先出的棧結構。top指針永遠指向棧的最上面的那個節點。
這個函數是新加一個節點到這個棧中,首先分配了空間給s,s是要新加入這個棧的那個節點。
s->next=top;就是讓s指向棧的最上面的那個元素。top=s;因為現在最上面的節點是s了,所以讓top指向s,使s成為棧頂指針。返回的就是棧的頂上那個節點的指針。這樣循環調用這個函數就可以不斷的添加新節點,加入的新節點總是指向前面的那個節點,同時新加入的節點成為頭節點。
『叄』 數據結構鏈棧與C語言
include<iostream.h>
struck
link
{
int
data;
link
*next;
};
//前面定義一個結構體
這個結構體有2個部分組成
一個是DATA
一個是指針
要理解好這個指針
後面就好理解
(可以理解這個指針指向另一個這個結構體)
int
gettop(link
*top)//這個是取頭元素
{
if(top->next!=NULL)//這個是判斷是否是空的
return(top->next->data);//這個是取最上頭的元素的值
else
//假如是空的
return
(NULL);//就會返回NULL
}
『肆』 急!用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語言數據結構中的鏈棧中有頭結點與無頭結點的各種操作上有什麼不同
無頭節點
你是無法將信息插入它前面的(單鏈表)
比如
學號
234存在
現在要插入一個學號1的
插不進去
而有頭結點的
head.next
=
1;
1.next
=
2
『陸』 數據結構實驗(用c語言寫) 棧的基本操作
//順序棧
#include
#include
#include
#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)
/*進棧函數,將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)/*出棧函數,若棧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;i
#include
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的區別,bool只有一個位元組,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請輸入要進棧的元素個數是:");
scanf("%d",&a);
printf("\n請輸入要進棧的%d個元素:",a);
for(b=0;b
評論
0
0
載入更多
『柒』 編程/C語言/數據結構鏈棧!
你得把全部代碼傳過來才好看的
『捌』 關於數據結構(c語言)鏈棧的問題
你的while循環寫錯了,你想藉助S遍歷,但是循環條件用的還是top,而top在循環內部沒有修改,所以就進入死循環了,應該這么改:
while(S!=NULL) //用S
{
printf("%d,\n",S->data);
S=S->next; //S要修改
}
還有,print函數應該是不想修改鏈棧的吧,只是讀取。所以參數就不要用a的地址了,直接用a傳遞就行了,保證函數體內不會意外修改a。函數參數要做相應的修改。
像這樣:都不用額外藉助一個S了。
void print(coffee *top)
{
top=top->next;
while(top!=NULL)
{
printf("%d,\n",top->data);
top=top->next; //top要修改
}
}
用print(a);調用就好了。
有問題Hi聯系。
『玖』 C語言數據結構鏈式棧問題
//既然是演算法就不用源碼了具體看注釋
typedefintDatatype;
typedefstructqueuenode
{
Datatypedata;
structqueuenode*next;
}QueueNode;//以上是結點類型的定義
typedefstruct
{
queuenoderear;
}LinkQueue;//只設一個指向隊尾元素的指針
voidInitQueue(LinkQueue
『拾』 C語言數據結構實現鏈棧的入棧、出棧、刪除與插入
1、棧(stack)又名堆棧,它是一種運算受限的線性表。
其限制是僅允許在表的一端進行插入和刪除運算。這一端被稱為棧頂,相對地,把另一端稱為棧底。向一個棧插入新元素又稱作進棧、入棧或壓棧,它是把新元素放到棧頂元素的上面,使之成為新的棧頂元素;從一個棧刪除元素又稱作出棧或退棧,它是把棧頂元素刪除掉,使其相鄰的元素成為新的棧頂元素。
2、常式:
#include<stdio.h>
#include<stdlib.h>
#defineMax100
typedefcharT;
typedefstructMyStack
{
Taa[Max];
unsignedintp;
}stack;
//創建空棧
stack*createEmptyStack()
{
stack*st=(stack*)malloc(sizeof(stack));
inti=0;
for(i=0;i<Max;i++)
st->aa[i]=0;
st->p=0;
returnst;
};
//棧判空
intisEmpty(conststack*st)
{
if(st->p==0)return1;
elsereturn0;
};
//求棧的大小
unsignedintsize(conststack*st)
{
returnst->p;
};
//push操作
voidpush(stack*st,constTa)
{
st->p=st->p+1;
if(st->p==Max)
{
printf("棧滿 ");
st->p--;
return;
}
st->aa[st->p]=a;
};
//pop操作
Tpop(stack*st)
{
if(isEmpty(st))
{
printf("棧空");
returnNULL;
}
chart=st->aa[st->p];
st->p=st->p-1;
printf("%c",t);
returnt;
};
//棧銷毀
voiddestroy(stack*st)
{
free(st);
};
intmain()
{
stack*st=createEmptyStack();
if(isEmpty(st))printf("MyStackisempty ");
elseprintf("MyStackisnotempty ");
push(st,'a');
push(st,'b');
push(st,'c');
push(st,'d');
push(st,'e');
printf("%d ",size(st));
while(!isEmpty(st))pop(st);
destroy(st);
system("pause");
return0;
}