0% found this document useful (0 votes)
2 views6 pages

assignment (1)

Uploaded by

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

assignment (1)

Uploaded by

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

#include <stdio.

h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

void insertAtBeginning(struct Node** head_ref, int new_data) {

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

new_node->data = new_data;

new_node->next = *head_ref;

*head_ref = new_node;

void insertAtEnd(struct Node** head_ref, int new_data) {

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

struct Node* last = *head_ref;

new_node->data = new_data;

new_node->next = NULL;

if (*head_ref == NULL) {

*head_ref = new_node;

return;

while (last->next != NULL)

last = last->next;

last->next = new_node;

void deleteNode(struct Node** head_ref, int key) {

struct Node* temp = *head_ref;


struct Node* prev = NULL;

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

*head_ref = temp->next;

free(temp);

return;

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

prev = temp;

temp = temp->next;

if (temp == NULL) return;

prev->next = temp->next;

free(temp);

void traverse(struct Node* node) {

while (node != NULL) {

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

node = node->next;

printf("NULL\n");

int main() {

struct Node* head = NULL;

insertAtEnd(&head, 1);

insertAtEnd(&head, 2);
insertAtEnd(&head, 3);

insertAtBeginning(&head, 0);

printf("Linked list after insertions: ");

traverse(head);

deleteNode(&head, 2);

printf("Linked list after deleting 2: ");

traverse(head);

return 0;

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

struct Node* prev;

};

struct Node* head = NULL;

struct Node* createNode(int data) {

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

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

newNode->prev = NULL;

return newNode;

void insertAtBeginning(int data) {

struct Node* newNode = createNode(data);

if (head == NULL) {

head = newNode;

return;

newNode->next = head;

head->prev = newNode;

head = newNode;

void insertAtEnd(int data) {

struct Node* newNode = createNode(data);

if (head == NULL) {

head = newNode;

return;

struct Node* temp = head;

while (temp->next != NULL)

temp = temp->next;

temp->next = newNode;

newNode->prev = temp;

void deleteNode(int data) {

struct Node* temp = head;


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

head = temp->next;

if (head != NULL)

head->prev = NULL;

free(temp);

return;

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

temp = temp->next;

if (temp == NULL)

return;

if (temp->next != NULL)

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

if (temp->prev != NULL)

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

free(temp);

void traverse() {

struct Node* temp = head;

while (temp != NULL) {

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

temp = temp->next;

printf("\n");

int main() {

insertAtBeginning(10);

insertAtEnd(20);

insertAtBeginning(5);
traverse();

deleteNode(10);

traverse();

deleteNode(5);

traverse();

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