Write a c program that creates a linked list of 5 elementsSo

Write a c++ program that creates a linked list of 5 elements.

Solution

#include <iostream>
using namespace std;

// using structure to implement singly linkedlist
// Singly linked list means one link should be there to point to next link
// structure holding an int variable and pointer to structure
struct node {
int data;
node *next;
};

int main()
{
// creating a structure nod
struct node *nod;
int i=0;
nod = new node; // now it will point to some location

// looping 5 times since we need 5 elements in linked list
for(i=1;i<6;i++)
{
nod->next = 0; // setting the next of the nod to 0
nod->data = i; // setting the data of the nod to i
printf(\"%d \",nod->data); // printing the data in nod structure that is singly linked list
}
}

---------------------------------------------------------------------
SAMPLE OUTPUT

1 2 3 4 5

Write a c++ program that creates a linked list of 5 elements.Solution#include <iostream> using namespace std; // using structure to implement singly linke

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site