C栈和队列的基本操作

#include<stdio.h>
#include<stdlib.h>
typedef struct stack{
        struct node *base;
        struct node *top;
        int stacklen;
}stack;

typedef struct node{
        int data;
        struct node *last;
}node;

void init(stack *p)
{
        node *n=(node *)malloc(sizeof(node));
        n->last=NULL;
        p->base=n;
        p->top=n;
        p->stacklen=0;
}
void push(stack *p)
{
        int num,i,data;
        printf("input num\n");
        scanf("%d",&num);
        for(i=0;i<num;++i)
        {
                printf("input data\n");
                scanf("%d",&data);
                node *n=(node *)malloc(sizeof(node));
                n->data=data;
                n->last=p->top;
                p->top=n;
                p->stacklen+=1;
        }
}

void pop(stack *p)
{
        printf("stack length is %d\n",p->stacklen);
        while((p->top)->last)
        {
                printf("%d->",(p->top)->data);
                p->top=(p->top)->last;
                p->stacklen-=1;
        }

        printf("end\nstack length is %d\n",p->stacklen);

}
int main()
{
        stack p;
        init(&p);
        push(&p);
        pop(&p);
}
结果:
sun@ubuntu:~/project/datastructure$ ./stack
input num
4
input data
1
input data
2
input data
3
input data
4
stack length is 4
4->3->2->1->end
stack length is 0

#include<stdio.h>
#include<stdlib.h>
typedef struct queue{
        struct node *head;
        struct node *end;
}queue;
typedef struct node{
        struct node *next;
        int data;
}node;
void init(queue *p)
{
        p->head=NULL;
        p->end=NULL;
}

void enqueue(queue *p)
{
        int data,num;
        printf("input num\n");
        scanf("%d",&num);
        for(int i=0;i<num;++i)
        {
                printf("input data\n");
                scanf("%d",&data);
                node *newnode=(node *)malloc(sizeof(node));
                newnode->data=data;
                newnode->next=NULL;
                if(p->head==NULL && p->end==NULL)
                {
                        p->head=newnode;
                        p->end=newnode;
                        continue;
                }
                (p->end)->next=newnode;
                p->end=newnode;
        }
}

void dequeue(queue *p)
{
        while(1)
        {
                if(p->head==p->end)
                {
                        printf("%d->end\n",(p->head)->data);
                        p->head=NULL;
                        p->end=NULL;
                        break;
                }
                printf("%d->",(p->head)->data);
                p->head=(p->head)->next;
        }
}
int main()
{
        queue p;
        init(&p);
        enqueue(&p);
        dequeue(&p);
        return 0;
}
结果:
sun@ubuntu:~/project/datastructure$ ./queue
input num
7
input data
1
input data
2
input data
3
input data
4
input data
5
input data
6
input data
7
1->2->3->4->5->6->7->end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值