单链表(c++)

//main.cpp

#include <iostream>
#include "singleLinkList.h"

using namespace std;

int main(int argc,char * argv[])
{
	SingleLink sl;
	int value;
	int i;
	 cout<<"\t\t\t\t\t单链表\n";    
    while(1)    
    {    
        cout<<"********************************************************************************";    
        cout<<"\t\t\t\t1添加元素\n\t\t\t\t2遍历链表\n\t\t\t\t3查找记录\n\t\t\t\t4插入元素\n\t\t\t\t5删除元素\n\t\t\t\t6清屏\n\t\t\t\t7退出\n";    
        cout<<"********************************************************************************";      
		value = sl.GetValue(); 
        switch(value)  
        {  
        case 1:  
            cout<<"\t\t\t1头插法建立单链表\n\t\t\t2尾插法建立单链表\n";  
			i = sl.GetValue();  
            if(i==1)  
            {  
				if(sl.AddNodeOne())
					cout<<"元素添加成功!\n";
				else
					cout<<"未能成功添加元素!\n";
            }  
            else if(i==2)  
            {  
                if(sl.AddNodeTwo())
					cout<<"元素添加成功!\n";
				else
					cout<<"未能成功添加元素!\n";
            }  
            else  
            {  
                printf("输入格式错误!\n");  
            }  
            break;  
        case 2:  
			//sl.TraverseList();
			cout<<sl;
            break;  
        case 3:  
			sl.FindValue();
            break;  
        case 4:  
			if(sl.InsertValue())
				cout<<"元素插入成功!\n";
            break;  
        case 5:  
			if(sl.DeleteValue())
				cout<<"元素删除成功!\n";
            break;  
        case 6:  
             system("cls");  
            break;  
        case 7:  
             exit(1);  
             break;  
        default:  
            cout<<"输入有误,从新输入!\n";  
            break;  
        }  
    }  
	return 0;
}

//singleLinkList.cpp

#include "singleLinkList.h"
using namespace std;

SingleLink::SingleLink()
{
	m_phead = NULL;
	m_count = 0;
}

SingleLink::~SingleLink()
{

}

bool SingleLink::AddNodeOne()
{
	LinkNode * pnode = new LinkNode();
	if(pnode == NULL)
		return false;
	int value = GetValue();
	if(m_phead == NULL)
	{
		pnode->SetNode(value,NULL);
	}
	else
	{
		pnode->SetNode(value,m_phead);
	}
	m_phead = pnode;
	m_count++;
	return true;
}

bool SingleLink::AddNodeTwo()
{
	LinkNode * pnode = new LinkNode();
	if(pnode == NULL)
		return false;
	int value = GetValue();
	if(m_phead == NULL)
	{
		m_phead = pnode;
	}
	else
	{
		LinkNode * ptail = m_phead;
		while(ptail->GetPData() != NULL)
		{
			ptail = ptail->GetPData();
		}
		ptail->SetPData(pnode);
	}
	pnode->SetNode(value,NULL);
	m_count++;
	return true;
}

void SingleLink::TraverseList()const
{
	if(m_phead == NULL)
	{
		cout<<"链表为空!\n";
		return;
	}
	LinkNode * pcurrent = m_phead;
	cout<<*pcurrent;
	while(pcurrent->GetPData() != NULL)
	{
		pcurrent = pcurrent->GetPData();
		cout<<*pcurrent;
	}
	cout<<endl;
	cout<<"元素总数为:"<<m_count<<endl;
}

bool SingleLink::FindValue()const
{
	int i = GetValue();
	int p=1;
	if(m_phead == NULL)
	{
		cout<<"链表为空!\n";
		return false;
	}
	LinkNode * pcurrent = m_phead;
	if(pcurrent->GetData() == i)
	{
		cout<<"该元素在链表中的位置为:"<<p<<endl;
		return true;
	}
	while(pcurrent->GetPData() != NULL)
	{
		pcurrent = pcurrent->GetPData();
		p++;
		if(pcurrent->GetData() == i)
		{
			cout<<"该元素在链表中的位置为:"<<p<<endl;
			return true;
		}
	}
	cout<<"单链表中无此元素!\n";
	return false;
}

bool SingleLink::InsertValue()
{
	int value = GetValue();
	int position;

	cout<<"在何处插入该元素:\n";
	bool b = true;
	while(b)
	{
		if(!(cin>>position))
		{
			cout<<"位置参数错误,请重新输入:\n";
			cin.clear();
		}
		else
		{
			b = false;
		}
		while(cin.get() != '\n')
			continue;
	}

	LinkNode * pcurrent = m_phead;
	int count=1;
	while(position != count && pcurrent->GetPData() != NULL)
	{
		count++;
		pcurrent = pcurrent->GetPData();
	}

	if(count == position)
	{
		if(count == 1)
		{
			LinkNode * pnode = new LinkNode;
			pnode->SetNode(value,m_phead);
			m_phead = pnode;
			m_count++;
		}
		else
		{
			LinkNode * pcurrent2 = m_phead;
			while(pcurrent2->GetPData() != pcurrent)
				pcurrent2 = pcurrent2->GetPData();
			LinkNode * pnode = new LinkNode;
			pnode->SetNode(value,pcurrent);
			pcurrent2->SetPData(pnode);
			m_count++;
		}
		return true;
	}

	if(pcurrent->GetPData() == NULL)
	{
		cout<<"您输入的位置不正确!\n";
		return false;
	}

	return false;
}

bool SingleLink::DeleteValue()
{
	int position;
	cout<<"输入你要删除元素的位置:\n";
	bool b = true;
	while(b)
	{
		if(!(cin>>position))
		{
			cout<<"位置参数错误,请重新输入:\n";
			cin.clear();
		}
		else
		{
			b = false;
		}
		while(cin.get() != '\n')
			continue;
	}

	LinkNode * pcurrent = m_phead;
	int count=1;
	while(position != count && pcurrent->GetPData() != NULL)
	{
		count++;
		pcurrent = pcurrent->GetPData();
	}

	if(position == count)
	{
		if(count == 1)
		{
			LinkNode * pcurrent2 = m_phead;
			m_phead = m_phead->GetPData();
			delete pcurrent2;
			m_count--;
		}
		else
		{
			LinkNode * pcurrent2 = m_phead;
			while(pcurrent2->GetPData() != pcurrent)
				pcurrent2 = pcurrent2->GetPData();
			pcurrent2->SetPData(pcurrent->GetPData());
			delete pcurrent;
			m_count--;
		}
		return true;
	}

	if(pcurrent->GetPData() == NULL)
	{
		cout<<"您输入的位置不正确!\n";
		return false;
	}

	return false;
}

int SingleLink::GetValue()const
{
	int value;
	bool b = true;
	cout<<"请输入你的值:\n";
	while(b)
	{
		if(!(cin>>value))
		{
			cout<<"你的输入有误,请重新输入:\n";
			cin.clear();
		}
		else
		{
			b = false;
		}
		while(cin.get() != '\n')
			continue;
	}
	return value;
}

//singleLinkList.h

#ifndef SINGLELINKLIST_H_
#define SINGLELINKLIST_H_
#include "linkNode.h"
#include <iostream>

class SingleLink
{
private:
	LinkNode * m_phead;
	int m_count;
public:
	SingleLink();
	~SingleLink();
	bool AddNodeOne();
	bool AddNodeTwo();
	bool InsertValue();
	bool DeleteValue();
	void TraverseList()const;
	friend std::ostream & operator<<(std::ostream & os,const SingleLink & sl){sl.TraverseList();return os;};
	bool FindValue()const;
	int GetValue()const;
};

#endif

//linkNode.h

#ifndef LINKNODE_H_
#define LINKNODE_H_
#include <iostream>

class LinkNode
{
private:
	int m_data;
	LinkNode * m_pdata;
public:
	LinkNode();
	void SetNode(int data,LinkNode * pdata);
	friend std::ostream & operator<<(std::ostream & os,const LinkNode & linknode);
	LinkNode * GetPData(){return m_pdata;};
	int GetData(){return m_data;};
	void SetPData(LinkNode * linkNode){m_pdata = linkNode;};
	void SetData(int value){m_data = value;};
};

#endif

//linkNode.cpp

#include "linkNode.h"

LinkNode::LinkNode()
{
	m_data = 0;
	m_pdata = NULL;
}

void LinkNode::SetNode(int data,LinkNode * pdata)
{
	m_data = data;
	m_pdata = pdata;
}

std::ostream & operator<<(std::ostream & os,const LinkNode & linknode)
{
	std::cout<<linknode.m_data<<"	";
	return os;
}

单链表是一种常见的数据结构,用于存储一系列的节点连接起来的数据。 在C语言中,我们可以使用结构体来定义一个节点,包含一个数据域和一个指向下一个节点的指针域。代码如下: ```c struct Node { int data; struct Node* next; }; ``` 然后,我们可以定义一个指向链表头部的指针,表示整个链表的起始位置。初始化链表时,这个指针可以指向NULL。 ```c struct Node* head = NULL; ``` 接下来,我们可以实现一些基本的操作,例如插入节点、删除节点、遍历链表等。 插入节点的过程包括创建一个新节点,并将其插入到链表中适当的位置。 ```c void insertNode(int value) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = value; newNode->next = NULL; if (head == NULL) { head = newNode; } else { struct Node* curr = head; while (curr->next != NULL) { curr = curr->next; } curr->next = newNode; } } ``` 删除节点的过程需要找到目标节点,并调整前后节点的指针。 ```c void deleteNode(int value) { if (head == NULL) { return; } if (head->data == value) { struct Node* temp = head; head = head->next; free(temp); return; } struct Node* prev = head; struct Node* curr = head->next; while (curr != NULL && curr->data != value) { prev = curr; curr = curr->next; } if (curr != NULL) { prev->next = curr->next; free(curr); } } ``` 遍历链表的过程是从头节点开始,依次打印每个节点的数据。 ```c void printList() { struct Node* curr = head; while (curr != NULL) { printf("%d ", curr->data); curr = curr->next; } printf("\n"); } ``` 这是一个简单的单链表的实现示例,你可以根据需要进一步扩展和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值