Professional Documents
Culture Documents
#include<stdio.h>
#define MAX_SIZE 5
int arr[MAX_SIZE],top=-1,i;
void push(int x)
{
if(top==(MAX_SIZE-1))
{
printf("Stack is Overflow\n");
}
else
{
arr[++top] = x;
}
}
void pop()
{
if(top<0)
{
printf("Stack is Underflow\n");
}
Else
{
arr[top--];
}
}
02) Stack Using Linked List
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *link;
};
struct Node *top = NULL;
void push(int value)
{
struct Node temp =
(struct Node)malloc(sizeof(struct Node));
temp->data = value;
temp->link = top;
top = temp;
}
void pop()
{
if(top!=NULL)
{
top = top->link;
free(del);
}
Else
{
printf("Stack Underflow\n");
}
}
03) Count Total Element in Stack Using Linked List
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *link;
};
temp->data = value;
temp->link = top;
top = temp;
}
void disp()
{
struct Node *head = top;
printf("Elements are : \n");
while(head!=NULL)
{
printf("%d ",head->data);
head = head->link;
}
printf("\n");
}
04) Search Element from Stack Using Array
#include<stdio.h>
#define MAX_SIZE 5
int arr[MAX_SIZE],top=-1,i;
void push(int x)
{
if(top==(MAX_SIZE-1))
{
printf("Stack is Overflow\n");
}
Else
{
arr[++top] = x;
}
}
void disp()
{
printf("Elements are\n");
for(i=0; i<=top; i++)
{
printf("%d\n",arr[i]);
}
}
05) Search Element from Stack Using
Linked List
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *link;
};
struct Node *top;
void push(int x)
{
if(top==NULL)
{
struct Node temp1 =
(struct Node)malloc(sizeof(struct Node));
temp1->data = x;
temp1->link = NULL;
top = temp1;
}
Else
{
struct Node temp2 =
(struct Node)malloc(sizeof(struct Node));
temp2->data = x;
temp2->link = top;
top = temp2;
}
}
void search(int x)
{
struct Node *temp = top;
int value = 0;
while(temp!=NULL)
Queue
01) Queue Using Array
#include<stdio.h>
#include<stdlib.h>
int arr[5],i,f=0,r=0;
void push(int x)
{
if(r==4)
{
printf("Queue if Overflow\n");
}
Else
{
arr[r++] = x;
}
}
void pop()
{
if(f==-1)
{
printf("Queue is Underflow\n");
}
Else
{
arr[f++];
}
}
02) Queue Using Linked List
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *link;
};
struct Node *rear;
struct Node *front;
void enQueue(int x)
{
struct Node newNode =
(struct Node)malloc(sizeof(struct Node));
newNode->data = x;
newNode->link = NULL;
if(rear==NULL)
{
rear = newNode;
front = newNode;
}
Else
{
rear->link = newNode;
rear = newNode;
}
}
void deQueue()
{
if(front==NULL)
{
printf("\nQueue is Empty\n");
}
Else
03) Count Total Element in Queue Using Linked List
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *link;
};
struct Node *rear;
struct Node *front;
void enQueue(int x)
{
struct Node newNode =
(struct Node)malloc(sizeof(struct Node));
newNode->data = x;
newNode->link = NULL;
if(rear==NULL)
{
rear = newNode;
front = newNode;
}
Else
{
rear->link = newNode;
rear = newNode;
}
}
void countNode()
{
int count = 0;
struct Node *temp = front;
while(temp->link != NULL)
04) Search Element from Queue Using Array
#include<stdio.h>
#include<stdlib.h>
int arr[5],i,f=0,r=0;
void push(int x)
{
if(r==4)
{
printf("Queue if Overflow\n");
}
Else
{
arr[r++] = x;
}
}
void search(int no)
{
int count = 0;
for(i=f; i<r; i++)
{
if(arr[i]==no)
{
count = count+1;
}
}
if(count==1)
{
printf("Found\n");
}
Else
{
printf("Not Found\n");
}
}
05) Search Element from Queue Using Linked List
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *link;
};
struct Node *rear;
struct Node *front;
void enQueue(int x)
{
struct Node newNode =
(struct Node)malloc(sizeof(struct
Node));
newNode->data = x;
newNode->link = NULL;
if(rear==NULL)
{
rear = newNode;
front = newNode;
}
Else
{
rear->link = newNode;
rear = newNode;
}
}
Stack implementation using array
-----------------------------------------------------------//HEAD
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "math.h"
//BODY
int stackArr[100005];
int top = -1; //- Stores the index of the topmost element on the stack
void push(int x)
{
if(top!=100005-1)
{
top++;
stackArr[top]=x;
}
}
int peek()
{
return stackArr[top];
}
void pop()
{
top--;
}//- Removes an element from the top of the stack
int empty()
{
if(top==-1)
return 1;
else
return 0;
}//- Returns 1 if the stack is empty and 0 otherwise
//TAIL
int main()
{
int n;
top = -1;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int t, x;
scanf("%d", &t);
if (t == 1) {
scanf("%d", &x);
push(x);
}
else if (t == 2) {
if (empty()) {
printf("Invalid\n");
}
else {
pop();
}
}
else if (t == 3){
if (empty()) {
printf("Invalid\n");
continue;
}
for (int j = top; j >= 0; j--) {
printf("%d ", stackArr[j]);
}printf("\n");
}
else {
if (empty()) {
printf("Invalid\n");
continue;
}
printf("%d\n", peek());
}
}
return 0;
}
Queue implementation
---------------------------------------------------------------------------------------- #include
<stdio.h>
#include <stdlib.h>
#include <limits.h>
#define CAPACITY 100
int queue[CAPACITY];
unsigned int size = 0;
unsigned int rear = CAPACITY - 1;
unsigned int front = 0;
int enqueue(int data);
int dequeue();
int isFull();
int isEmpty();
int getRear();
int getFront();
int main()
{
int ch, data;
while (1)
{
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Size\n");
printf("4. Get Rear\n");
printf("5. Get Front\n");
printf("0. Exit\n");
printf("Select an option: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter data to enqueue: ");
scanf("%d", &data);
if (enqueue(data))
printf("Element added to queue.");
else
printf("Queue is full.");
break;
case 2:
data = dequeue();
if (data == INT_MIN)
printf("Queue is empty.");
else
printf("Data => %d", data);
break;
case 3:
if (isEmpty())
printf("Queue is empty.");
else
printf("Queue size => %d", size);
break;
case 4:
if (isEmpty())
printf("Queue is empty.");
else
printf("Rear => %d", getRear());
break;
case 5:
if (isEmpty())
printf("Queue is empty.");
else
printf("Front => %d", getFront());
break;
case 0:
printf("Exiting from app.\n");
exit(0);
default:
printf("Invalid choice, please input number between (0-5).");
break;
}
printf("\n\n");
}
}
int enqueue(int data)
{
if (isFull())
{
return 0;
}
rear = (rear + 1) % CAPACITY;
size++;
queue[rear] = data;
return 1;
}
int dequeue()
{
int data = INT_MIN;
if (isEmpty())
{
return INT_MIN;
}
data = queue[front];
front = (front + 1) % CAPACITY;
size--;
return data;
}
int isFull()
{
return (size == CAPACITY);
}
int isEmpty()
{
return (size == 0);
}
int getFront()
{
return (isEmpty())? INT_MIN: queue[front];
}
getRear()
{
return (isEmpty())? INT_MIN: queue[rear];
}
worried
The sadness of an element in an array is defined as the minimum distance between itself and
another element whose value is the same as this element. If there is no other element with the
same value, its sadness is -1.
Given an array of size N, find and print the sadness of every element.
Input Format
First line contains integer N. Second line contains N integers, the array arr[N].
Constraints
Output Format
Output N integers separated by spaces, ith of them denoting the sadness of arr[i].
Sample Input 0
5
2 1 3 2 1
Sample Output 0
3 3 -1 3 3
#include<stdio.h>
int main()
{
int n,i,j;
scanf("%d",&n);
int a[n],b[n];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
b[i]=-1;
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i!=j && a[i]==a[j])
{
b[i]=abs(i-j);
}
}
}
for(i=0;i<n;i++)
{
printf("%d ",b[i]);
}
return 0;
}