Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我是个初学者,望牛人指点。代码很详细,纯栈操作....

Posted by qichao at 2009-08-01 11:20:05 on Problem 1028
#include<iostream>
#include<string.h>
#include<malloc.h>
using namespace std;

typedef struct web
{
	char data[105][75];
	int top;
}Forword,Backword,Listack;

void Initstack(Listack *&S)
{
	S=(Forword *)malloc(sizeof(Forword));
	S->top=-1;
}

void Push(Listack *&S,char *p)//进栈
{
	S->top++;
	strcpy(S->data[S->top],p);
}

int Pop(Listack *&S,char *p)//出栈
{
	if(S->top==-1)
	{
		cout<<"Ignored"<<endl;
		return 0;
	}
	else
	{
		strcpy(p,S->data[S->top]);
		S->top--;
		return 1;
	}
}

void Destorystack(Listack *&S)
{
	free(S);
}

int main()
{
	Forword *S1;
	Backword *S2;
	Initstack(S1);
	Initstack(S2);
	char cur[71]="http://www.acm.org/";
	char ch1[10],ch2[71];
	cin>>ch1;
	while(strcmp(ch1,"QUIT")!=0)
	{		
		if(strcmp(ch1,"VISIT")==0)
		{
			cin>>ch2;
			Push(S2,cur);
			strcpy(cur,ch2);
			cout<<cur<<endl;			
			Destorystack(S1);
			Initstack(S1);
		}
		else if(strcmp(ch1,"BACK")==0)
		{
			if(strcmp(cur,"http://www.acm.org/")!=0)
				Push(S1,cur);
			if(Pop(S2,cur))
				cout<<cur<<endl;
		}
		else if(strcmp(ch1,"FORWARD")==0)
		{
			Push(S2,cur);
			if(Pop(S1,cur))
				cout<<cur<<endl;
		}
		cin>>ch1;
	}
	Destorystack(S1);
	Destorystack(S2);
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator