33,318
社区成员
发帖
与我相关
我的任务
分享
class stack
{..
};
int main(int argc,char** argv)
{
char buf[100];
stack textlines;
FILE* file = fopen(argv[1],"r");
while (fgets(buf,100,file))
{
char* string = (char*)malloc(100);
strcpy(string,buf);
textlines.push(string);
}
char* s;
while ( (s = (char*)textlines.pop() ) != 0)
{
wprintf(L"&s",s);
free(s);
}
}
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
class stack
{
struct link
{
void* date;
link* next;
void initialize (void* const Date,link* const Next) { date = Date;next = Next;}
} *head;
public:
stack() { head = 0 ; }
~stack();
void push (void* const Date);
void* const peek()const { return head->date ; }
void* const pop();
};
void stack::push(void* const Date)
{
link* newlink = (link*)malloc(sizeof(link));
newlink->initialize(Date,head);
head = newlink;
}
void* const stack::pop()
{
if ( head == 0 )
return 0;
link* temp = head;
void* result = head->date;
head = head->next;
free(temp);
return result;
}
stack::~stack()
{
link *temp = head;
while ( temp != 0 )
{
head = head->next;
free(head->date);
free(head);
}
}
int main(int argc,char* argv[])
{
char buf[100];
stack textlines;
FILE* file = fopen(argv[1],"r");
while (fgets(buf,100,file))
{
char* string = (char*)malloc(strlen(buf)+1);
strcpy(string,buf);
textlines.push(string);
}
char* s;
while ( (s = (char*)textlines.pop() ) != 0)
{
printf("%s",s);
free(s);
}
}