zoukankan      html  css  js  c++  java
  • liststack——链表栈(procedure)

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "list.h"
    #include "stack.h"

    #define   NAMESIZE  24

    typedef struct stuinfo{
     int id;
     char name[NAMESIZE];
     int math;
    }DATA;

    static void print_s(const void *data)
    {
     const DATA *stup = data;
     printf("%d %s %d ",
       stup->id,
       stup->name,
       stup->math);
    }

    static int IdCmp(const void *key, const void *data)
    {
     const int *id = key;
     const DATA *stup = data;

     return (*id - stup->id);
    }
    static int NameCmp(const void *key, const void *data)
    {
     const char *name = key;
     const DATA *stup = data;

     return strcmp(name, stup->name);
    }

    int main()
    {
     int i;
     int id = 5;
     char name[NAMESIZE] = "stu3";
     DATA stu, *stup;
     STACK s = NULL;

     s = StackCreate(sizeof(DATA));
     if(s == NULL)
      return -1;

     for(i = 0; i < 6; i++)
     {
      stu.id = i + 1;
      snprintf(stu.name,NAMESIZE,
        "stu%d", i + 1);
      stu.math = 100 - i;

      PushStack(s, &stu);
     }
     PopStack(s);
     stup = TopOfStack(s);
     print_s(stup);
     TopAndPopStack(s, &stu);

     StackDisplay(s, print_s);
     StackDispose(s);
     
     return 0;
    }
    ----------------------------------------------------------

    #ifndef _STACK_H__
    #define _STACK_H__

    #include "list.h"

    typedef LIST STACK;

    STACK StackCreate(int);
    int StackIsEmpty(STACK);
    int PushStack(STACK, const void *);
    int PopStack(STACK);
    void* TopOfStack(STACK);
    int TopAndPopStack(STACK, void *);
    void StackDisplay(STACK, print *);
    void StackDispose(STACK);

    #endif
    ---------------------------------------------------------------

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "list.h"
    #include "stack.h"

    STACK StackCreate(int size)
    {
     return ListCreate(size);
    }
    int StackIsEmpty(STACK s)
    {
     return s->head.next == &s->head;
    }
    int PushStack(STACK s, const void *data)
    {
     return ListInsert(s, data, HEADINSERT);
    }
    static int always_match(const void *key, const void *data)
    {
     return 0;
    }
    int PopStack(STACK s)
    {
     return ListDelete(s, (void *)0, always_match);
    }
    void* TopOfStack(STACK s)
    {
     return ListFind(s, (void *)0, always_match);
    }
    int TopAndPopStack(STACK s, void *data)
    {
     return ListFetch(s, (void *)0, always_match, data);
    }
    void StackDisplay(STACK s, print *funp)
    {
     ListDisplay(s, funp);
    }
    void StackDispose(STACK s)
    {
     ListDispose(s);
    }
    -------------------------------------------------------------------------

    #ifndef _LIST_H__
    #define _LIST_H__

    #define HEADINSERT 1
    #define TAILINSERT  2

    struct listnode;
    struct headnode;
    typedef struct headnode *LIST;
    typedef struct listnode *PtrNode;

    typedef void print(const void *);
    typedef int cmp(const void *, const void *);

    LIST ListCreate(int);
    int ListInsert(LIST, const void *, int);
    void *ListFind(LIST, const void *, cmp *);
    int ListDelete(LIST, const void *, cmp *);
    int ListFetch(LIST, const void *, cmp *, void *);
    void ListDisplay(LIST, print *);
    void ListDispose(LIST);

    struct listnode{
     void *data;
     struct listnode *prev;
     struct listnode *next;
    };

    struct headnode{
     int size;
     struct listnode head;
    };

    #endif
    -----------------------------------------------------------------------

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "list.h"

    LIST ListCreate(int size)
    {
     LIST handle = malloc(sizeof(*handle));
     if(NULL == handle)
      return NULL;

     handle->size = size;
     handle->head.data = NULL;
     handle->head.prev = handle->head.next = &handle->head;

     return handle;
    }

    int ListInsert(LIST l, const void *data, int mode)
    {
     PtrNode cur = malloc(sizeof(*cur));
     if(NULL == cur)
      return -1;
     cur->data = malloc(l->size);
     if(NULL == cur->data)
     {
      free(cur);
      return -2;
     }

     memcpy(cur->data, data, l->size);

     if(mode == HEADINSERT)
     {
      cur->next = l->head.next;
      cur->prev = &l->head;
     }
     else if(mode == TAILINSERT)
     {
      cur->next = &l->head;
      cur->prev = l->head.prev;
     }
     else
     {
      free(cur->data);
      free(cur);
      return -3;
     }
     cur->prev->next = cur;
     cur->next->prev = cur;
     return 0;
    }

    static PtrNode find(LIST l, const void *key, cmp *funp)
    {
     PtrNode p = l->head.next;

     for(;p != &l->head && funp(key, p->data); p = p->next);

     return p;
    }

    void *ListFind(LIST l, const void *key, cmp *funp)
    {
     return find(l, key, funp)->data;
    }

    int ListDelete(LIST l, const void *key, cmp *funp)
    {
     PtrNode p = find(l, key, funp);
     if(p == &l->head)
      return -1;

     p->prev->next = p->next;
     p->next->prev = p->prev;
     //p->prev = p->next = NULL;
     
     free(p->data);
     free(p);
     //p = NULL;
     return 0;
    }

    int ListFetch(LIST l, const void *key, cmp * funp, void *data)
    {
     PtrNode p = find(l, key, funp);
     if(p == &l->head)
      return -1;

     memcpy(data, p->data, l->size);
     p->prev->next = p->next;
     p->next->prev = p->prev;
     //p->prev = p->next = NULL;
     
     free(p->data);
     free(p);
     //p = NULL;
     return 0;
    }

    void ListDisplay(LIST l, print *funp)
    {
     PtrNode p = l->head.next;

     while(p != &l->head)
     {
      funp(p->data);
      p = p->next;
     }
    }

    void ListDispose(LIST l)
    {
     PtrNode p = l->head.next;
     PtrNode q;

     while(p != &l->head)
     {
      q = p;
      p = p->next;

      free(q->data);
      free(q);
     }#FLAGS = -lmylist
    all:main

    main:main.o list.o stack.o

    clean:
     rm -f *.o main
     free(l);
    }
    --------------------------------------------

  • 相关阅读:
    RHEL6安装JDK7
    Linux&nbsp;下安装配置&nbsp;JDK7(2)
    Linux安装Tomcat7
    useradd命令
    Linux下搭建tomcat集群全记录
    (转)通向架构师的道路(第五天)…
    Apache2.2安装图解
    (转)apache2.2.x+tomcat7.0.x集群+…
    web性能并发测试工具(转)
    Hibernate注解详细介绍
  • 原文地址:https://www.cnblogs.com/riskyer/p/3356264.html
Copyright © 2011-2022 走看看