数据结构之链表

8 篇文章 0 订阅
#ifndef NODE_H
#define NODE_H

class Node
{
public:
	int data;
	Node *next;
	void printNode();
	
};
#endif


#include "Node.h"
#include <iostream>
using namespace std;
void Node::printNode(){
	cout<<data<<endl;
}
#ifndef LIST_H
#define LIST_H
#include "Node.h"
class List
{
public:
	List();
	~List();
	void ClearList();
	bool ListEmpty();
	int ListLength();
	bool GetElem(int i, Node *pNode);
	int locateElem(Node *pNode);
	bool PriorElem(Node *pcurrentNode, Node *pPreNode);
	bool NextElem(Node *pcurrentNode, Node *pNextNode);
	void ListTraverse();
	bool ListInsert(int i, Node *pNode);
	bool ListDelete(int i, Node *pNode);
	bool ListInsertHead(Node *pNode);
	bool ListInsertTail(Node *pNode);
private:
	Node *m_pList;
	int m_iLength;
	
};
#endif

#include "List.h"
#include "Node.h"
#include <iostream>
using namespace std;
List::List(){
	m_pList = new Node;
	m_pList->data = 0;
	m_pList->next = NULL;
	m_iLength = 0;
}
bool List::ListEmpty(){
	if(m_iLength == 0)
		return true;
	else
		return false;
}

int List::ListLength(){
	return m_iLength;
}

void List::ClearList(){
	Node *currentNode = m_pList->next;
	while(currentNode != NULL){
		Node *temp = currentNode->next;
		delete currentNode;
		currentNode = temp;
	}
	m_pList->next = NULL;
}


List::~List(){
	ClearList();
	delete m_pList;
	m_pList = NULL;
}

bool List::ListInsertHead(Node *pNode){
	Node *temp = m_pList->next;
	Node *newNode = new Node;
	if(newNode == NULL)
		return false;
	newNode->data = pNode->data;
	m_pList->next = newNode;
	newNode->next = temp;
	m_iLength++;
	return true;
}

bool List::ListInsertTail(Node *pNode){
	Node *currentNode  = m_pList;
	while(currentNode->next!=NULL){
		currentNode = currentNode->next;
	}
	Node *newNode = new Node;
	if(newNode == NULL)
		return false;
	newNode->data = pNode->data;
	newNode->next = NULL;
	currentNode->next = newNode;
	m_iLength++;
	return true;
}

bool List::ListInsert(int i, Node *pNode){
	if(i < 0 || i > m_iLength)
		return false;
	Node *currentNode = m_pList;
	for(int k = 0; k < i; k++){
		currentNode = currentNode->next;
	}
	Node *newNode = new Node;
	if(newNode == NULL)
		return false;
	newNode->data = pNode->data;
	newNode->next = currentNode->next;
	currentNode->next = newNode;
	m_iLength++;
	return true;

}

bool List::ListDelete(int i, Node *pNode){
	if(i < 0 || i >= m_iLength)
		return false;
	Node *currentNode = m_pList;
	Node *currentNodeBefore = NULL;
	for(int k = 0; k <= i; k++){
		currentNodeBefore = currentNode;
		currentNode = currentNode->next;
	}
	currentNodeBefore->next = currentNode->next;
	pNode->data = currentNode->data;
	delete currentNode;
	currentNode = NULL;
	m_iLength--;
}

int List::locateElem(Node *pNode){
	Node *currentNode = m_pList;
	int count = 0;
	while(currentNode->next != NULL){
		currentNode = currentNode->next;
		if(currentNode->data == pNode->data){
			return count;
		}
		count++;
	}
	return -1;
}

bool List::PriorElem(Node *pcurrentNode, Node *pPreNode){
	Node *currentNode = m_pList;
	Node *tempNode = NULL;
	while(currentNode->next != NULL){
		tempNode = currentNode;
		currentNode = currentNode->next;
		if(currentNode->data == pcurrentNode->data){
			if(tempNode == m_pList)
				return false;
			pPreNode->data = tempNode->data;
			return true;
		}
	}
	return false;

}
bool List::NextElem(Node *pcurrentNode, Node *pNextNode){
	Node *currentNode = m_pList;
	
	while(currentNode->next != NULL){
		
		currentNode = currentNode->next;
		if(currentNode->data == pcurrentNode->data){
			if(currentNode->next == NULL)
				return false;
			pNextNode->data = currentNode->next->data;
			return true;
		}
	}
	return false;
}

void List::ListTraverse(){
	Node *currentNode = m_pList;
	while(currentNode->next != NULL){
		currentNode = currentNode->next;
		currentNode->printNode();
	}
}













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


int main(){
	List *pList = new List();
	Node node1;
	Node node2;
	node1.data = 3;
	pList->ListInsertHead(&node1);
	pList->ListTraverse();
	cout<<pList->ListEmpty()<<endl;
	cout<<pList->ListLength()<<endl;

	delete pList;
	pList = NULL;
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值