You are on page 1of 4

#include <iostream>

using namespace std;


struct Node
{
int info;
Node *next;
};
void initNode(struct Node *head, int n)
{
head->info = n;
head->next =NULL;
}
void addNode_end(struct Node *head, int n)
{
Node *new_node = new Node;
new_node->info = n;
new_node->next = NULL;
Node *temp = head;
while (temp != NULL)
{
if (temp->next == NULL)
{
temp->next = new_node;
return;
}
else
temp = temp->next;
}
}
void display(struct Node *head)
{
Node *temp = head;
cout << endl;
while (temp != NULL)
{
cout << " " << temp->info << " ->";
temp = temp->next;
}
cout << " NULL";
}
int main()
{
Node *head = new Node;
initNode(head,10);
display(head);
addNode_end(head,20);
display(head);
addNode_end(head,30);
display(head);
addNode_end(head,40);
display(head);

addNode_end(head,50);
display(head);
cout << endl;
return 0;
}

#include <iostream>
using namespace std;
typedef struct LLSI
{
int info;
LLSI *next;
}NODE;
void initNODE(NODE *head, int n)
{
head->info = n;
head->next =NULL;
}
void addNODE(NODE *head, int n)
{
NODE *new_node = new NODE;
new_node->info = n;
new_node->next = NULL;
NODE *temp = head;
while (temp != NULL)
{
if (temp->next == NULL)
{
temp->next = new_node;
return;
}
temp = temp->next;
}
}
void display(NODE *head)
{
NODE *temp;
temp = head;
cout << endl;
while (temp)
{
cout << " " << temp->info << " ->";
temp = temp->next;
}
cout << " NULL";
}
int main()
{
NODE *head;
head = new NODE;
initNODE(head,10);
display(head);
addNODE(head,20);
display(head);

addNODE(head,30);
display(head);
addNODE(head,40);
display(head);
addNODE(head,50);
display(head);
cout << endl;
return 0;
}

You might also like