Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deletion in linked list #1783

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions deletion_in_linked_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include<stdio.h>
#include<stdlib.h>

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

void linkedListTraversal(struct Node *ptr)
{
while(ptr != NULL){
printf("Element: %d\n", ptr->data);
ptr = ptr->next;
}
}

//------------------------------------------------------------------------------------------

struct Node * deleteFirst(struct Node * head){
struct Node * ptr = head;
head = head->next;
free(ptr);
return head;
}

struct Node * deleteAtIndex(struct Node * head, int index){
struct Node * p = head;
struct Node * q = head->next;

for (int i = 0; i < (index-1); i++)
{
p = p->next;
q = q->next;
}

p->next = q->next;
free(q);
return head;
}

struct Node * deleteAtEnd(struct Node * head){
struct Node * p = head;
struct Node * q = head->next;

while(q->next != NULL)
{
p = p->next;
q = q->next;
}

p->next = NULL;
free(q);
return head;
}

struct Node * deleteAtValue(struct Node * head, int value){
struct Node * p = head;
struct Node * q = head->next;

while(q->data != value && q->next != NULL)
{
p = p->next;
q = q->next;
}

if (q->data == value)
{
p->next = q->next;
free(q);
}
return head;
}

int main(){
struct Node *head;
struct Node *second;
struct Node *third;
struct Node *fourth;

// Allocate memory for nodes in the linkedlist.
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
fourth = (struct Node*)malloc(sizeof(struct Node));

// Link first and second nodes
head->data = 4;
head->next = second;

// Link second and third nodes
second->data = 3;
second->next = third;

// Link third and fourth nodes
third->data = 8;
third->next = fourth;

// Terminate the list at the third node
fourth->data = 1;
fourth->next = NULL;

printf("Linked List before traversal\n");
linkedListTraversal(head);

printf("Linked List after traversal\n");
// head = deleteFirst(head);
// head = deleteAtIndex(head, 2);
// head = deleteAtEnd(head);
head = deleteAtValue(head, 3);
linkedListTraversal(head);
return 0;
}