Skip to content

Latest commit

 

History

History
106 lines (41 loc) · 1.3 KB

Dynamic Data Structures in C.md

File metadata and controls

106 lines (41 loc) · 1.3 KB

Dynamic Data Structures

s

In addition to arrays, C allows you to create dynamic data structures such as linked lists, stacks, queues, and trees. Implementing these data structures enhances your ability to manage complex data efficiently.

#include <stdio.h>

#include <stdlib.h>

struct Node {

int data;

struct Node* next;

};

struct Stack {

struct Node* top;

};

void push(struct Stack* stack, int data) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = data;

newNode->next = stack->top;

stack->top = newNode;

}

int pop(struct Stack* stack) {

if (stack->top == NULL) {

    printf("Stack underflow\n");

    return -1;

}



struct Node* temp = stack->top;

int data = temp->data;

stack->top = stack->top->next;

free(temp);



return data;

}

int main() {

struct Stack stack;

stack.top = NULL;



push(&stack, 3);

push(&stack, 2);

push(&stack, 1);



printf("Popped element: %d\n", pop(&stack));

printf("Popped element: %d\n", pop(&stack));

printf("Popped element: %d\n", pop(&stack));

printf("Popped element: %d\n", pop(&stack));



return 0;

}