SHARE
TWEET

Untitled

a guest Jun 6th, 2015 236 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct numberNode
  6. {
  7.         int number;
  8.         struct numberNode* Next;
  9. };
  10.  
  11. typedef struct numberNode numStruct;
  12.  
  13.  
  14. numStruct* createSong(int numbers)
  15. {
  16.         numStruct* newSong = (numStruct*)malloc(sizeof(numStruct));
  17.         if (newSong)
  18.         {              
  19.                 newSong->number = numbers;
  20.                 newSong->Next = NULL;
  21.         }
  22.         return newSong;
  23. }
  24.  
  25.  
  26.  
  27. void insertAtEnd(numStruct** firstNode, numStruct* newNode)
  28. {
  29.         numStruct* currNode = *firstNode;
  30.  
  31.         // if the linked list is empty
  32.         // should put the new node as the first
  33.         if (!currNode)
  34.         {
  35.                 *firstNode = newNode;
  36.                 newNode->Next = NULL;
  37.         }
  38.         else
  39.         {
  40.                 while (currNode->Next)  // problem at the second loop
  41.                 {
  42.                         currNode = currNode->Next;
  43.                 }
  44.  
  45.                 currNode->Next = newNode;
  46.                 newNode->Next = NULL;
  47.         }
  48.         printf("\n\n");
  49. }
  50.  
  51.  
  52. numStruct* AddSongs(numStruct* anchorNode, numStruct* newNode)
  53. {
  54.  
  55.         int count = 0;
  56.         int numbers;
  57.  
  58.         printf("\nPlease enter numbers and at the end enter -999\n");
  59.        
  60.         do
  61.         {
  62.                 count++;
  63.                 _flushall();
  64.                 numbers = 0;
  65.  
  66.                 printf("\n\n%d.\n\nnumber: ", count);
  67.                 scanf("%d",&numbers);
  68.  
  69.                 if (numbers != -999)
  70.                 {
  71.  
  72.                         newNode = createSong(numbers);
  73.                         insertAtEnd(&anchorNode, newNode);
  74.                 }
  75.         }
  76.         while (numbers != -999);
  77.         printf("\n\n");
  78. }
  79.  
  80.  
  81.  
  82.  
  83. int main(void)
  84. {
  85. numStruct* anchorNode = NULL;
  86. numStruct* newNode;    
  87. AddSongs(&anchorNode, &newNode);
  88.  
  89. system("PAUSE");
  90. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top