Linked List :
#include <stdio.h>
#include <stdlib.h>
typedef struct Node Node;
#define newNode (Node*) malloc(sizeof(Node));
struct Node
{
int data;
Node *next;
};
void insert(Node **p, int x)
{
Node *t = (Node *)malloc(sizeof(Node));
t->data = x;
t->next = NULL;
if (*p == NULL)
*p = t;
else
{
Node *last = *p;
while (last->next)
last = last->next;
last->next = t;
}
}
void pushArray(Node **p, int arr[])
{
Node *t = (Node *)malloc(sizeof(Node));
Node *last = NULL;
t->data = arr[0];
t->next = NULL;
*p = t;
last = *p;
for (int i = 1; i < 5; i++)
{
Node *t = (Node *)malloc(sizeof(Node));
t->data=arr[i];
t->next =NULL;
last->next = t;
last=t;
}
}
void Display(Node *p)
{
while (p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
void insertBefore(Node**first, int n, int x)
{
Node *p = *first;
if(p->data == n)
{
Node *t = newNode;
t->data=x;
t->next=*first;
*first = t;
return;
}
while(p->next && p->next->data != n)
p=p->next;
if(p->next != NULL)
{
Node *t = newNode;
t->data=x;
t->next=p->next;
p->next=t;
}
else
printf("Node value does not exist !\n");
}
int main()
{
Node *first = NULL;
int arr[5] = {1,2,3,4,23};
pushArray(&first, arr);
Display(first);
insertBefore(&first, 1, 999);
Display(first);
}
#include <stdio.h>
#include <stdlib.h>
typedef struct Stack Stack;
#define newStack (Stack *)malloc(sizeof(Stack));
struct Stack
{
int top;
int size;
int *s;
};
void create(Stack *st)
{
st->top = -1;
st->size = 10;
st->s = (int *)malloc(st->size * (sizeof(int)));
}
void push(Stack *st, int x)
{
if (st->top > st->size - 1)
printf("Stack Overflow\n");
else
{
st->top++;
st->s[st->top] = x;
}
}
void Display(Stack st)
{
if (st.top >= 0)
{
printf("%d ", st.s[st.top]);
st.top--;
Display(st);
}
}
int mex(Stack st)
{
static int max = -1;
if (st.top >= 0)
{
if (st.top > max)
max = st.s[st.top];
st.top--;
mex(st);
// printf("%d ", max);
}
return max;
}
int main()
{
Stack st;
create(&st);
push(&st, 20);
push(&st, 30);
push(&st, 40);
push(&st, 10);
push(&st, 90);
Display(st);
printf("\n");
printf("%d \n", mex(st));
;
return 0;
}