Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef struct node {
  5.         int value;
  6.         struct node * next;}node_t;
  7.  
  8. node_t* addHead(){
  9.  
  10.     node_t* h = malloc(sizeof(node_t));
  11.     if(h == NULL) return NULL;
  12.     h->value=1;
  13.     h->next=NULL;
  14.     return h;
  15. }
  16. void addNode (int val,node_t* head)
  17. {
  18.     node_t * newNode =NULL;
  19.     newNode = malloc(sizeof(node_t));
  20.     if(newNode==NULL) return;
  21.     newNode->value=val;
  22.     newNode->next=NULL;
  23.     while(head->next!=NULL)
  24.         head=head->next;
  25.     head->next=newNode;
  26. }
  27. void printNodes (node_t* head)
  28. {
  29.     if(head==NULL)return;
  30.     while(head)
  31.     {
  32.         printf("%d ",head->value);
  33.         head=head->next;
  34.     }
  35.     printf("\n");
  36. }
  37. void removeNode(int val,node_t* head)
  38. {
  39.     node_t * second;
  40.     second = head->next;
  41.     node_t * third;
  42.     third = second->next;
  43.  
  44.     while(third){
  45.          if(second->value==val)
  46.         {
  47.             head->next=third;
  48.             free(second);
  49.             return;
  50.         }
  51.         head=second;
  52.         second=head->next;
  53.         third=second->next;
  54.     }
  55.     if(second->value==val)
  56.     {
  57.         head->next=NULL;
  58.         free(second);
  59.         return;
  60.     }
  61.  
  62.  
  63. }
  64. node_t* removeHead(node_t* head){
  65.     node_t * second;
  66.     second=head;
  67.     head=head->next;
  68.     free(second);
  69.     return head;
  70. }
  71.  
  72.  
  73. int main()
  74. {
  75.     node_t * head;
  76.     head=addHead();
  77.     addNode(5,head);
  78.     addNode(7,head);
  79.     printNodes(head);
  80.     head=removeHead(head);
  81.     addNode(9,head);
  82.     addNode(10,head);
  83.     addNode(2,head);
  84.     removeNode(7,head);
  85.     printNodes(head);
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement