Created
September 15, 2020 09:11
-
-
Save cs-fedy/ddaab628538c0328f3c824baf1f7a2af to your computer and use it in GitHub Desktop.
k stack implementation C.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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