Pract 7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

#include <stdio.

h>

#include <stdlib.h>

typedef struct Node {

int data;

struct Node *next;

struct Node *prev;

} Node;

Node* createNode(int data) {

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

newNode->data = data;

newNode->next = NULL;

newNode->prev = NULL;

return newNode;

void insertNode(Node** head, int data) {

Node* newNode = createNode(data);

if (*head == NULL) {

*head = newNode;

} else {

Node* temp = *head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

newNode->prev = temp;

void deleteNode(Node** head, int data)

Node* temp = *head;

while (temp != NULL) {


if (temp->data == data) {

if (temp->prev != NULL) {

temp->prev->next = temp->next;

} else {

*head = temp->next;

if (temp->next != NULL) {

temp->next->prev = temp->prev;

free(temp);

return;

temp = temp->next;

printf("");

void printList(Node* head) {

while (head != NULL) {

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

head = head->next;

printf("NULL\n");

int main() {

Node* head = NULL;

int choice, data;

while (1) {

printf("1. Insert a node\n");

printf("2. Delete a node\n");

printf("3. Print the list\n");

printf("4. Exit\n");
printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the number to insert: ");

scanf("%d", &data);

insertNode(&head, data);

break;

case 2:

printf("Enter the number to delete: ");

scanf("%d", &data);

deleteNode(&head, data);

break;

case 3:

printList(head);

break;

case 4:

return 0;

default:

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

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy