Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct node {
- int data;
- struct node* link;
- };
- void print_data(struct node* head)
- {
- if (head == NULL)
- printf("Linked list is empty\n");
- int count = 0;
- struct node* ptr = NULL;
- ptr = head;
- printf("Before deleting first node: ");
- while (ptr != NULL)
- {
- printf("%d ", ptr->data);
- ptr = ptr->link;
- }
- }
- struct node* del_first(struct node* head)
- {
- if (head == NULL)
- printf("List is already empty\n");
- else
- {
- struct node* temp = head;
- head = head->link;
- free(temp);
- temp = NULL;
- }
- return head;
- }
- int main()
- {
- /* Imazine that head is the pointer of the first node of the linked list */
- 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;
- print_data(head);
- struct node *ptr;
- head = del_first(head);
- ptr = head;
- printf("\nAfter deleting first node: ");
- while (ptr != NULL)
- {
- printf("%d ", ptr->data);
- ptr = ptr->link;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement