现在的位置: 首页 > 综合 > 正文

stack.cpp 【简单的栈的实现】【c++】

2013年03月09日 ⁄ 综合 ⁄ 共 2834字 ⁄ 字号 评论关闭

stack3.h

//---------------------------------------------------------------------------

#ifndef stack3H
#define stack3H

class stack
{
 struct link
 {
   void* data;
   link* next;
   void initialize(void*data,link*next);
 }*head;

 public:
 stack();
 ~stack();
 void push(void* Data);
 void* peek();
 void* pop();
};
//---------------------------------------------------------------------------
#endif

stack3.cpp

//---------------------------------------------------------------------------

#pragma hdrstop

#include "stack3.h"
#include <stdlib.h>
#include <assert.h>
                 
//---------------------------------------------------------------------------
void  stack::link::initialize(void* Data,link*Next)
{
  data=Data;
  next=Next;
}
stack::stack(){ head=0;}
void  stack::push(void* Data)
{
   link* newlink=(link*)malloc(sizeof(link));
   assert(newlink);
   newlink->initialize(Data,head);
   head=newlink;
}
void* stack::peek(){   return  head->data;}
void* stack::pop()
{
  if(head==0)
   return 0;
  void* result=head->data;
  link* oldHead=head;
  head=head->next;
  free(oldHead);
  return result;
}

stack::~stack()
{
 link* cursor=head;
 while(head)
 {
  cursor=cursor->next;
  free(head->data);
  free(head);
  head=cursor;
 }
}
#pragma package(smart_init)

main.cpp

//---------------------------------------------------------------------------

#pragma hdrstop
#include "stack3.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <conio.h>
#define BUFSIZE 100

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{

 //  assert(argc==2);
   FILE* file=fopen("I:\\c_coder\\data.txt","r");
   if(file==NULL)
    printf("%s","file error!");
   //assert(file);

   char buf[BUFSIZE];
   stack textlines;                    
   while(fgets(buf,BUFSIZE,file))
   {
     char* string=
        (char*)malloc(strlen(buf)+1);
        assert(string);
        strcpy(string,buf);
        textlines.push(string);
   }
   
   char*s;

   while((s=(char*)textlines.pop())!=0)
   {
       printf("%s",s);
       free(s);
   }   
   
   getch();
   return 0;
}
//---------------------------------------------------------------------------

 #pragma hdrstop

预编译头文件的结束标志 

#pragma argsused

 

Syntax

#pragma hdrstop

Description

This directive terminates the list of header files eligible for precompilation. You can use it to reduce the amount of disk space used by precompiled headers.

Precompiled header files can be shared between the source files of your project only if the #include directives before #pragma hdrstop are identical. Therefore, you get the best compiler performance if you include common header files of your project before
the #pragma hdrstop, and specific ones after it. Make sure the 
#include directives before the #pragma hdrstop are identical in all the source files, or that there are only very few variations.

The integrated development environment generates code to enhance precompiled header performance. For example, after a New Application, source file "Unit1.cpp" will look like this (comments added):

#include <vcl.h> // common header file

#pragma hdrstop // terminate list here

#include "Unit1.h" // specific header file
// ....

Use this pragma directive only in source files. The pragma has no effect when it is used in a header file.

 

抱歉!评论已关闭.