Deletion in LL
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *head = NULL;
push(&head,7);
push(&head,8);
push(&head,4);
push(&head,3);
printf("\n Linked List is:\n");
print(head);
delete(&head,8);
printf("\n LinkedList after Deletion of 8: \n");
print(head);
return 0;
}
void push(struct node** ref, int newData)
{
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = newData;
newNode->next = (*ref);
(*ref) = newNode;
}
void delete(struct node** headref,int key)
{
struct node* temp = *headref;struct node* prev;
if(temp != NULL && temp->data == key)
{
*headref = temp->next;
free(temp);
return;
}
while(temp != NULL && temp->data != key)
{
prev = temp;
temp = temp->next;
}
prev->next = temp->next;
free(temp);
}
void print(struct node *n)
{
while(n!= NULL)
{
printf("%d",n->data);
n=n->next;
}
}
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *head = NULL;
push(&head,7);
push(&head,8);
push(&head,4);
push(&head,3);
printf("\n Linked List is:\n");
print(head);
delete(&head,8);
printf("\n LinkedList after Deletion of 8: \n");
print(head);
return 0;
}
void push(struct node** ref, int newData)
{
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = newData;
newNode->next = (*ref);
(*ref) = newNode;
}
void delete(struct node** headref,int key)
{
struct node* temp = *headref;struct node* prev;
if(temp != NULL && temp->data == key)
{
*headref = temp->next;
free(temp);
return;
}
while(temp != NULL && temp->data != key)
{
prev = temp;
temp = temp->next;
}
prev->next = temp->next;
free(temp);
}
void print(struct node *n)
{
while(n!= NULL)
{
printf("%d",n->data);
n=n->next;
}
}
Comments
Post a Comment