Pengertian kelas besar tentang head, curr, tail



  The topic I learn today in Big class is about the concept position of where will our pointer be directed at when we have head, curr, and tail.

  Let say we have a singly link list that we want to make a push insertion.To make it happen we need to know where our address is first before we start making push insertion. The push insertion will be pushing from the front(beginning) and make a pop will be delete address.

struct node{
  int data; // value
  struct node *next, *prev; // address
}*head,*curr,*tail;

void push(int a)
{
    curr = (struct Data*)malloc(sizeof(struct Data));//create address
    curr->value = a;//insert value input a
   
    if(head==NULL){// if head don have any address then we create one
        head = tail = curr;
    }
    else{
        tail->next = curr;// move tail to curr
        curr->prev = tail;// move curr to tail
        tail = curr;// then we have tail and curr swap position
    }
    head->prev = tail->next = NULL;// if we create something like 10->20->NULL->40 it will print output of 10->20->0->40
}


void pop()// delete from front to behind
{
    curr = head;// start the curr from the head
   
    if(tail == head){
        free(curr);
        tail = head = curr = NULL;// if tail and head pointing same address it will be deleted
    }
    else
    {
        while(curr->next!=tail){
            curr = curr->next;// walk until finding the last input
        }
        free(tail); // delete value and memory
        tail = curr;// the curr last position become the tail position
        tail->next = NULL;// the delete value get null input
    }
}


source:
https://codeforwin.org/2015/09/c-program-to-delete-last-node-of-singly-linked-list.html

Blog create by Anthony
Date:03/3/2020

Comments

Popular posts from this blog

AVL TREE

Binary Search Tree(BNS)