Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Linked List 1 and 2
- 7A. Write a program to search the elements in the linked list and display the
- same
- #include <stdio.h>
- #include <stdlib.h> // for malloc and exit
- #include <conio.h> // for getch() (Turbo C-specific)
- struct node {
- int info;
- struct node *next;
- };
- int main() {
- struct node *new_node, *ptr, *begin = NULL;
- int item;
- int option;
- // Input first element
- new_node = (struct node*)malloc(sizeof(struct node));
- if (!new_node) {
- printf("Memory allocation failed\n");
- return 1; // Exit if malloc fails
- }
- printf("\nEnter the element: ");
- scanf("%d", &item);
- new_node->info = item;
- new_node->next = NULL;
- begin = new_node;
- ptr = begin;
- // Loop for entering more elements
- printf("\nDo you want to enter another element? 1.Yes, 2.No: ");
- scanf("%d", &option);
- while (option != 2) {
- new_node = (struct node*)malloc(sizeof(struct node));
- if (!new_node) {
- printf("Memory allocation failed\n");
- return 1; // Exit if malloc fails
- }
- printf("\nEnter the element: ");
- scanf("%d", &item);
- new_node->info = item;
- new_node->next = NULL;
- ptr->next = new_node;
- ptr = new_node;
- printf("\nDo you want to enter another element? 1.Yes, 2.No: ");
- scanf("%d", &option);
- }
- // Display the linked list
- printf("\nThe Linked List:\n");
- ptr = begin;
- while (ptr != NULL) {
- printf("\t%d", ptr->info);
- ptr = ptr->next;
- }
- // Search for an element
- printf("\nEnter the element to be searched: ");
- scanf("%d", &item);
- ptr = begin;
- while (ptr != NULL) {
- if (ptr->info == item) {
- printf("\nElement found in the list!\n");
- getch(); // Wait for user input before exiting (Turbo C-specific)
- return 0; // Exit successfully
- }
- ptr = ptr->next;
- }
- printf("\nThe element is not present in the list!\n");
- getch(); // Wait for user input before exiting (Turbo C-specific)
- return 0; // Exit successfully
- }
- 7B. Write a program to create a single linked list and display the node elements
- in the reverse order.
- #include <stdio.h>
- #include <stdlib.h> // for malloc and free
- #include <conio.h> // for getch() (Turbo C-specific)
- struct node {
- int info;
- struct node *next;
- };
- struct node* createNode(int data) {
- struct node *newNode = (struct node*)malloc(sizeof(struct node));
- if (!newNode) {
- printf("Memory allocation failed\n");
- exit(1);
- }
- newNode->info = data;
- newNode->next = NULL;
- return newNode;
- }
- void displayNormal(struct node *head) {
- struct node *ptr = head;
- printf("The Linked List in Normal Order:\n");
- while (ptr != NULL) {
- printf("%d ", ptr->info);
- ptr = ptr->next;
- }
- printf("\n");
- }
- void displayReverse(struct node *head) {
- if (head == NULL) {
- return;
- }
- displayReverse(head->next);
- printf("%d ", head->info);
- }
- int main() {
- struct node *head = NULL, *ptr = NULL;
- int item, option;
- do {
- printf("Enter an element to add to the list: ");
- scanf("%d", &item);
- struct node *newNode = createNode(item);
- if (head == NULL) {
- head = newNode;
- } else {
- ptr->next = newNode;
- }
- ptr = newNode;
- printf("Do you want to add another element? 1.Yes, 2.No: ");
- scanf("%d", &option);
- } while (option == 1);
- displayNormal(head);
- printf("The Linked List in Reverse Order:\n");
- displayReverse(head);
- printf("\n");
- // Free the memory allocated for the linked list
- ptr = head;
- while (ptr != NULL) {
- struct node *temp = ptr;
- ptr = ptr->next;
- free(temp);
- }
- getch(); // Wait for user input before closing (Turbo C-specific)
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement