c stack

用一个头链表实现栈,头指针永远指向栈顶元素

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

typedef  int ElemenType  ;

typedef struct node{
    ElemenType data;
    struct node *next;
}Linkstack;

Linkstack *top;

Linkstack *CreateStack()
{
  Linkstack *s;
  s=(Linkstack*)malloc(sizeof(struct node));
  s->next=NULL;
  return s;
}

int IsEmpty(Linkstack *s)
{
  if(s->next==NULL)
    return 0;
  else return 1;
}

void push(Linkstack *s,ElemenType item)
{
  Linkstack *n=(Linkstack*)malloc(sizeof(struct node));
  n->data=item;
  n->next=s->next;
  s->next=n;
}

ElemenType pop(Linkstack *s)
{
  if(s->next == NULL)
    return 0;
  ElemenType item=s->data;
   Linkstack *n=(Linkstack*)malloc(sizeof(struct node));
   n=s->next;
   s->next=n->next;
   item=n->data;
   free(n);
   return item;

}

void VistStack(Linkstack *s)
{
 Linkstack *p=(Linkstack*)malloc(sizeof(struct node));
 p=s->next;
  while(p->next!=NULL)
    {
      printf("%d ",p->data);
      p=p->next;
    }
  printf("%d",p->data);//打印最后一个元素
}

int main()
{
  Linkstack *q;
  q=CreateStack();
  push(q,10);
  push(q,11);
  push(q,8);
  push(q,13);
  push(q,7);
  push(q,9);
  VistStack(q);
 ElemenType z= pop(q);
 printf("\n%d\n",z);
 VistStack(q);

  return 0;
}
时间: 2024-10-12 18:06:46

c stack的相关文章

51nod1289 stack

1289 大鱼吃小鱼 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 有N条鱼每条鱼的位置及大小均不同,他们沿着X轴游动,有的向左,有的向右.游动的速度是一样的,两条鱼相遇大鱼会吃掉小鱼.从左到右给出每条鱼的大小和游动的方向(0表示向左,1表示向右).问足够长的时间之后,能剩下多少条鱼? Input 第1行:1个数N,表示鱼的数量(1 <= N <= 100000). 第2 - N + 1行:每行两个数A[i], 

How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu

About LAMP LAMP stack is a group of open source software used to get web servers up and running. The acronym stands for Linux, Apache, MySQL, and PHP. Since the virtual private server is already running Ubuntu, the linux part is taken care of. Here i

Stack的pop和push操作

#include <stack> #include <cstdio> using namespace std; int main(){ stack<int> s; s.push(1); s.push(2); s.push(3); printf("%d\n", s.top()); s.pop(); printf("%d\n", s.top()); s.pop(); printf("%d\n", s.top());

从头认识java-9.8 栈(Stack)

这一章节我们来讨论一下栈(Stack). 1.特性 先进后出,当一个元素压进栈里面,他就会处于栈的底部,然后,另一个再压进来,盖在原来的元素上面,原来的元素想出去,只有等上面的元素先顶出栈才有机会. 2.方法演示 package com.ray.ch09; import java.util.Arrays; import java.util.Stack; public class Test { public static void main(String[] args) { Stack<Integ

C#中泛型容器Stack&lt;T&gt;的用法,以及借此实现&rdquo;撤销/重做&rdquo;功能

.Net为我们提供了众多的泛型集合.比如,Stack<T>先进后出,Queue<T>先进先出,List<T>集合元素可排序,支持索引,LinkedList<T>,双向链表的泛型实现,不支持索引;ISet<T>不允许被复制,他有2个实现,一个是HashSet<T>,不维持集合元素的排序,另一个是SortedSet<T>,支持集合元素的排序;IDictionary<TKey, TValue>是一个字典集合的泛型接口

Improving performance – A full stack problem

Improving performance – A full stack problem March 6, 2015 by ronald 4 Comments Improving the performance of a web system involves knowledge of how the entire technology stack operates and interacts. There are many simple and common tips that can pro

LeetCode Min Stack

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. top() -- Get the top element. getMin() -- Retrieve the minimum e

[Java Basics] Stack, Heap, Constructor

Good about Java: friendly syntax, memory management[GC can collect unreferenced memory resources], object-oriented features, portability. Stack Stores method invocations, local variables(include object reference, but the object itself is still stored

使用Stack堆栈集合大数据运算

使用Stack堆栈集合大数据运算 package com.sta.to; import java.util.Iterator; import java.util.Stack; public class DaMax { public void jiaFa(String value1, String value2) { /** * 更多资料欢迎浏览凯哥学堂官网:http://kaige123.com * @author 小沫 */ // 把字符串用toCharArray拆成字符 char[] c1

Java之集合(四)Vector和Stack

转载请注明源出处:http://www.cnblogs.com/lighten/p/7296023.html 1.前言 本章介绍Java集合List中的Vector和其子类Stack.Vector类是Java 1.0就提供的一个集合类,其实现和上章讲解的ArrayList几乎一样,在下面会简单介绍一下(不然就没有可说的了),其子类Stack是一个程序员都比较熟悉的栈结构,特点就是先入后出,Stack其实也较为简单,会简单描述一下. 2.Vector 在前言中也谈到了Vector与ArrayLis