zoukankan      html  css  js  c++  java
  • 用链表实现栈和队列

    #include<stdio.h>
    #include
    <stdlib.h>
    #define N 10

    typedef
    struct node {
    char val;
    struct node *next;
    } link,
    *plink;
    int i=0,j=0;
    char a[N]="I am boy!";

    link
    *make_node(int n)
    {
    link
    *p=NULL;
    if ((p = (link *) malloc(sizeof(link))) == NULL) {
    printf(
    "error!");
    return NULL;
    }
    p
    ->next = NULL;
    p
    ->val=n;
    return p;
    }
    //头查
    /*
    void insert1(plink *head,link* p)
    {

    p ->next=*head;
    *head=p;
    }
    */
    //进栈
    /*
    void push (plink *head,link*p){

    insert1(head,p);
    }
    */
    //尾查
    void insert2(plink *head,link*p)
    {
    plink s
    =*head;
    if(*head==NULL)
    *head=p;
    else
    {
    while(s->next)
    s
    = s->next;
    s
    ->next=p;

    }
    }
    //进队
    void enqueue(plink*head,link*p){

    insert2(head,p);
    }
    //出队
    link*dequeue(plink*head){

    plink p
    =NULL;
    if(NULL==*head)return NULL;
    p
    =*head;
    *head=(*head)->next;
    return p;

    }
    //出栈
    /*
    link* pop(plink *head){

    plink *p=NULL;
    if(NULL==*head)return NULL;
    p=head;
    *head=(*head)->next;
    return *p;
    }
    */
    //判空
    int is_empty(plink*head){

    return *head==NULL;
    }
    //查询
    /*
    link* serach(plink head,int key){

    plink p;
    for (p = head; p; p = p->next)
    if (p->val == key)
    return p;
    return NULL;
    }
    */
    //遍历
    void traverse(void (*visit)(plink),plink head)

    {

    plink p;
    for (p = head; p; p = p->next)
    visit(p);
    }
    void visit(plink p)
    {
    printf(
    "%c",p->val);
    }
    /*
    //删除
    void delete(plink *head,plink p)

    {
    plink pre;
    if (p == *head)
    {
    *head = p->next;
    return;
    }
    for (pre = *head; pre; pre = pre->next)
    if (pre->next == p)
    {
    pre->next = p->next;
    return;
    }

    }
    //释放
    void destroy(plink *head)
    {
    plink q, p = *head;
    *head = NULL;
    while (p)
    {
    q = p;
    p = p->next;
    free_node(q);
    }
    }

    free_node(link *p)

    {
    if (p)
    free(p);
    }
    */
    int main(int argc, char *argv[]) {

    link
    *head=NULL, *p=NULL, *s=NULL;
    for (i = 0; i<N; i++)
    // insert1(&head,make_node(a[i]));
    // insert2(&head,make_node(a[i]));
    // push(&head,make_node(a[i]));
    // plink f=head;
    // head=pop(&head);
    enqueue(&head,make_node(a[i]));

    plink f
    =head;
    // is_empyt(head);
    for(i=0;i<N;i++)
    while((p=dequeue(&head))!='\0'){

    printf(
    "%c",p->val);
    }
    // printf("\n");
    // printf("%d\n",serach(f,5)->val);
    // traverse(visit,head);
    // delete(&f,serach(f,5));
    printf("\n");
    // destroy(&f);

    // while(f)
    // {
    // printf("%c",f->val);
    // f=f->next;
    //
    // }
    // free_node(p);
    }

      

    转载自:http://www.oschina.net/code/snippet_164270_5310

  • 相关阅读:
    vs2015安装出问题
    Cookie的Domain属性
    IE6 PNG不透明问题 (只解决img标签的图片)
    C#aspx页面前台使用<%=%>无法取到后台的值
    题解【洛谷P2003】平板
    题解【洛谷P2070】刷墙
    题解【洛谷P1083】[NOIP2012]借教室
    CSP-J/S 2019 游记
    题解【洛谷P1967】[NOIP2013]货车运输
    “美登杯”上海市高校大学生程序设计邀请赛 (华东理工大学) E 小花梨的数组 线段树
  • 原文地址:https://www.cnblogs.com/dartagnan/p/2177664.html
Copyright © 2011-2022 走看看