0% found this document useful (0 votes)
17 views

Assignment 6

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)
17 views

Assignment 6

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/ 6

ASSIGNMENT 6

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Node* head = NULL;

void createList(int value) {

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

newNode->data = value;

newNode->next = NULL;

if (head == NULL) {

head = newNode;

} else {

struct Node* temp = head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

void insertAtBeginning(int value) {

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

newNode->data = value;

newNode->next = head;

head = newNode;

void insertAtEnd(int value) {

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

newNode->data = value;
newNode->next = NULL;

if (head == NULL) {

head = newNode;

} else {

struct Node* temp = head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

void deleteNode(int value) {

struct Node* temp = head;

struct Node* prev = NULL;

if (temp != NULL && temp->data == value) {

head = temp->next;

free(temp);

return;

while (temp != NULL && temp->data != value) {

prev = temp;

temp = temp->next;

if (temp == NULL) return;

prev->next = temp->next;

free(temp);

void traverseList() {

struct Node* temp = head;

if (temp == NULL) {
printf("The list is empty.\n");

return;

printf("Elements in the list: ");

while (temp != NULL) {

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

temp = temp->next;

printf("NULL\n");

int main() {

int choice, value;

do {

printf("\nMenu:\n");

printf("1. Create List\n");

printf("2. Insert at Beginning\n");

printf("3. Insert at End\n");

printf("4. Delete Node\n");

printf("5. Traverse List\n");

printf("6. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter value to create the list: ");

scanf("%d", &value);

createList(value);

break;

case 2:

printf("Enter value to insert at beginning: ");

scanf("%d", &value);
insertAtBeginning(value);

break;

case 3:

printf("Enter value to insert at end: ");

scanf("%d", &value);

insertAtEnd(value);

break;

case 4:

printf("Enter value to delete: ");

scanf("%d", &value);

deleteNode(value);

break;

case 5:

traverseList();

break;

case 6:

printf("Exiting...\n");

break;

default:

printf("Invalid choice! Please try again.\n");

} while (choice != 6);

return 0;

OUTPUT:

You might also like