链表
#include <iostream>
using namespace std;
struct stnode //stnode为结构体名字
{
//数据部分
int value;
//指针部分
stnode* pNext;//结构体类型的指针
};
stnode* g_phead = NULL; //链表头结点
//添加一个节点 从头部添加
void addnode(stnode** pphead, stnode* paddnode)
{
if (pphead == NULL)
{
return;
}
stnode* phead = *pphead;//一个指针 指向头结点的指针
if (phead == NULL)
{
//说明链表头为NULL,无结点
*pphead = paddnode;
return;
}
paddnode->pNext = phead;
*pphead = paddnode;
}
//插入一个节点
void InsertNode(stnode** ppHead,stnode* insertnode,stnode* node)
{
if (node==NULL||insertnode==NULL)
{
return;
}
insertnode->pNext = node->pNext;
node->pNext = insertnode;
}
//删除某一个节点
void deletenote(stnode** ppHead, int value)
{
if (ppHead == NULL || *ppHead == NULL)
{
return;
}
stnode*pCur = *ppHead;
if (pCur->value == value)
{
//头结点
stnode* pDeleteNode = pCur;
*ppHead = pCur->pNext;
delete pDeleteNode;
return;
}
while (pCur)
{
if (pCur->pNext != NULL && pCur->pNext->value == value)
{
//找到了,pCur为要删除结点的前一个结点
stnode* pDelNode = pCur->pNext;
pCur->pNext = pCur->pNext->pNext;
delete pDelNode;
return;
}
pCur = pCur->pNext;
}
}
//查找一个节点
stnode* FindNode(stnode*pHead, int value)
{
while (pHead)
{
if (pHead->value == value)
{
return pHead;
}
pHead = pHead->pNext;
}
return NULL;
}
//修改结点值
void AlterNode(stnode* pHead, int changeValue, int changeToValue)
{
stnode* node = FindNode(pHead, changeValue);
if (node == NULL)
{
return;
}
node->value = changeToValue;
}
int main()
{
stnode* pNodel = new stnode;
pNodel->value = 1;
pNodel->pNext = NULL;
stnode* pNode2 = new stnode;
pNode2->value = 2;
pNode2->pNext = NULL;
stnode* pNode3 = new stnode;
pNode3->value = 3;
pNode3->pNext = NULL;
addnode(&g_phead, pNodel);
addnode(&g_phead,pNode2);
addnode(&g_phead,pNode3);
}