Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  new2.c
Language: C/C++
Code:
#include <stdio.h>
#include <stdlib.h>

struct Node{
     int value;
     struct Node *next;
        }Node;

struct Node *head;


int insertElement(int value) {

     struct Node *current;
     
     for(current = head; current != NULL; current = current->next)

          if(current->value == value){
               return -1;
          }     else{
               current->next = malloc(sizeof(Node));
                     current->next->value = value;
                     current->next->next = NULL;
                           return value;
          }
          
}

int removeElement(void) {
     
     int tmp = 0;

     if(head == NULL){
     return -1;
     }
     else if(head->next == NULL){
          tmp=head->value;
             free(head);
          return tmp;
     }
     else{
          struct Node *current = head;
     while (current->next->next != NULL){
          current = current->next;
}
        tmp = current->next->value;
     free(current->next);
     current->next = NULL;
     return tmp;
     }
return -1;
}

int main (int argc, char* argv[]) {
     printf("insert 47: %dn", insertElement(47));
     printf("insert 11: %dn", insertElement(11));
     printf("insert 23: %dn", insertElement(23));
     printf("insert 11: %dn", insertElement(11));

     printf("remove: %dn", removeElement());
     printf("remove: %dn", removeElement());
     exit(EXIT_SUCCESS);
}

          
Comments: