Dibawah ini adalah Coding C++/CPP yang digunakan untuk Delete Node.
#include<iostream>
using namespace std;
struct NodeType
{
int data;
NodeType *next;
};
void InsertNode(int data, NodeType **L)
{
NodeType *New, *Point;
New = new NodeType;
New->data=data;
New->next=NULL;
if(*L == NULL)
{
*L = New;
}
else
{
Point = *L;
while(Point ->next != NULL)
{
Point = Point->next;
}
Point->next = New;
}
}
void PrintList(NodeType *L)
{
NodeType *Node;
Node = L;
while (Node != NULL)
{
cout<< (Node->data);
Node = Node->next;
if(Node != NULL)
cout<< ", ";
}
cout<<endl;
}
int CountNode(NodeType *L)
{
NodeType *Node;
Node = L;
int jum=0;
while(Node != NULL)
{
++jum;
Node = Node->next;
}
return jum;
}
void DeleteNode(int PosNode,NodeType **L)
{
NodeType *Prev, *Curr;
if (PosNode > CountNode(*L))
cout<<"node ke- "<<PosNode<<" tidak ada"<<endl;
else
{
if((PosNode == 1) && ((*L)->next == NULL))
{
delete *L;
*L = NULL;
}
else
{
Prev = *L;
Curr = *L;
for(int i=1; i<PosNode; i++)
{
Prev =Curr;
Curr=Curr->next;
}
if(PosNode == CountNode (*L))
Prev->next = NULL;
else if (PosNode == 1)
*L = Curr->next;
else
Prev->next = Curr->next;
delete Curr;
}
}
}
NodeType* CopyList(NodeType *L)
{
NodeType *NewList, *CurrList;
if(L->next == NULL) return NULL;
else
{
NewList = NULL;
CurrList = L;
while(CurrList != NULL)
{
InsertNode(CurrList ->data, &NewList);
CurrList = CurrList->next;
}
}
return NewList;
}
int main()
{
NodeType *List;
List = NULL;
for(int i=0; i<10; i++)
InsertNode(i, &List);
PrintList(List);
DeleteNode(3, &List);
DeleteNode(3, &List);
PrintList(List);
NodeType *List2;
List2 = CopyList(List);
PrintList(List2);
system("pause");
return 0;
}
Diatas merupakan Coding C++/CPP yang digunakan Untuk delete Node, Semoga Bermanfaat.
Tidak ada komentar:
Posting Komentar