Advertisement
Guest User

C++ Double Linked List

a guest
Apr 25th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #define null ((void *)0)
  5.  
  6. typedef long long LL;
  7. typedef struct _model {
  8.     char NIM[11];
  9.     char name[200];
  10.     char gender;
  11.     LL income;
  12.     struct _model *next;
  13.     struct _model *prev;
  14. } Model;
  15.  
  16. Model *head = null;
  17. Model *tail = null;
  18. Model *current = null;
  19.  
  20. Model* create_new(const char NIM[11], const char name[200], const char& gender, const LL income) {
  21.     Model *temp = (Model*)malloc(sizeof(Model));
  22.     memcpy(temp->name, name, (strlen(name) + 1) * sizeof(char));
  23.     temp->gender = gender;
  24.     temp->income = income;
  25.     temp->next = temp->prev = null;
  26.    
  27.     return temp;
  28. }
  29.  
  30. void push_head() {
  31.     current->next = head;
  32.     head->prev = current;
  33.     head = current;
  34.     head->prev = null;
  35. }
  36. void push_tail() {
  37.     tail->next = current;
  38.     current->prev = tail;
  39.     tail = current;
  40.     tail->next = null;
  41. }
  42. void add(const char NIM[11], const char name[200], const char& gender, const LL income) {
  43.     current = create_new(name, gender, income);
  44.     if(head == null) {
  45.         head = tail = current;
  46.     } else if(head == tail) {
  47.         if(income > head->income) {
  48.             push_head();
  49.         } else {
  50.             push_tail();
  51.         }
  52.     } else {
  53.         Model *temp = head;
  54.         while(temp != null) {
  55.             if(income <= temp->income) {
  56.                 temp = temp->next;
  57.             } else {
  58.                 break;
  59.             }
  60.         }
  61.         if(temp == head) {
  62.             push_head();
  63.         } else if(temp == null) {
  64.             push_tail();
  65.         } else {
  66.             Model *before = temp->prev;
  67.             before->next = current;
  68.             current->prev = before;
  69.             current->next = temp;
  70.             temp->prev = current;
  71.         }
  72.     }
  73. }
  74.  
  75. void main_menu() {
  76.    
  77. }
  78. int main() {
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement