Advertisement
tom19960222

資結個人作業 - linked-list

Oct 21st, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #define INSERT 1
  5. #define SEARCH 2
  6. #define DELETE 3
  7. // #define DEBUG IHATEBUG
  8.  
  9. struct _account
  10. {
  11.     unsigned int ID, type, amount;
  12.     struct _account* next;
  13.     struct _account* prev;
  14. };
  15. typedef struct _account Account;
  16.  
  17. Account* AccountList;
  18. Account* AccountListTail;
  19.  
  20. void Append(Account* new);
  21. int Count();
  22. void Delete(Account* account);
  23. Account* GetAccountByIndex(int index);
  24. void Initial();
  25. void Insert(Account* account);
  26. Account* Last();
  27. Account* Search(int ID);
  28. Account* newAccount();
  29. void Print(Account* account);
  30. void PrintAll();
  31. int RandomAction();
  32. Account* RandomExistData();
  33. void sortList(int left, int right);
  34. void swapValue(Account* a1, Account* a2);
  35.  
  36. void Initial()
  37. {
  38.     AccountList = newAccount();
  39.     #ifdef DEBUG
  40.         printf ("Initial -- the AccountList is located at %X\n\n", AccountList);
  41.     #endif
  42.     AccountListTail = AccountList;
  43.     int i;
  44.     for(i = 0; i < 9; i++)
  45.     {
  46.         Account* tmp = newAccount();
  47.         Append(tmp);
  48.     }
  49.     sortList(0, Count()-1);
  50. }
  51.  
  52. /* 題目要求的部分 */
  53.  
  54. Account* Search(int ID)
  55. {
  56.     #ifdef DEBUG
  57.         printf ("Searching ID=%d\n", ID);
  58.     #endif
  59.     Account* it = AccountList;
  60.     while (it)
  61.     {
  62.         #ifdef DEBUG
  63.             printf ("Compare it->ID=%d with ID=%d\n", it->ID, ID);
  64.             printf ("it->next=%X\n", it->next);
  65.         #endif
  66.         if(it->ID == ID) return it;
  67.         else it = it->next;
  68.     }
  69.     #ifdef DEBUG
  70.         if(it)
  71.         {
  72.             printf ("Account found!\n");
  73.             Print(it);
  74.         }
  75.         else
  76.             printf ("Account not found.\n");
  77.     #endif
  78.     return NULL;   
  79. }
  80.  
  81. void Insert(Account* account)
  82. {
  83.     #ifdef DEBUG
  84.         printf ("Inserting the following item: \n");
  85.         Print(account);
  86.         printf("\n");
  87.     #endif
  88.     if(Search(account->ID)) // Item duplicated.
  89.     {
  90.         printf("Item duplicated.");
  91.         return;
  92.     }
  93.     else
  94.     {
  95.         Append(account);
  96.         sortList(0, Count()-1);
  97.     }
  98. }
  99.  
  100. void Delete(Account* account)
  101. {
  102.     #ifdef DEBUG
  103.         printf ("Deleting the following item: \n");
  104.         Print(account);
  105.         printf("\n");
  106.         printf ("Prev item: ");
  107.         if (account->prev) Print(account->prev);
  108.         else printf("Prev is NULL!\n");
  109.         printf("\n");
  110.         printf ("Next item: ");
  111.         if (account->next) Print(account->next);
  112.         else printf("Next is NULL!\n");
  113.         printf("\n");
  114.     #endif
  115.  
  116.     if(!AccountList)
  117.     {
  118.         printf ("Nothing in the list!");
  119.         return;
  120.     }
  121.     if(account == AccountList) // Delete the first item.
  122.     {
  123.         #ifdef DEBUG
  124.             printf ("Deleting the first item.\n");
  125.         #endif
  126.  
  127.         if(!(account->next)) // There is only one item in the list.
  128.         {
  129.             #ifdef DEBUG
  130.                 printf ("Only one item in the list.\n");
  131.             #endif
  132.             AccountList = NULL;
  133.             AccountListTail = NULL;
  134.             free(account);
  135.         }
  136.         else
  137.         {
  138.             account->next->prev = NULL;
  139.             AccountList = account->next;
  140.             free(account);
  141.         }
  142.     }
  143.     else if (account == AccountListTail) // Delete the last item.
  144.     {
  145.         #ifdef DEBUG
  146.             printf ("Deleting the last item.\n");
  147.         #endif
  148.         AccountListTail = account->prev;
  149.         account->prev->next = NULL;
  150.         free(account);
  151.     }
  152.     else
  153.     {
  154.         #ifdef DEBUG
  155.             printf ("The previous account's next before edit is %X", account->prev->next);
  156.         #endif
  157.         account->prev->next = account->next;
  158.         account->next->prev = account->prev;
  159.         #ifdef DEBUG
  160.             printf ("The previous account's next after edit is %X", account->prev->next);
  161.         #endif
  162.         free(account);
  163.     }
  164.  
  165. }
  166.  
  167. /* 題目要求的部分 */
  168.  
  169. /* 其他直接用到的部分 */
  170.  
  171. void Print(Account* account)
  172. {
  173.     #ifdef DEBUG
  174.         printf("This account is located at %X\n", account);
  175.         printf("next=%X  prev=%X\n", account->next, account->prev);
  176.     #endif
  177.     printf ("ID: %d\ntype: %d\namount: %d\n\n", account->ID, account->type, account->amount);
  178. }
  179.  
  180. void PrintAll()
  181. {
  182.     Account* it = AccountList;
  183.     #ifdef DEBUG
  184.         printf ("There are %d items in the list.\n", Count());
  185.         printf("The link of the whole list :\n");
  186.         while(it)
  187.         {
  188.             printf ("%d(%X) -> ", it->ID, it);
  189.             it = it->next;
  190.         }
  191.         printf ("\n");
  192.         it = Last();
  193.         printf("The link of the whole list (reversed) :\n");
  194.         while(it)
  195.         {
  196.             printf ("%d(%X) -> ", it->ID, it);
  197.             it = it->prev;
  198.         }
  199.         printf ("\n");
  200.         it = AccountList;
  201.         printf ("Dumping the list.\n");
  202.     #endif
  203.    
  204.     while(it)
  205.     {
  206.         Print(it);
  207.         it = it->next;
  208.     }
  209. }
  210.  
  211. int RandomAction()
  212. {
  213.     int action[] = {INSERT, SEARCH, DELETE};
  214.     return action[rand()%3];
  215. }
  216.  
  217. Account* RandomExistData()
  218. {
  219.     return GetAccountByIndex(rand()%Count());
  220. }
  221.  
  222. Account* newAccount()
  223. {
  224.     Account* newac = (Account*)malloc(sizeof(Account));
  225. RandomID:
  226.     newac->ID = rand()%(RAND_MAX-1000000000)+1000000000;
  227.     if(Search(newac->ID)) { printf ("Duplicated! Re-generating ID...\n"); goto RandomID; }
  228.     newac->type = rand()%90+10;
  229.     newac->amount = rand();
  230.     newac->next = NULL;
  231.     newac->prev = NULL;
  232.     return newac;
  233. }
  234.  
  235. /* 其他直接用到的部分 */
  236.  
  237. /* 內部用到的功能 */
  238. void Append(Account* newac)
  239. {
  240.     #ifdef DEBUG
  241.         printf ("Appending the following item to the list.\n");
  242.         Print(newac);
  243.         printf ("\n");
  244.     #endif
  245.     if(!AccountList) // Nothing in the list.
  246.     {
  247.         #ifdef DEBUG
  248.             printf ("Nothing in the list, point AccountList to the new item.\n");
  249.         #endif
  250.         AccountList = newac;
  251.         AccountListTail = newac;
  252.     }
  253.  
  254.     newac->prev = AccountListTail;
  255.     AccountListTail->next = newac;
  256.     AccountListTail = newac;
  257. }
  258.  
  259. void swapValue(Account* a1, Account* a2)
  260. {
  261.     Account* temp = newAccount();
  262.     temp->ID = a1->ID;
  263.     temp->type = a1->type;
  264.     temp->amount = a1->amount;
  265.     a1->ID  = a2->ID;
  266.     a1->type = a2->type;
  267.     a1->amount = a2->amount;
  268.     a2->ID = temp->ID;
  269.     a2->type = temp->type;
  270.     a2->amount = temp->amount;
  271.     free(temp);
  272. }
  273.  
  274. Account* GetAccountByIndex(int index)
  275. {
  276.     #ifdef DEBUG
  277.         printf ("Getting item at index %d\n", index);
  278.     #endif
  279.     int i = 0;
  280.     Account* it = AccountList;
  281.     for (i = 0; it && i < index; i++)
  282.         it = it->next;
  283.     if(!it) printf ("Index %d not found!\n", index);
  284.     return it;
  285. }
  286.  
  287. int Count()
  288. {
  289.     #ifdef DEBUG
  290.         printf ("Counting the number of item in the list.\n");
  291.     #endif
  292.     int count;
  293.     Account* it = AccountList;
  294.     for (count = 0; it; count++)
  295.         it = it->next;
  296.     #ifdef DEBUG
  297.         printf ("Count = %d\n", count);
  298.     #endif
  299.     return count;
  300. }
  301.  
  302. void sortList(int left, int right)
  303. {
  304.     #ifdef DEBUG
  305.         printf ("Sorting the list with left=%d, right=%d\n", left, right);
  306.     #endif
  307.  
  308.     if (left < right)
  309.     {
  310.         int pivot = GetAccountByIndex((left+right)/2)->ID;
  311.         int i = left - 1, j = right + 1;
  312.         while (i < j)
  313.         {
  314.             do ++i; while (GetAccountByIndex(i)->ID < pivot);
  315.             do --j; while (GetAccountByIndex(j)->ID > pivot);
  316.             if (i < j) swapValue(GetAccountByIndex(i), GetAccountByIndex(j));
  317.         }
  318.  
  319.         sortList(left, i-1);
  320.         sortList(j+1, right);
  321.     }
  322. }
  323.  
  324. Account* Last()
  325. {
  326.     return GetAccountByIndex(Count()-1);
  327. }
  328.  
  329. /* 內部用到的功能 */
  330.  
  331. int main()
  332. {
  333.     srand(time(NULL));
  334.     /* Test code. */
  335.  
  336.     // Initial();
  337.  
  338.     // printf("### Dumping the list.\n");
  339.     // PrintAll();
  340.     // putchar('\n');
  341.  
  342.     // printf("### There are %d items in the list.\n", Count());
  343.     // putchar('\n');  
  344.  
  345.     // printf ("### Print index 1\n");
  346.     // Print(GetAccountByIndex(1));
  347.     // putchar('\n');  
  348.  
  349.     // printf("--------------------------------\n");
  350.     // printf ("### Delete 1st item.\n");
  351.     // Delete(GetAccountByIndex(0));
  352.     // printf("### Dumping the list.\n");
  353.     // PrintAll();
  354.     // putchar('\n');      
  355.  
  356.     // printf ("### Delete last item.\n");
  357.     // Delete(GetAccountByIndex(Count()-1));
  358.     // printf("### Dumping the list.\n");
  359.     // PrintAll();
  360.     // putchar('\n');  
  361.  
  362.     // printf ("### Search ID=%d\n", GetAccountByIndex(0)->ID);
  363.     // Print(Search((GetAccountByIndex(0))->ID));
  364.     // putchar('\n');
  365.  
  366.     // printf("### There are %d items in the list.\n", Count());
  367.     // putchar('\n');
  368.  
  369.     // Account* newac = newAccount();
  370.     // printf ("### Created a new account: \n");
  371.     // Print(newac);
  372.     // printf("### Insert new account into the list.\n");
  373.     // Insert(newac);
  374.     // printf("### Dumping the list.\n");
  375.     // PrintAll();
  376.     // putchar('\n');
  377.  
  378.     // printf("### There are %d items in the list.\n", Count());
  379.     // putchar('\n');
  380.  
  381.     /* Test code. */
  382.  
  383.     printf ("Program start. -- Initializing.\n\n");
  384.     Initial();
  385.     printf("Initialize completed. Dumping the list.\n\n");
  386.     PrintAll();
  387.     printf("\n\n");
  388.  
  389.     int i;
  390.     Account* randomdata, *result;
  391.     for (i = 0; i < 10; i++)
  392.     {
  393.         switch(RandomAction())
  394.         {
  395.             case INSERT:
  396.                 printf ("Action insert choosed!\n");
  397.                 Account* newac = newAccount();
  398.                 printf("New account created.\n");
  399.                 Print(newac);
  400.                 printf("Inserting into the list.\n");
  401.                 Insert(newac);
  402.                 printf("Insert completed. Dumping the list.\n");
  403.                 PrintAll();
  404.             break;
  405.  
  406.             case SEARCH:
  407.                 printf ("Action search choosed!\n");
  408.                 randomdata = RandomExistData();
  409.                 if(!randomdata) { printf ("So bad! There is no data in the list!\n"); continue; }
  410.                 printf ("Going to search ID=%d\n", randomdata->ID);
  411.                 result = Search(randomdata->ID);
  412.                 if(result)
  413.                 {
  414.                     printf ("Account found!\n");
  415.                     Print(result);
  416.                 }
  417.                 else
  418.                     printf ("Account not found.\n");
  419.             break;
  420.  
  421.             case DELETE:
  422.                 printf ("Action delete choosed!\n");
  423.                 randomdata = RandomExistData();
  424.                 if(!randomdata) { printf ("So bad! There is no data in the list!\n"); continue; }
  425.                 printf ("Going to delete ID=%d\n", randomdata->ID);
  426.                 Delete(randomdata);
  427.                 printf ("Delete completed. Dumping the list.\n");
  428.                 PrintAll();
  429.             break;
  430.         }
  431.         printf ("\n");
  432.     }
  433.  
  434.     printf("Program end.\n");
  435.  
  436.     return 0;  
  437. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement