Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <regex.h>
- typedef struct Node{
- int n;
- struct Node* next
- } N;
- int rem(N** head, int key);
- void pl(N* node);
- int main()
- {
- N* head = (N*)calloc(1, sizeof(N));
- head->next = (N*)calloc(1, sizeof(N));
- head->next->next = (N*)calloc(1, sizeof(N));
- head->next->next->next = (N*)calloc(1, sizeof(N));
- head->next->next->next->next = (N*)calloc(1, sizeof(N));
- //
- head->n = 1;
- head->next->n = 2;
- head->next->next->n = 3;
- head->next->next->next->n = 4;
- head->next->next->next->next->n = 5;
- pl(head);
- //delete first item
- rem(&head, 1);
- pl(head);
- //delete last
- rem(&head, 5);
- pl(head);
- //del mid
- rem(&head, 3);
- pl(head);
- return 0;
- }
- int rem(N** head, int key)
- {
- N** outside = head;
- N* temp;
- while((temp = *outside)){
- if(temp->n == key){
- *outside = temp->next;
- free(temp);
- return 1;
- } else {
- outside = &temp->next;
- }
- }
- return -1;
- }
- void pl(N* node){
- while(node){
- printf("[%d] ", node->n);
- node = node->next;
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement