Advertisement
Guest User

Untitled

a guest
May 26th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct listNode {
  5.     int number;
  6.     struct listNode *nextPtr;
  7. };
  8.  
  9. typedef struct listNode LISTNODE;
  10. typedef LISTNODE *LISTNODEPTR;
  11. // insert new number into ordered list
  12. void insert(LISTNODEPTR *, int);
  13.  
  14. // remove number(s) from list, returns 1 if match is found, 0 otherwise
  15. int delete(LISTNODEPTR *, int);
  16.  
  17. // print all numbers in list in a loop
  18. void printList(LISTNODE *);
  19.  
  20. // returns 1 if list is empty, 0 otherwise
  21. int isEmpty(LISTNODE *);
  22.  
  23. int main ()
  24. {
  25.     //LISTNODE b = {24,NULL};
  26.     LISTNODE a = {1,NULL};
  27.     LISTNODEPTR ptr = &a;
  28.     printList(&a);
  29.     insert(&ptr,23);
  30.     insert(&ptr,10);
  31.     insert(&ptr,12);
  32.     insert(&ptr,15);
  33.     printList(&a);
  34.     return 0;
  35. }
  36. void printList(LISTNODE *listPtr){
  37.     printf("\nprinting list:\n");
  38.     while(1){
  39.         printf("%d", (*listPtr).number);
  40.         if((*listPtr).nextPtr != NULL){
  41.             listPtr = (*listPtr).nextPtr;
  42.             printf("-");
  43.         }else{
  44.             break;
  45.         }
  46.     }
  47. }
  48. void insert(LISTNODEPTR *list, int number){
  49.     if((**list).nextPtr == NULL){
  50.     printf("nextp == null %d\n",number);
  51.         LISTNODE *newNode =  malloc(sizeof(LISTNODE));
  52.         (*newNode).number  = number;
  53.         (*newNode).nextPtr  =  NULL;
  54.         (**list).nextPtr = newNode;
  55.    
  56.     }else{
  57.     printf("other %d\n",number);
  58.         LISTNODE *listPtr = *list;
  59.         LISTNODE *listPtr1 = (*listPtr).nextPtr;
  60.         while((*listPtr1).number < number && (*listPtr).nextPtr != NULL ){
  61.             printf("%d > %d\n",(*listPtr).number , number);
  62.             listPtr = (*listPtr).nextPtr;  
  63.             listPtr1 = (*listPtr).nextPtr;             
  64.         }      
  65.         LISTNODE *newNode =  malloc(sizeof(LISTNODE));
  66.         (*newNode).number  = number;
  67.         if((*listPtr).nextPtr != NULL){
  68.             (*newNode).nextPtr  =  listPtr1;           
  69.         }else{
  70.             (*newNode).nextPtr  =  NULL;
  71.         }
  72.         (*listPtr).nextPtr = newNode;
  73.     }
  74.    
  75.    
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement