Link List
Link List
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
// only for the 1st Node
void initNode(struct Node *head,int n){
head->data = n;
head->next =NULL;
}
// apending
void addNode(struct Node *head, int n) {
Node *newNode = new Node;
newNode->data = n;
newNode->next = NULL;
}
return false;
}
/* reverse the list */
struct Node* reverse(struct Node** List)
{
Node *parent = *List;
Node *me = parent->next;
Node *child = me->next;
/* make parent as tail */
parent->next = NULL;
while(child) {
me->next = parent;
parent = me;
me = child;
child = child->next;
}
me->next = parent;
*List = me;
return *List;
}
/* Creating a copy of a linked list */
void copyLinkedList(struct Node *node, struct Node **pNew)
{
if(node != NULL) {
*pNew = new Node;
(*pNew)->data = node->data;
(*pNew)->next = NULL;
copyLinkedList(node->next, &((*pNew)->next));
}
}
/* Compare two linked list */
/* return value: same(1), different(0) */
int compareLinkedList(struct Node *node1, struct Node *node2)
{
static int flag;
/* both lists are NULL */
if(node1 == NULL && node2 == NULL) {
flag = 1;
}
else {
if(node1 == NULL || node2 == NULL)
flag = 0;
else if(node1->data != node2->data)
flag = 0;
else
compareLinkedList(node1->next, node2->next);
}
}
return flag;
delete tmpNode;
}
void display(struct Node *head) {
Node *list = head;
while(list) {
cout << list->data << " ";
list = list->next;
}
cout << endl;
cout << endl;
}
int main()
{
struct Node *newHead;
struct Node *head = new Node;
initNode(head,10);
display(head);
addNode(head,20);
display(head);
addNode(head,30);
display(head);
addNode(head,35);
display(head);
addNode(head,40);
display(head);
insertFront(&head,5);
display(head);
int numDel = 5;
Node *ptrDelete = searchNode(head,numDel);
if(deleteNode(&head,ptrDelete))
cout << "Node "<< numDel << " deleted!\n";
display(head);
cout << "The list is reversed\n";
reverse(&head);
display(head);
cout << "The list is copied\n";
copyLinkedList(head,&newHead);
display(newHead);
cout << "Comparing the two lists...\n";
cout << "Are the two lists same?\n";
if(compareLinkedList(head,newHead))
cout << "Yes, they are same!\n";
else
cout << "No, they are different!\n";
cout << endl;
numDel = 35;
ptrDelete = searchNode(newHead,numDel);
if(deleteNode(&newHead,ptrDelete)) {
cout << "Node "<< numDel << " deleted!\n";
#include <iostream>
using namespace std;
class LinkList{
private:
struct node{
int data;
node *next;
};
node *head;
public:
LinkList();
void insertData(int num);
void deleteData (int num);
void displayData();
int count();
~LinkList();
};
LinkList::LinkList()
{
head=NULL;
}
void LinkList::insertData(int num)
{
node *temp;
node *t;
if( head == NULL)
{
head = new node;
head->data = num;
head->next = NULL;
}
else
{
temp = head;
while(temp->next != NULL)
{
temp = temp->next;
}
t = new node;
t->data = num;
t->next = NULL;
temp->next = t;
}
}
void LinkList::deleteData(int num)
{
node *q, *r;
q= head;
if(q->data == num)
{
head = q->next;
delete q;
return;
}
r = q;
while(q!= NULL)
{
if(q->data == num )
{
r->next = q->next;
delete q;
return;
}
r = q;
q = q->next;
}
cout << Nilai << num << tidak ditemukan;
}
void LinkList::displayData()
{
node *q;
for(q=head; q != NULL; q=q->next)
{
cout<<q->data<<endl;
}
}
int LinkList::count()
{
node *q;
int c=0;
for( q=head ; q != NULL ; q = q->next )
c++;
return c;
}
LinkList::~LinkList()
{
node *q;
if(head == NULL)
{
return;
}
while(head != NULL)
{
q = head->next;
delete head;
head = q;
}
}
int main()
{ LinkList list;
int temp;
int pilihan;
while(1)
{
cout<<Link List Single<<endl;
cout<<1.Insert / Creation<<endl;
cout<<2.Delete Element<<endl;
cout<<3.View Element<<endl;
cout<<4.Count Element<<endl;
cout<<5.Exit<<endl;
cout<<Enter your choice:;
cin>>pilihan;
switch(pilihan)
{ cout<<endl;
case 1:
cout<<Insert Element Data;
cin>>temp;
list.insertData(temp);
break;
case 2:
cout<<Delete Element :;
cin>>temp;
list.deleteData(temp);
break;
case 3:
list.displayData();
break;
case 4:
cout<<Number Total Element at List : <<list.count()<<endl;
break;
case 5:
return 0;
}
}
}
[\code]