c语言中的栈,队列
之前只是简单了解了栈和队列,栈后进先出,先进后出,就像乘坐
电梯,最先进来的人是最后一个出去的。之前也用栈的思想做过简
单的题,例如括号配对,回文字符串。而队列是先进先出,就跟排
队一样,这些都是之前了解到。这次初步学习栈和队列。
栈
之前用栈的思想做过的简单的题,基本是数组解决
1、回文字符串
int main()
{
char s[100];
int a[100],top=0,i,mid;
scanf("%s",s);
mid=strlen(s)/2-1;
for(i=0;i<=mid;i++)
a[top++]=s[i];
if(strlen(s)%2)
i=mid+2;
else
i=mid+1;
for(;i<strlen(s);i++)
{
if(a[top-1]==s[i])
top--;
else
break;
}
if(top==0)
printf("yes\n");
else
printf("no");
return 0;
}
2、括号配对
int main()
{
char a[1000],s[1000];
scanf("%s",a);
int n,i;
n=strlen(a);
int top=0;
for(i=0;i<n;i++)
{
if((top>0&&s[top-1]=='('&&a[i]==')')||(top>0&&s[top-1]=='['&&a[i]==']')
||(top>0&&s[top-1]=='{'&&a[i]=='}'))
{
top--;
}
else
{
s[top]=a[i];
top++;
}
}
if(top==0)
printf("true");
else
printf("false");
return 0;
}
1.栈(stack)又名堆栈,它是一种运算受限的线性表。其限制是仅允
许在表的一端进行插入和删除运算。这一端被称为栈顶,相对地,
把另一端称为栈底。
2.栈中有两个指针,栈顶指针和栈尾指针。栈尾指针不动。用栈顶
指针等于栈尾指针来判定栈是否为空。压栈时,将栈顶指针+1,然
后将数据存入栈顶指针所指向的位置。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//栈的声明
typedef struct
{
char data[10];
int top;
int bottom;
}stack;
//栈的创建
stack *Stackcreat()
{
stack *p=(stack*)malloc(sizeof(stack));
p->top =p->bottom =0;
return p;
}
//入栈
void StackInput(stack *p,char str)
{
p->data [p->top ]=str;
p->top ++;
}
//出栈,栈顶的内容输出
char StackOutput(stack *p,char str)
{
if(p->top !=p->bottom )
{
str=p->data [p->top -1];
p->top --;
return str;
}
}
//栈的遍历
void StackPrint(stack *p)
{
while(p->top !=p->bottom )
{
printf("%c",p->data [p->top-1]);
p->top --;
}
}
int main()
{
//压入单个字符
char str;
stack *p;
p=Stackcreat();
StackInput(p,'a');
str=StackOutput(p,str);
printf("%c\n",str);
//压入字符串
stack *p1;
char str1[10]="abcdefg";
p1=Stackcreat();
for(int i;i<strlen(str1);i++)
StackInput(p1,str1[i]);
StackPrint(p1);
return 0;
}
输出结果:a
gfedcba
队列
一种操作受限的线性表,其限制在表的一端进行插入,另一端进行删除。可进行插入的一端称为队尾(rear),可进行删除的一端称为队头(front)。向队中插入元素叫入队,新元素进入之后就称为新的队尾元素。从队中删除元素叫出队,元素出队后,其后继结点元素就称为新的队头元素。
特点:先进先出
#include <stdio.h>
#include <stdlib.h>
//定义 节点
typedef struct node
{
char data;
struct node *next;
}node;
//定义队列
typedef struct quene
{
node *front;
node *rear;
}Quene;
//初始化队列
Quene *QueneCreat(void)
{
Quene *p=(Quene *)malloc(sizeof(Quene));
p->front =p->rear =NULL;
return p;
}
//入队
void QuentInput(Quene *p,char data)
{
node *n=(node*)malloc(sizeof(node));
if(n==NULL)
return;
n->data =data;
n->next =NULL;
if(p->rear==NULL)
{
p->front =n;
p->rear =n;
}
else
{
p->rear->next =n;
p->rear =n;
}
}
//出队
void QueneOutput(Quene *p)
{
node *n=p->front;
if(p->front ==NULL)
return;
if(p->front ==p->rear )
{
p->front =NULL;
p->rear =NULL;
}
else
{
p->front =p->front->next;
free(n);
}
}
//打印队列
void QuenePrint(Quene *p)
{
node *n=(node*)malloc(sizeof(node));
n=p->front;
if(n==NULL)
return;
while(n!=NULL)
{
printf("%c",n->data);
n=n->next;
}
printf("\n");
}
int main()
{
Quene *p;
p=QueneCreat();
// QuentInput(p,'a');
// QuenePrint(p);
// QueneOutput(p);
// QuenePrint(p);
int i;
for(i=0;i<=5;i++)
{
QuentInput(p,'a'+i);
}
QuenePrint(p);
QueneOutput(p);
QuenePrint(p);
return 0;
}
结果:abcdef
bcdef
目前只是初步了解了栈和队列,需要深化和熟练,特别是熟练,之后要学会应用。