天之道

享受编程的乐趣。
posts - 118, comments - 7, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

利用链表创建栈

Posted on 2012-02-28 00:37 hoshelly 阅读(208) 评论(0)  编辑 收藏 引用 所属分类: DS && Algorithm
//利用链表创建栈
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
struct stack_node
{
    int data;
struct stack_node*next;
};
typedef struct stack_node stack_list;
typedef stack_list *link;
link stack=NULL;
//栈数据的存入
int push(int value)
{
   link new_node;
   new_node=(link)malloc(sizeof(stack_list));
   if(!new_node)
   {
      printf("内存分配失败!\n");
  return -1;
}
new_node->data=value;
new_node->next=stack;
stack=new_node;
}
//栈数据的取出
int pop()
{
   link top;
   int temp;
   if(stack!=NULL)
   {
      top=stack;
  stack=stack->next;
  temp=top->data;
  free(top);
  return temp;
}
6
    else
return -1;
}
int empty()
{
   if(stack == NULL)
    return 1;
   else
    return 0;
}
void main()
{
  int card[52];
  int pos;
  int i,temp;
  for(i=0;i<52;i++)
    card[i]=0;
  i=0;
  while(i!=52)
  {
     pos=rand()%52;
 if(card[pos] == 0)
 {
    push(pos);
card[pos]=1;
i++;
 }
  }
  
  printf("  1    2    3    4\n");
  printf("======================\n");
  for(i=0;i<5;i++)
  {
     temp=pop();
 printf("[%c%2d]",temp/13+3,temp%13+1);
 temp=pop();
 printf("[%c%2d]",temp/13+3,temp%13+1);
 temp=pop();
 printf("[%c%2d]",temp/13+3,temp%13+1);
 temp=pop();
 printf("[%c%2d]",temp/13+3,temp%13+1);
 printf("\n");
   }
}

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理