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);
}
--------------------------------------------
liststack——链表栈(procedure)的更多相关文章
- C语言 复杂的栈(链表栈)
//复杂的栈--链表栈 #include<stdio.h> #include<stdlib.h> #define datatype int//定义链表栈数据类型 //定义链表栈 ...
- java——链表、链表栈 LinkedListStack、链表队列 LinkedListQueue
LikedList: package Date_pacage; public class LinkedList<E> { public static void main(String[] ...
- Python与数据结构[1] -> 栈/Stack[0] -> 链表栈与数组栈的 Python 实现
栈 / Stack 目录 链表栈 数组栈 栈是一种基本的线性数据结构(先入后出FILO),在 C 语言中有链表和数组两种实现方式,下面用 Python 对这两种栈进行实现. 1 链表栈 链表栈是以单链 ...
- bzoj 1098 办公楼biu —— 链表+栈
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1098 首先,没有连边的人一定得在一个连通块里: 先把所有人连成一个链表,然后从第一个人开始, ...
- 链表栈C语言实现
#ifndef LINKSTACK_H_INCLUDED #define LINKSTACK_H_INCLUDED #include <stdlib.h> #include <std ...
- 链表栈的C语言实现
#ifndef _CONST_H_#define _CONST_H_ #include <stdio.h>#include <stdlib.h> typedef enum { ...
- 剑指offer-反向遍历链表-栈和递归2种方法(一次性跑通)
- 二叉树、栈、队列、链表的Java代码实现
这是我的学习总结. 如有文章存在谬误,欢迎指出,有其他意见或者建议,也欢迎留言 二叉树链表 前序遍历:先访问根节点,然后访问左子树.右子树 中序遍历:先访问左子树,然后访问根节点.右子树 后序遍历:先 ...
- 用OC实现一个栈:结合单链表创建动态栈
一.介绍 栈是一种数据存储结构,存储的数据具有先进后出的特点.栈一般分为动态栈和静态栈. 静态栈比较好理解,例如用数组实现的栈.动态栈可以用链表来实现. 方式:固定base指针,每次更改top指向入栈 ...
随机推荐
- iOS设备、Icon、LaunchImage、图片分辨率
iOS设备 iOS设备的屏幕的大小.分辨率以及比例因数(Scale Factor)[1]. iPhone 设备 宽(inch) 高(inch) 对角线(inch) 逻辑分辨率(point) Scale ...
- jquery 只有二级下拉菜单
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- div模块变灰
整站变灰目前没发现什么特别好的办法,但是div(或者其他标签模块)模块变灰方法兼容性还不错. .gay_box{ filter: grayscale(100%); -webkit-filter: gr ...
- 用Django搭建个人博客—(1)
业精于勤荒于嬉,形成于思毁于随. 本阶段的任务小记: 简单介绍一下Django的使用,创建项目和一个app 简单介绍一下Django的settings.py文件的相关配置 整合数据库到自己的博客系统中 ...
- 2016021902 - linux解压缩命令
转载自:http://blog.csdn.net/luo86106/article/details/6946255 .gz 解压1:gunzip FileName.gz 解压2:gzip -d Fil ...
- 字符串copy
#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string. ...
- code jam训练
https://code.google.com/codejam/contests.html http://student.csdn.net/mcs/programming_challenges
- PAT (Basic Level) 1002. 写出这个数 (20)
读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式:在一行内输出n的各位数字之和的每 ...
- 百度编辑器ueditor 使用
ueditor 百度开源的一个 编辑器 ,支持api.扩展,demo丰富.推荐下 以前写 编辑 词典的使用 jquery-te 轻量级编辑器..当时看中了 它代码轻巧.容易改. 把他的功能改了好多. ...
- Hadoop 学习笔记 (十) hadoop2.2.0 生产环境部署 HDFS HA Federation 含Yarn部署
其他的配置跟HDFS-HA部署方式完全一样.但JournalNOde的配置不一样>hadoop-cluster1中的nn1和nn2和hadoop-cluster2中的nn3和nn4可以公用同样的 ...