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\n",
   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);
}
--------------------------------------------

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
哈希表是一种高效的数据结构,可以用来存储和查找键值对。其中,哈希函数将键映射到一个特定的桶中,每个桶中存储一组键值对。在哈希表中,如果两个键被映射到同一个桶中,就会发生碰撞。为了解决这个问题,可以使用链表法。 链表法是一种解决哈希表碰撞问题的方法。具体来说,对于哈希表中的每个桶,可以使用一个链表来存储所有映射到该桶的键值对。如果发生碰撞,只需要将新的键值对添加到链表的末尾即可。 下面是一个使用链表法实现哈希表的示例代码: ```python class Node: def __init__(self, key, value): self.key = key self.value = value self.next = None class HashTable: def __init__(self, capacity): self.capacity = capacity self.buckets = [None] * capacity def hash_function(self, key): return hash(key) % self.capacity def put(self, key, value): index = self.hash_function(key) node = self.buckets[index] while node: if node.key == key: node.value = value return node = node.next new_node = Node(key, value) new_node.next = self.buckets[index] self.buckets[index] = new_node def get(self, key): index = self.hash_function(key) node = self.buckets[index] while node: if node.key == key: return node.value node = node.next return None def remove(self, key): index = self.hash_function(key) node = self.buckets[index] prev = None while node: if node.key == key: if prev: prev.next = node.next else: self.buckets[index] = node.next return prev = node node = node.next ``` 在这个示例中,我们定义了一个Node类来表示哈希表中的每个节点,每个节点包含一个键、一个值和一个指向下一个节点的指针。我们还定义了一个HashTable类来实现哈希表,其中包含一个桶数组和一些基本的操作方法,如put、get和remove。 在put方法中,我们首先使用哈希函数计算出键的索引,然后遍历桶中的链表,查找该键是否已经存在于哈希表中。如果找到了该键,我们只需要更新其对应的值即可。否则,我们创建一个新的节点,并将其添加到链表的开头。 在get方法中,我们同样使用哈希函数计算出键的索引,然后遍历桶中的链表,查找该键的值。如果找到了该键,我们返回其对应的值。否则,返回None。 在remove方法中,我们首先使用哈希函数计算出键的索引,然后遍历桶中的链表,查找该键。如果找到了该键,我们将其从链表中删除即可。 总的来说,链表法是一种简单且常用的哈希表解决碰撞问题的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值