Advertisement
ItzhakEfraimov

Untitled

May 17th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.74 KB | None | 0 0
  1. void AddNewItem(PItem* head, PItem* tail, int n, int a, float b)
  2. {
  3.     PItem curr, prev = NULL, temp = (PItem)malloc(sizeof(struct Item));
  4.     int pos = 1;
  5.     if (temp == NULL)
  6.     {
  7.         DeleteList(head);
  8.         Error_Msg("Memmory!");
  9.     }
  10.     temp->num = a;
  11.     temp->price = b;
  12.     curr = *head;
  13.     if (head != NULL)
  14.     {
  15.         while (curr->next != NULL && pos != n)
  16.         {
  17.             prev = curr;
  18.             curr = curr->next;
  19.             pos++;
  20.         }
  21.         if (pos == 1) // Set NewItem as head
  22.         {
  23.             temp->next = *head;
  24.             *head = temp;
  25.         }
  26.         else if (curr->next == NULL && n == pos + 1) // Set NewItem as tail
  27.         {
  28.             (*tail)->next = *tail;
  29.             *tail = temp;
  30.         }
  31.         else if (n > pos + 1)
  32.             return; //Position not valid
  33.         else
  34.         {
  35.             prev->next = temp;
  36.             temp->next = curr;
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement