0% found this document useful (0 votes)
10 views3 pages

DS File

Uploaded by

Riddhi Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

DS File

Uploaded by

Riddhi Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PROGRAM NO:

WAP FOR IMPLEMENTATION OF A LINK LIST


#include<stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

void insertAtBeginning(struct Node** head, int newData) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = newData;

newNode->next = *head;

*head = newNode; }

void printList(struct Node* head) {

struct Node* temp = head;

while (temp != NULL) {

printf("%d -> ", temp->data);

temp = temp->next;

printf("NULL\n");}

int main() {

struct Node* head = NULL;

int n, value, newValue;

char choice;

printf("Enter the number of elements in the list: ");

scanf("%d", &n);

for (int i = 0; i < n; i++) {


printf("Enter element %d: ", i + 1);

scanf("%d", &value);

insertAtBeginning(&head, value);

printf("Current Linked List: ");

printList(head);

do {

printf("Enter a new element to insert at the beginning: ");

scanf("%d", &newValue);

insertAtBeginning(&head, newValue);

printf("Updated Linked List: ");

printList(head);

printf("Do you want to insert another node at the beginning? (y/n): ");

scanf(" %c", &choice);

} while (choice == 'y' || choice == 'Y');

printf("Final Linked List: ");

printList(head);

return 0;}

OUTPUT:
Enter the number of elements in the list: 5

Enter element 1: 7

Enter element 2: 6

Enter element 3: 7

Enter element 4: 8

Enter element 5: 9

Current Linked List: 9 -> 8 -> 7 -> 6 -> 7 -> NULL

Enter a new element to insert at the beginning: 5

Updated Linked List: 5 -> 9 -> 8 -> 7 -> 6 -> 7 -> NULL

Do you want to insert another node at the beginning? (y/n): Y

Enter a new element to insert at the beginning: 22

You might also like