Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct Node{
- int data;
- struct Node *next;
- }node;
- node *head;
- void deletData(int data){
- node *current=head;
- node *previous;
- if(current->data==data){
- head=head->next;
- free(current);
- }
- else{
- while(current!=NULL && current->data!=data){
- previous=current;
- current=current->next;
- }
- previous->next=current->next;
- free(current);
- }
- }
- void printData(){
- node *current=head;
- printf("New node is:");
- while(current!=NULL){
- printf("%d ",current->data);
- current=current->next;
- }
- printf("\n");
- }
- int main()
- {
- int n;
- head=(node*)malloc(sizeof(node));
- head->data=5;
- head->next=(node*)malloc(sizeof(node));
- head->next->data=3;
- head->next->next=(node*)malloc(sizeof(node));
- head->next->next->data=7;
- head->next->next->next=(node*)malloc(sizeof(node));
- head->next->next->next->data=2;
- head->next->next->next->next=NULL;
- printf("Enter the data you want to delete: ");
- scanf("%d",&n);
- deletData(n);
- printData();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement