두 개의 단일 체인 테이블 (헤드 노드와 헤드 노드가 없는 것) 의 창설 및 기본 조작 (삭제, 수정)

헤드 노드 사용:

#include "pch.h"
#include "pch.h"
#include "SingleList.h"
#include 
using namespace std;

int main(void)
{
	SingleList list;
	list.create(10);
	list.print();
	cout << " !" << endl;
	list.Delete(2);
	list.print();
	cout << " !" << endl;
	list.insert(2, 100);
	list.print();
	cout << " !" << endl;
	cout << "find:" << list.find(3) << endl;
	cout << " !" << endl;
	return 0;
}

typedef
struct Node {
	int data;
	struct Node * next;
}Node;

class SingleList
{
	Node head;//Node * head  , ,  head , 
	int length;
public:
	SingleList();
	void create(int len);
	void insert(int index,int data);
	void Delete(int index);
	int find(int index);
	int size();
	void print();
	~SingleList();
};

#include "pch.h"
#include "SingleList.h"


SingleList::SingleList()
{
	head.next = NULL;
	head.data = 0;
	length = 0;
}

void SingleList::create(int len)
{
	for (int i = 0; i < len; i++)
	{
		insert(i, i);
	}
}

void SingleList::insert(int index,int data)
{
	Node * p_head = &head;
	while (index --)
	{
		p_head = p_head->next;
	}
	Node * nodeTemp = new Node;
	nodeTemp->data = data;
	nodeTemp->next = p_head->next;
	p_head->next = nodeTemp;
	length++;
}

void SingleList::Delete(int index)
{
	Node * p_head = &head;
	index++;
	while (index --)
	{
		p_head = p_head->next;
	}
	Node * tmpPtr = p_head->next;
	p_head->next = p_head->next->next;
	delete tmpPtr;
	length--;
}

int SingleList::find(int index)
{
	if (index < 0 || index > length) {
		return 0;
	}
	Node * tmpPtr = head.next;
	while (index --)
	{
		tmpPtr = tmpPtr->next;
	}
	return tmpPtr->data;
}

int SingleList::size()
{
	return length;
}

void SingleList::print()
{
	Node * p_head = head.next;
	while (p_head->next != NULL)
	{
		cout << p_head->data << " ";
		p_head = p_head->next;
	}
}

SingleList::~SingleList()
{
}

좋은 웹페이지 즐겨찾기