Queue using array

#define MAX 10
int queue[MAX], front=-1, rear=-1;

void enqueue (int num)
{
  if(front == 0 && rear == MAX-1)
    printf("Queue OverFlow Occured");
  
  else if(front == -1 && rear == -1)
  {
      front = rear = 0;
      queue[rear] = num;
  }

  else if(rear == MAX-1 && front != 0)
  {
    rear = 0;
    queue[rear] = num;
  }
  else
  {
      rear++;
      queue[rear] = num;
  }
}

void dequeue()
{
  if(front == -1)
  {
      printf("Underflow");
      return
  }
  int element = queue[front];
  if(front == rear)
     front = rear = -1;
  if(front == MAX-1)
     front = 0;
  else
     front++;
    
   printf("The deleted element is: %d", element);
}

Stack using Linked List

struct Stack
{
    int Data;
    struct Stack *next;
}*top;

void popStack()
{
    struct Stack *temp = top;
    if(temp == NULL)
        printf("\nStack Empty")
    else{
        top = top->next;
        free(temp);
    }
}

void pushStack(int value)
{
    struct Stack *temp;
    temp=(struct Stack *)malloc(sizeof(Stack Node));
    temp->Data=value;
    if (top == NULL)
    {
         top=temp;
         top->next=NULL;
    }
    else
    {
        temp->next=top;
        top=temp;
    }
}

void display()
{
     struct Stack *var=top;
     if(var == NULL)
         printf("Stack is Empty");
     else
     { 
          while(var!=NULL) {
               printf("%d \n",var->Data);
               var=var->next;
          } 
     }

}

Stack using array

#define size 10
struct stack {
   int s[size];
   int top;
} st;
 
int stfull() {
   if (st.top >= size - 1)
      return 1;
   else
      return 0;
}
 
void push(int item) {
   st.s[st.top++] = item;
}
 
int stempty() {
   if (st.top == -1)
      return 1;
   else
      return 0;
}
 
int pop() {
   int item;
   item = st.s[st.top];
   st.top--;
   return (item);
}
 
void display() {
   int i;
   if (stempty())
      printf("\nStack Is Empty!");
   else {
      for (i = st.top; i >= 0; i--)
         printf("\n %d", st.s[i]);
   }
}

Remove duplicate from unsorted linked list

METHOD 1 (Using two loops)

struct node
{
 int data;
 struct node *next;
};
 
void removeDuplicates(struct node *start)
{
  struct node *ptr1, *ptr2, *dup;
  ptr1 = start;
 
  while(ptr1 != NULL && ptr1->next != NULL)
  {
     ptr2 = ptr1;
 
     while(ptr2->next != NULL)
     {
       if(ptr1->data == ptr2->next->data)
       {
          dup = ptr2->next;
          ptr2->next = ptr2->next->next;
          free(dup);
       }
       else 
       {
          ptr2 = ptr2->next;
       }
     }
     ptr1 = ptr1->next;
  }
}

Time Complexity: O(n^2)

METHOD 2 (Use Sorting)

In general, Merge Sort is the best suited sorting algorithm for sorting linked lists efficiently.
1) Sort the elements using Merge Sort. O(nLogn)
2) Remove duplicates in linear time using the algorithm for removing duplicates in sorted Linked List. O(n)

Please note that this method doesn’t preserve the original order of elements.

Time Complexity: O(nLogn)

METHOD 3 (Use Hashing)

void deleteDups(LinkedListNode n)
{
    Hashtable table = new Hashtable();
    LinkedListNode previous = null;
    while (n != null) {
        if (table.containsKey(n.data)) 
            previous.next = n.next;                   
        else {
            table.put(n.data, true);
            previous = n;
        } 
        n = n.next;
    }
}

Time Complexity: O(n) on average