Data Structures Practical 13

Question 1
Write a function to insert a node at the end in a Singly Linked List.
linkedlist.c
C
				#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

struct Node
{
    int data;
    struct Node *next;
};

struct Node *start = NULL;

void insertAtEnd(int data);
void displayList();

void main()
{
    int choice, data;
    char ch;

    do
    {
        clrscr();
        printf("\n--- SINGLY LINKED LIST OPERATIONS ---\n");
        printf("1. Insert at End\n");
        printf("2. Display List\n");
        printf("3. Exit\n");
        printf("-------------------------------------\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice)
        {
        case 1:
            printf("Enter data to insert at end: ");
            scanf("%d", &data);
            insertAtEnd(data);
            break;
        case 2:
            displayList();
            break;
        case 3:
            exit(0);
        default:
            printf("Invalid choice! Please try again.\n");
        }
        printf("\nDo you want to continue(y/n)?");
        ch = getch();
    } while (ch == 'y' || ch == 'Y');
}

void insertAtEnd(int data)
{
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    struct Node *ptr = start;

    if (newNode == NULL)
    {
        printf("Memory allocation failed!\n");
        return;
    }
    
    newNode->data = data;
    newNode->next = NULL;

    if (start == NULL)
    {
        start = newNode;
        printf("%d inserted as the first node.\n", data);
        return;
    }

    while (ptr->next != NULL)
    {
        ptr = ptr->next;
    }
    
    ptr->next = newNode;
    printf("%d inserted at the end.\n", data);
}

void displayList()
{
    struct Node *ptr = start;
    if (start == NULL)
    {
        printf("The list is empty.\n");
        return;
    }
    printf("Linked List: ");
    while (ptr != NULL)
    {
        printf("%d -> ", ptr->data);
        ptr = ptr->next;
    }
    printf("NULL\n");
}

			
Output
Question 2
Write a function to check whether a singly linked list is a palindrome or not.
palindrom.c
C
				#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

struct Node 
{
    int data;
    struct Node *next;
};

struct Node *start1 = NULL; /* Original List */
struct Node *start2 = NULL; /* Reversed Copy List */

void insertAtEndList1(int data);
void insertAtBeginningList2(int data);
void displayList(struct Node *head);
void checkPalindromeApproach();

void main() 
{
    int choice, data;
    char ch;

    do {
        printf("\n--- PALINDROME CHECKER (TWO LISTS APPROACH) ---\n");
        printf("1. Insert Node (Original List)\n");
        printf("2. Display Original List\n");
        printf("3. Check if Palindrome\n");
        printf("4. Exit\n");
        printf("------------------------------------------------\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
        case 1:
            printf("Enter data to insert: ");
            scanf("%d", &data);
            insertAtEndList1(data);
            break;
        case 2:
            printf("Original ");
            displayList(start1);
            break;
        case 3:
            if (start1 == NULL) 
			{
                printf("The list is empty.\n");
            } 
			else 
			{
                checkPalindromeApproach();
            }
            break;
        case 4:
            exit(0);
        default:
            printf("Invalid choice! Please try again.\n");
        }
        printf("\nDo you want to continue(y/n)?");
        ch = getch();
    } while (ch == 'y' || ch == 'Y');
}

void insertAtEndList1(int data) 
{
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    struct Node *ptr = start1;

    if (newNode == NULL) 
	{
        printf("Memory allocation failed!\n");
        return;
    }
    newNode->data = data;
    newNode->next = NULL;

    if (start1 == NULL) 
	{
        start1 = newNode;
        printf("%d inserted into original list.\n", data);
        return;
    }

    while (ptr->next != NULL) 
	{
        ptr = ptr->next;
    }
    ptr->next = newNode;
    printf("%d inserted into original list.\n", data);
}

void insertAtBeginningList2(int data) 
{
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    if (newNode == NULL) 
	{
        return;
    }
    newNode->data = data;
    newNode->next = start2;
    start2 = newNode;
}

void checkPalindromeApproach() 
{
    struct Node *ptr1 = start1;
    struct Node *ptr2 = NULL;
    int flag = 0;

	start2=NULL;
    while (ptr1 != NULL) 
	{
        insertAtBeginningList2(ptr1->data);
        ptr1 = ptr1->next;
    }

    ptr1 = start1;
    ptr2 = start2;

    while (ptr1 != NULL && ptr2 != NULL) 
	{
        if (ptr1->data != ptr2->data) 
		{
            flag = 1; 
            break;
        }
        ptr1 = ptr1->next;
        ptr2 = ptr2->next;
    }

    if(flag==0)
		printf("\nList is palindrom");
	else
		printf("\nList is not palindrom");
}

void displayList(struct Node *head) 
{
    struct Node *ptr = head;
    if (head == NULL) {
        printf("Linked List: Empty\n");
        return;
    }
    printf("Linked List: ");
    while (ptr != NULL) {
        printf("%d -> ", ptr->data);
        ptr = ptr->next;
    }
    printf("NULL\n");
}
			
Output

Leave a Comment

Your email address will not be published. Required fields are marked *