我有问题的链接列表程序 [英] linklist program that i have a problem

查看:74
本文介绍了我有问题的链接列表程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// nomi az akhar.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
using namespace std;

class node 
{
	friend class linklist ;
	int data ;
	node *next ;
};

class linklist
{

public:

	linklist() ;
	void insert() ;
	void search();
	void show();

private:

	node *last;
	node *first ;

};


linklist::linklist()
{
	last = first = NULL ;
}

void linklist ::insert()
{
	int n ;
	cout <<"plz enter the numbers of numbers :" << endl ;
	cin >> n;
	cout << "enter the numbers :" << endl ;
	for(int i =0 ; i < n ; i++)
	{
        node *help = new node ();
        cin >> help -> data ;
        help->next = NULL;
        if(first == NULL)
        {
            first = help ;
            last = help;
        }
        else
        {
            node* temp = first;
            while(temp->next != NULL)
            {
                temp = temp->next;
            }
            temp->next = help;
            last = help;
        }
    }
}

void linklist::search()
{
	//chand ta adad begire n omin adad az akharo prinnt kone
	int n , m ;
	cout <<"plz enter the numbers of numbers :" << endl ;
	cin >> n;
	cout <<"plz enter number of gereh :" << endl ;
	cin >> m;
	node *p = first ;
	//while ( p != NULL )
	//{
		if ( n > m)
		{
			p = p -> next ;
		}
		else
		{
			cout <<"yay"<< p->next-> data ;	
		}
		
//	}
	cout <<"yay"<<p;
}

void linklist ::show()
{
	//int r = 2;
    //cout << "number      " <<endl ;
    node *curPtr = first;
  
      /*while( curPtr ) {
            for(int i =0 ;i<3 ;i++){
                 cout << curPtr -> data << "     ";
            }
		         curPtr = curPtr -> next;
		         r ++;
            }*/
	
    while(curPtr)
    {
        cout<<curPtr->data<<endl;
        curPtr = curPtr->next;
	
	}
}
int main()
{
	
    linklist m;
    m.insert();
    m.show();
	m.search();
	
	return 0;
}



该程序必须搜索用户写的编号(从最后到第一个)
例如
1 2 3 4 5 6
用户想要第二个数字
用户5的程序打印内容.



this program must search the number that user write (from the last to first)
for example
1 2 3 4 5 6
the user want the second number
the the program print for user 5.

推荐答案

由于您似乎被卡住了,因此我将为您提供从何处开始的基本想法

更改您的插入方法以接受一个int,然后从外部执行插入.
Since you seem stuck, I''ll give you a basic idea where to start

Change your insert method to accept an int, and perform insertion from outside.
class linklist
{
    ...
    void insert(int val);
}

void outerfunction()
{
    for (size_t i = 0; i < max; ++i)
    {
        int aValue;
        cin >> aValue;
        mylinklist.insert(aValue);
    }
}



创建对数据的访问功能.这可以通过多种方式完成:
1.按索引访问



Create access functions to your data. This can be done in a number of ways:
1. Access by index

int getAt(size_t index) const; // For read-only access
int& getAt(size_t index); // for read/write access


2.将指针公开到您的内部结构(节点),但将其封装好.
实现类似于迭代器之类的东西,或者创建自己的类型(如MFC框架所使用的POSITION).如果您不熟悉C ++,则后者可能更易于实现.您要做的就是将节点指针转换为POSITION类型,并仅从列表中公开该类型.要访问数据,您可以在方法中接受这样的POSIION值,然后将其类型强制转换回节点指针.


2. Expose pointers to your internal structure (node), but encapsulate it well.
Either implement something like iterators a la stl, or create your own type like POSITION as used by the MFC framework. The latter might be easier to implement if you''re new to C++. All you have to do is to cast you node pointers to a POSITION type, and only expose that type from your list. To access the data you can accept such a POSIION value in a method, and type cast it back to a node pointer.

typedef void* POSITION; // No-one outside should know what this is
POSITION getHead() { return first; }
int getAt(POSITION p) const { return ((node*) p)->data; } // Cast to node*, and access data

void next(POSITION &p) const { p = ((node*) p)->next; }


不要忘记对参数进行NULL检查.

即使迭代器的实现比较困难,您也将受益于使用标准算法(例如搜索和排序),而无需自己实现它们.

现在,您可以将show()函数移出该类,而仅使用您创建的访问函数.

另外,您的链接列表类之外的任何人都不需要了解内部知识.
将节点的声明移到链接列表类内部,并使其受到保护.

如果正确执行所有这些操作,则将其设置为可以容纳其他数据类型的模板类将没有问题.不只是整数.

希望这将使您朝正确的方向发展.


Don''t forget to do NULL checks on parameters.

Even though the iterator implementation is harder, you will have the benefit of using standard algorithms such as searching and sorting, without the need of implementing them yourself.

Now, your show() function can then be moved out of the class, and only use the access functions you have created.

Also, no-one outside your linklist class needs to know about the internals.
Move the declaration of node to inside the linklist class and make it protected.

If you do all this properly, you will have no problem making it a template class, which can hold other data types. Not just ints.

Hope this will get you in the right direction.


首先,请执行以下操作:从列表类中删除I/O操作.有没有听说过分离问题?
应该从不是列表操作的任何东西中完全抽象出这种通用类型.

抱歉,我没有回答您有关搜索错误的问题.按照目前的形式,您的课程根本不值得考虑.首先删除I/O,然后设计适当的一组类操作以及所有方法签名.只有完成后,您才可以担心实现细节.

—SA
To start with: remove I/O operations from you list class. Ever heard of Separation of Concerns?
Such universal type should be completely abstracted from anything which is not the list operations.

Sorry I''m not answering your Question about errors in search. In the present form, your class simply does not deserve consideration. Remove I/O first, then design proper set of the class operations, all the method signatures. Only when it''s done you can worry about implementation detail.

—SA


这篇关于我有问题的链接列表程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆