exp 3,5,6,7: init

Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
2022-12-27 19:40:13 +05:30
parent 8fa27b556e
commit c460e95304
9 changed files with 266 additions and 3 deletions

7
2.c
View File

@@ -82,9 +82,10 @@ int main() {
delete (&head, 2);
reverse(&head);
while (head != NULL) {
printf("%d ", head->sid);
head = head->next;
student *cur = head;
while (cur != NULL) {
printf("%d ", cur->sid);
cur = cur->next;
}
// free

76
3.c Normal file
View File

@@ -0,0 +1,76 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct Employee employee;
struct Employee {
int eid;
struct Employee *next;
struct Employee *prev;
};
typedef struct Employees employees;
struct Employees {
uint len;
struct Employee *head;
struct Employee *tail;
};
employees *init() {
employees *list = malloc(sizeof(employees));
list->len = 0;
list->head = NULL;
list->tail = NULL;
return list;
}
void insert(employees *list, uint eid) {
employee *new = malloc(sizeof(employee));
new->eid = eid;
new->prev = NULL;
new->next = list->head;
if (list->head)
list->head->prev = new;
else
list->tail = new;
list->head = new;
list->len++;
}
void delete (employees *list) {
employee *prev = list->tail->prev;
free(list->tail);
if (prev) {
prev->next = NULL;
if (prev->prev == NULL)
list->head = prev;
}
list->tail = prev;
list->len--;
}
int main() {
employees *list = init();
insert(list, 4);
insert(list, 20);
insert(list, 300);
insert(list, 44);
delete (list);
employee *cur = list->head;
while (cur != NULL) {
printf("%d ", cur->eid);
cur = cur->next;
}
// free
while (list->tail != NULL)
delete (list);
free(list);
}

BIN
3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 KiB

72
5.c Normal file
View File

@@ -0,0 +1,72 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct Stacks stacks;
struct Stacks {
int *stack;
uint s1, s2;
uint l1, l2;
};
stacks *init(uint s1, uint s2) {
stacks *s = malloc(sizeof(stacks));
s->stack = malloc(sizeof(int) * (s1 + s2));
s->s1 = s1, s->s2 = s2;
s->l1 = s->l2 = 0;
return s;
}
void push(uint stack, stacks *s, int elem) {
if (stack != 1 && stack != 2)
fprintf(stderr, "Invalid stack number\n");
if (stack == 1) {
if (s->l1 == s->s1) {
fprintf(stderr, "Stack 1 Overflow");
return;
}
s->stack[s->l1++] = elem;
return;
}
if (s->l2 == s->s2) {
fprintf(stderr, "Stack 2 Overflow");
return;
}
s->stack[s->s1 + s->l2++] = elem;
}
int pop(uint stack, stacks *s) {
if (stack != 1 && stack != 2)
fprintf(stderr, "Invalid stack number\n");
if (stack == 1) {
if (!s->l1) {
fprintf(stderr, "Stack 1 Underflow");
exit(1);
}
return s->stack[--s->l1];
}
if (!s->l2) {
fprintf(stderr, "Stack 2 Underflow");
exit(1);
}
return s->stack[s->s1 + --s->l2];
}
int main() {
int s1 = 8, s2 = 4; // sizes
stacks *s = init(s1, s2);
push(1, s, 3);
push(1, s, 99);
push(2, s, 6);
push(1, s, 4);
printf("%d ", pop(1, s));
printf("%d", pop(2, s));
free(s->stack);
free(s);
}

BIN
5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

48
6.c Normal file
View File

@@ -0,0 +1,48 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct Node node;
struct Node {
int id;
struct Node *next;
};
int peek(node *stack) { return stack->id; }
void push(node **stack, int id) {
node *new = malloc(sizeof(node));
new->id = id;
new->next = *stack;
*stack = new;
}
int pop(node **stack) {
int id = (*stack)->id;
node *next = (*stack)->next;
free(*stack);
*stack = next;
return id;
}
void traverse(node *stack) {
while (stack != NULL) {
printf("%d ", stack->id);
stack = stack->next;
}
printf("\n");
}
int main() {
node *stack = NULL;
push(&stack, 4);
push(&stack, 99);
push(&stack, 23);
printf("Pop: %d\n", pop(&stack));
printf("Traverseal: ");
traverse(stack);
// free
while (stack != NULL)
pop(&stack);
}

BIN
6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

66
7.c Normal file
View File

@@ -0,0 +1,66 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct Node node;
struct Node {
int id;
struct Node *next;
};
typedef struct Queue queue;
struct Queue {
node *head;
node *tail;
};
int peek(queue *q) { return q->head->id; }
void insert(queue *q, int id) {
node *new = malloc(sizeof(node));
new->id = id;
new->next = NULL;
if (q->tail)
q->tail->next = new;
else
q->head = new;
q->tail = new;
}
void delete (queue *q) {
if (!q->head)
return;
node *next = q->head->next;
free(q->head);
q->head = next;
if (!q->head)
q->tail = NULL;
}
void display(queue *q) {
node *cur = q->head;
while (cur != NULL) {
printf("%d ", cur->id);
cur = cur->next;
}
printf("\n");
}
int main() {
queue *q = malloc(sizeof(queue));
insert(q, 56);
insert(q, 3);
insert(q, 99);
delete(q);
display(q);
// free
while (q->head != NULL)
delete(q);
free(q);
}

BIN
7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB