Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Deleting the node at particular position */
- #include <stdio.h>
- #include <stdlib.h>
- struct node {
- int data;
- struct node* link;
- };
- void del_pos(struct node** head, int position)
- {
- struct node* current = *head;
- struct node* previous = *head;
- if (*head == NULL)
- printf("List is already empty\n");
- else if (position == 1)
- {
- *head = current->link;
- free(current);
- current = NULL;
- }
- else
- {
- while (position != 1)
- {
- previous = current;
- current = current->link;
- position--;
- }
- previous->link = current->link;
- free(current);
- current = NULL;
- }
- }
- int main()
- {
- struct node* head = NULL;
- head = malloc(sizeof(struct node));
- head->data = 45;
- head->link = NULL;
- struct node* current = NULL;
- current = malloc(sizeof(struct node));
- current->data = 90;
- current->link = NULL;
- head->link = current;
- struct node* third = NULL;
- third = malloc(sizeof(struct node));
- third->data = 98;
- third->link = NULL;
- head->link->link = third;
- printf("Before Deleting: ");
- printf("%d %d %d\n", head->data, current->data, third->data);
- struct node* ptr;
- int position = 2;
- del_pos(&head, position);
- ptr = head;
- printf("After deleting: ");
- while (ptr != NULL)
- {
- printf("%d ", ptr->data);
- ptr = ptr->link;
- }
- return 0;
- }
- /*
- Output:
- Before Deleting : 45 90 98
- After deleting : 45 98
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement