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 090609103 at 2011-03-13 13:09:57 on Problem 1028
// 1028 Web Navigation.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

struct Node
{
	string site;
	Node* pre;
	Node* next;
};

void Insert( Node* &cur, Node* &b, Node* &f, const string site )
{
	// 新建节点

	Node* s = new Node();
	s->site = site;
	s->pre = cur;
	s->next = NULL;

	// 连

	cur->next = s;
	b = cur;
	cur = s;

	// 删除f之后所有节点

	while( f )
	{
		Node* p = f->next;
		delete f;
		f = p;
	}
}

bool Back( Node* &cur, Node* &b, Node* &f )
{
	if( b )
	{
		f = cur;
		cur = b;
		b = b->pre;
		return true;
	}
	else
		return false;
}

bool Forward( Node* &cur, Node* &b, Node* &f )
{
	if( f )
	{
		b = cur;
		cur = f;
		f = f->next;
		return true;
	}
	else
		return false;
}

void ShowCur( const Node* cur, const bool flag )
{
	if( flag ) 
		cout << cur->site << endl;
	else
		cout << "Ignored\n";
}

void Dispose( Node* &cur, Node* &b, Node* &f )
{
	while( b )
	{
		Node* p = b->pre;
		delete b;
		b = p;
	}

	delete cur;
	cur = NULL;

	while( f )
	{
		Node* p = f->next;
		delete f;
		f = p;
	}
}

int main()
{
	Node* cur = new Node();
	cur->site = "http://www.acm.org/";
	cur->pre = NULL;
	cur->next = NULL;
	Node* b = cur->pre;
	Node* f = cur->next;

	string cam, buff;
	cin >> cam;
	while( cam != "QUIT" )
	{
		char ch = getchar();
		if( ch == ' ' )
		{
			cin >> buff;
		}
		bool flag = true;
		if( cam == "VISIT" )
			 Insert( cur, b, f, buff );
		else if( cam == "BACK" )
			flag = Back( cur, b, f );
		else if( cam == "FORWARD" )
			flag = Forward( cur, b, f );
		ShowCur( cur, flag );
		cin >> cam;
	}
	// 释放链表
	Dispose( cur, b, f );


	system("pause");
	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