Chinaunix首页 | 论坛 | 博客
  • 博客访问: 46344
  • 博文数量: 9
  • 博客积分: 587
  • 博客等级: 中士
  • 技术积分: 210
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-11 17:00
文章分类

全部博文(9)

文章存档

2012年(1)

2010年(1)

2009年(3)

2008年(4)

我的朋友
最近访客

分类:

2009-01-12 23:15:47

 

/*
 * Stack.h
 *
 * Created on: 2009-1-12
 * Author: chen
 */

#include <stdio.h>
#include <stdlib.h>

#ifndef STACK_H_
#define STACK_H_

//栈的链表实现方式。

typedef struct Stack
{
    void *content;
    struct Stack *next;
}Stack;

#endif /* STACK_H_ */

Stack* pop(Stack* head);
void push(Stack* head, void *content);
int stack_dispose(Stack* head);

 

具体实现。

/*
 * Stack.c
 *
 * Created on: 2009-1-12
 * Author: chen
 */


#include "Stack.h"

void push(Stack* head, void *content)
{
    Stack *node = (Stack *)malloc(sizeof(Stack));
    node->content = content;
    node->next = head->next;
    head->next = node;
}

Stack* pop(Stack* head)
{
    Stack* temp = head->next;
    if(temp) {
        head->next = temp->next;
    }else{
        head->next = temp;
    }
    return temp;
}

int stack_dispose(Stack* head)
{
    Stack *del = head->next;
    Stack *temp = NULL;
    while(del) {
        temp = del->next;
        free(del->content);
        free(del);
        del = temp;
    }
    return 0;
}

 

测试用主函数。

 

/*
 * main.c
 *
 * Created on: 2009-1-12
 * Author: chen
 */


#include "Stack.h"

int main(void)
{
    Stack head;
    head.next = NULL;
    char *txt1 = (char *)malloc(100);
    memset(txt1, 0, 100);
    char *txt2 = (char *)malloc(100);
    memset(txt2, 0, 100);
    char *txt3 = (char *)malloc(100);
    memset(txt3, 0, 100);
    char *txt4 = (char *)malloc(100);
    memset(txt4, 0, 100);
    char *txt5 = (char *)malloc(100);
    memset(txt5, 0, 100);
    memcpy(txt1, "aaaaaaaa", 100);
    memcpy(txt2, "bbbbbbbb", 100);
    memcpy(txt3, "cccccccc", 100);
    memcpy(txt4, "dddddddd", 100);
    memcpy(txt5, "eeeeeeee", 100);
    push(&head, (void *)txt1);
    push(&head, (void *)txt2);
    push(&head, (void *)txt3);
    push(&head, (void *)txt4);
    push(&head, (void *)txt5);

    Stack *temp = pop(&head);
    printf("%s\n",(char *)temp->content);
    free(temp->content);
    free(temp);
    temp = pop(&head);
    printf("%s\n",(char *)temp->content);
    free(temp->content);
    free(temp);
    stack_dispose(&head);
    return 0;
}

阅读(494) | 评论(0) | 转发(0) |
0

上一篇:accept阻塞挡不住(原创)

下一篇:没有了

给主人留下些什么吧!~~