0% found this document useful (0 votes)
20 views8 pages

Assignement Ds

Uploaded by

cssingh.2463
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)
20 views8 pages

Assignement Ds

Uploaded by

cssingh.2463
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/ 8

ASSIGNEMENT

WAP to implement singly linked list (insertion,deletion and traverse function)

#include <stdio.h>
#include <stdlib.h>

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

// Insert at the starting


void insertAtBeginning(struct Node** head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) return;
newNode->data = value;
newNode->next = *head;
*head = newNode;
printf("Inserted %d at the beginning.\n", value);
}

// Insert at the ending


void insertAtEnd(struct Node** head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (!newNode) return;
newNode->data = value;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next) temp = temp->next;
temp->next = newNode;
}
printf("Inserted %d at the end.\n", value);
}

// Delete a node by value


void deleteNode(struct Node** head, int value) {
struct Node *temp = *head, *prev = NULL;
while (temp && temp->data != value) {
prev = temp;
temp = temp->next;
}
if (!temp) return;
if (!prev) *head = temp->next;
else prev->next = temp->next;
free(temp);
printf("Deleted %d from the list.\n", value);
}
// Traversing
void traverseList(struct Node* head) {
struct Node* temp = head;
printf("List: ");
while (temp) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main() {
struct Node* head = NULL;
insertAtBeginning(&head, 10);
insertAtEnd(&head, 20);
insertAtBeginning(&head, 5);
traverseList(head);
deleteNode(&head, 10);
traverseList(head);
return 0;
}

OUTPUT:
WAP to implement doubly linked list(insertion,deletion and traverse function)
#include <stdio.h>
#include <stdlib.h>

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

// Function to create a new node


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;
}
// Function to insert a node at the starting
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
}
else {
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
}
}

// Function to insert a node at the ending


void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
}
else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
}

// Function to delete a node


void deleteNode(struct Node** head, int key) {
if (*head == NULL) {
return;
}

struct Node* temp = *head;


if (temp->data == key) {
*head = temp->next;
if (*head != NULL) {
(*head)->prev = NULL;
}
free(temp);
return;
}

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


temp = temp->next;
}

if (temp == NULL) {
return;
}
temp->prev->next = temp->next;
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
free(temp);
}

// Traversing
void traverse(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}

int main() {
struct Node* head = NULL;

insertAtBeginning(&head, 5);
insertAtBeginning(&head, 3);
insertAtEnd(&head, 7);
insertAtEnd(&head, 9);
printf("Doubly Linked List: ");
traverse(head);

deleteNode(&head, 3);

printf("Doubly Linked List after deletion: ");


traverse(head);

return 0;
}

OUTPUT:

Name:Prachi Saxena
Roll no:lateral entry

You might also like