c++は単一チェーンテーブルの作成、削除、遍歴、挿入、修正操作を実現する
2935 ワード
#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)
{
cout<data = 1;
head->next = NULL;
Node *curNode = head;
for (int i=1;idata = i+1;
newNode->next = NULL;
curNode->next = newNode;
curNode = newNode;
}
return head;
}
struct Node* addtail_node_list(struct Node* head, struct Node* newNode)
{
if (head == NULL || newNode == NULL)
{
return head;
}
struct Node* pcurrNode=head;
while(pcurrNode->next != NULL)
{
pcurrNode = pcurrNode->next;
}
if (pcurrNode != NULL)
{
pcurrNode->next = newNode;
newNode->next = 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)
{
cout<data == key)
{
if(head==pCurrNode)
{
head=pCurrNode->next;
}
pPrevNode->next = pCurrNode->next;
pCurrNode->next = NULL;
free(pCurrNode);
bfind = true;
break;
}
pPrevNode = pCurrNode;
pCurrNode=pCurrNode->next;
}
if (!bfind)
{
cout<data<next;
}
cout<data = newvalue;
newNode->next = NULL;
cout<data = 7;
newNode1->next = NULL;
head = addprev_node_list(head,newNode1,8);
cout<