-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
07d3c20
commit cec0064
Showing
1 changed file
with
0 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
|
||
typedef struct node { | ||
int data; | ||
struct node* east; | ||
struct node* west; | ||
struct node* north; | ||
} Node; | ||
|
||
Node* createNode(int data) { | ||
Node* newNode = (Node*) malloc(sizeof(Node)); | ||
newNode->data = data; | ||
newNode->east = newNode->west = newNode->north = NULL; | ||
return newNode; | ||
} | ||
|
||
// Function to dynamically fill the linked list with random data | ||
void fillLinkedList(Node** head, int size, int min, int max) { | ||
srand(time(NULL)); // seed random number generator | ||
Node* current = *head; | ||
for (int i = 0; i < size; i++) { | ||
int data = min + rand() % (max - min + 1); | ||
Node* newNode = createNode(data); | ||
if (current == NULL) { | ||
*head = newNode; | ||
current = *head; | ||
} else { | ||
current->east = newNode; | ||
current = current->east; | ||
} | ||
} | ||
} | ||
|
||
// Function to print the linked list | ||
void printLinkedList(Node* head) { | ||
Node* current = head; | ||
while (current != NULL) { | ||
printf("%d---> ", current->data); | ||
current = current->east; | ||
} | ||
printf("NULL\n"); | ||
} | ||
|
||
int main() { | ||
int size, min, max; | ||
printf("Enter the number of data items to generate: "); | ||
scanf("%d", &size); | ||
printf("Enter the minimum value of the range: "); | ||
scanf("%d", &min); | ||
printf("Enter the maximum value of the range: "); | ||
scanf("%d", &max); | ||
|
||
Node* head = NULL; | ||
fillLinkedList(&head, size, min, max); // fill the linked list with random nodes within the specified range | ||
printLinkedList(head); // print the linked list | ||
return 0; | ||
} |