您当前的位置: 首页 >  链表

phymat.nico

暂无认证

  • 4浏览

    0关注

    1967博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

c++实现单链表创建,删除,遍历,插入,修改操作

phymat.nico 发布时间:2017-05-09 18:23:19 ,浏览量:4


#include    
#include  
#include    
using namespace std;    

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

struct Node* build_list(int n)   
{    
	Node *head =(struct Node*)malloc(sizeof(Node));
	if (head == NULL)
	{
		coutnext = NULL;
	}
	return head;
}

struct Node* addprev_node_list(struct Node* head, struct Node* newNode,int key)
{
	if (head == NULL || newNode == NULL)
	{
		return head;
	}
	struct Node* pcurrNode = head;
	struct Node* pprevNode = NULL;
	while(pcurrNode != NULL)    
	{
		if (pcurrNode->data == key)
		{
			if (pcurrNode == head)
			{
				newNode->next = head;
				head = newNode;
			}
			else
			{
				pprevNode->next = newNode;
				newNode->next = pcurrNode;
			}
			break;
		}
		pprevNode = pcurrNode;
		pcurrNode = pcurrNode->next;    
	}
	return head;
}

struct Node* modkey_node_list(struct Node* head, int key, int modval)
{
	if(head==NULL)  
	{
		return head;  
	}

	struct Node* pCurrNode=head;  
	while(pCurrNode!=NULL)  
	{  
		if (pCurrNode->data == key)
		{
			pCurrNode->data = modval;
			break;
		}
		pCurrNode=pCurrNode->next;  

	} 

	return head;
}

struct Node* query_node_list(struct Node* head, int key)
{
	if(head==NULL)  
	{
		return head;  
	}

	struct Node* pCurrNode=head;   
	while(pCurrNode!=NULL)  
	{  
		if (pCurrNode->data == key)
		{
			return pCurrNode;
		}
		pCurrNode=pCurrNode->next;  

	} 

	return NULL;
}
struct Node* delete_node_list(struct Node* head, int key)   
{  
	if(head==NULL)  
	{  
		coutnext;
			pCurrNode->next = NULL;
			free(pCurrNode);

			bfind = true;
			break;
		} 
		pPrevNode = pCurrNode;
		pCurrNode=pCurrNode->next;  

	} 
	if (!bfind)
	{
		cout            
关注
打赏
1659628745
查看更多评论
0.0434s