多文件编程链状结构实现栈的操作 c语言版
2014-03-26 00:00
639 查看
动态开辟存储空间模拟栈的操作:
stack.c
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
typedef struct node{
int num;
struct node *p_next;
}node;
static node head,tail;
void push(int number){
node *tmp = (node *)malloc(sizeof(node));
tmp->num = number;
tmp->p_next = head.p_next;
head.p_next = tmp;
}
int pop(){
int number;
if (head.p_next != &head){
node *tmp = head.p_next;
head.p_next = head.p_next->p_next;
number = tmp->num;
free(tmp);
tmp = NULL;
}
return number;
}
int top(){
return head.p_next == &tail ? 0 : head.p_next->num;
}
int empty(){
return head.p_next == &tail;
}
int full(){
return 0;
}
int size(){
node *p_node =&head;
int cnt = 0;
for (p_node = &head;p_node != &tail;p_node = p_node->p_next){
node *p_lnext = p_node->p_next;
if (p_lnext != &tail){
cnt++;
}
}
return cnt;
}
void init(){
head.p_next = &tail;
}
void deinit(){
node *p_node;
while (head.p_next != &tail){
p_node = head.p_next;
head.p_next = head.p_next->p_next;
free(p_node);
p_node = NULL;
}
}
stack.h
#ifndef __STACK_H__
#define __STACK_H__
void push(int number);
int pop();
int top();
int empty();
int full();
int size();
void init();
void deinit();
#endif
main.c
#include<stdio.h>
int main()
{
init();
push(1);
push(3);
push(4);
printf("size = %d\n",size());
printf("%d\n",pop());
printf("%d\n",pop());
printf("%d\n",pop());
printf("size = %d\n",size());
if(empty()) printf("the stack is empty.\n");
return 0;
}
stack.c
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
typedef struct node{
int num;
struct node *p_next;
}node;
static node head,tail;
void push(int number){
node *tmp = (node *)malloc(sizeof(node));
tmp->num = number;
tmp->p_next = head.p_next;
head.p_next = tmp;
}
int pop(){
int number;
if (head.p_next != &head){
node *tmp = head.p_next;
head.p_next = head.p_next->p_next;
number = tmp->num;
free(tmp);
tmp = NULL;
}
return number;
}
int top(){
return head.p_next == &tail ? 0 : head.p_next->num;
}
int empty(){
return head.p_next == &tail;
}
int full(){
return 0;
}
int size(){
node *p_node =&head;
int cnt = 0;
for (p_node = &head;p_node != &tail;p_node = p_node->p_next){
node *p_lnext = p_node->p_next;
if (p_lnext != &tail){
cnt++;
}
}
return cnt;
}
void init(){
head.p_next = &tail;
}
void deinit(){
node *p_node;
while (head.p_next != &tail){
p_node = head.p_next;
head.p_next = head.p_next->p_next;
free(p_node);
p_node = NULL;
}
}
stack.h
#ifndef __STACK_H__
#define __STACK_H__
void push(int number);
int pop();
int top();
int empty();
int full();
int size();
void init();
void deinit();
#endif
main.c
#include<stdio.h>
int main()
{
init();
push(1);
push(3);
push(4);
printf("size = %d\n",size());
printf("%d\n",pop());
printf("%d\n",pop());
printf("%d\n",pop());
printf("size = %d\n",size());
if(empty()) printf("the stack is empty.\n");
return 0;
}
相关文章推荐
- 多文件编程动态开辟空间来实现队列的操作 c语言版
- linux系统编程之文件与I/O(五):文件的内核结构file和dup实现重定向
- linux系统编程之文件与I/O(五):文件的内核结构file和dup实现重定向
- Java编程___File各类方法使用(实现拷贝特定文件至特定目录下)
- 在keil中使用头文件实现多文件编程
- (转载)VS2010/MFC编程入门之三(VS2010应用程序工程中文件的组成结构)
- 【数字图像处理】C++读取、旋转和保存bmp图像文件编程实现
- 【小程序】递归实现控制台输出的文件树形结构
- 高质量C++/C编程指南 -- 第1章 文件结构
- [C#]TreeView实现文件结构查看器
- 【数字图像处理】C++读取、旋转和保存bmp图像文件编程实现
- 编程实现QQ表情文件CFC格式
- 混合编程 -- 实现C自动编译以及thrift文件实现生成定义文件
- 【java编程】IO特殊类之RandomAccessFile实现多线程文件下载
- iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载+使用输出流代替文件句柄
- iOS开发之网络编程--使用NSURLConnection实现大文件断点续传下载
- MPI实现fft的迭代算法 源于并行计算——结构。算法。编程中伪码 更新2
- linux c/c++ 编程之-----创建管道文件,并实现两个进程间的信息传递。
- VisualC++信息安全编程(4)实现文件自我删除
- Linux中.a,.la,.o,.so文件的意义和编程实现