C programming for linux Generate a linked list of 30 random

C programming for linux

Generate a linked list of 30 random numbers from 0 to 10. Explain what is going on in the linked list.

Solution

#include <stdio.h> // Header files

#include <stdlib.h>

#include<conio.h>

struct node{ // Node Struture

int data; // node consits of one data element of integer type

struct node *next; // next node pointer

}*start=NULL; // initially starting with null

void creat(){ // Node creating function

char c; // Declaring required variables

for (c = 1; c <= 30; c++){ // for 30 random numbers

struct node *new_node,*current; // variables for node

new_node=(struct node *)malloc(sizeof(struct node));// dynamic memory allocation for node

new_node->data = rand() % 10 + 1; // random numbers between 1 to 10 will be generated and assigned to a node in each iterartion

new_node->next=NULL; // and next to node to null

if(start==NULL){ // if first node created

start=new_node; // that first node will be start

current=new_node;

}

else{ // else go attach at end

current->next=new_node;

current=new_node;

}

}

}

void display(){ // displaying og linked list function

struct node *new_node; // node variables

printf(\"The Linked List : \ \");

new_node=start; // let new node be start

while(new_node!=NULL){ /// till last

printf(\"%d->\",new_node->data);// print the data

new_node=new_node->next;// goto next node

}

}

main() {// main function

creat(); // creating nodes

display(); // displaying it

getch();

}

C programming for linux Generate a linked list of 30 random numbers from 0 to 10. Explain what is going on in the linked list.Solution#include <stdio.h> /
C programming for linux Generate a linked list of 30 random numbers from 0 to 10. Explain what is going on in the linked list.Solution#include <stdio.h> /

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site