Skip to content

Instantly share code, notes, and snippets.

@cs-fedy
Created September 15, 2020 09:11
Show Gist options
  • Save cs-fedy/ddaab628538c0328f3c824baf1f7a2af to your computer and use it in GitHub Desktop.
Save cs-fedy/ddaab628538c0328f3c824baf1f7a2af to your computer and use it in GitHub Desktop.
k stack implementation C.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define capacity 100
struct Node {
int key;
Node *next;
};
typedef struct Node Node;
Node *kStacks[capacity];
void createStacks(int);
int getPeek(int);
void push(int,int);
void pop(int);
unsigned isEmpty(int);
void createStacks(int stacksNumber) {
int index;
for (index = 0; index < stacksNumber; index++) {
*(kStacks + index) = NULL;
}
}
int getPeek(int index) {
assert(!isEmpty(index));
return((*(kStacks + index)) -> key);
}
void push(int key, int index) {
Node * nd = malloc(sizeof(Node));
nd -> key = key;
nd -> next =*(kStacks + index);
*(kStacks + index) = nd;
}
void pop(int index) {
Node *nd;
assert(!isEmpty(index));
nd = *(kStacks + index);
*(kStacks + index) = nd -> next;
free(nd);
}
unsigned pile_vide(int rang)
{
return((*(t+rang))==NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment