Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stack implementation #77

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions C/queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <stdio.h>
#include <stdlib.h>

typedef struct _NODE {
int data;
struct _NODE* next;
} node;

typedef struct _QUEUE {
struct _NODE* front;
struct _NODE* back;
int size;
}queue;


void initQueue(queue* q)
{
q->front = q->back = NULL;
q->size = 0;
}

void push(queue* q, int data)
{
node* newNode = (node *)malloc(sizeof(node));

newNode->data = data;
if (q->front == NULL)
{
newNode->next = NULL;
q->front = newNode;
}
else if (q->back == NULL)
{
q->front->next = newNode;
q->back = newNode;
}
else
{
q->back->next = newNode;
q->back = newNode;
q->back->next = NULL;
}

q->size++;
}

void pop(queue* q)
{
node* delNode = q->front;
q->front = delNode->next;
free(delNode);
q->size--;

}

void printQueue (queue q)
{
node* printData;
while (q.front != NULL)
{
printData = q.front;
printf("%d ", printData->data);
q.front = printData->next;
}
printf("\n");
}

int getTop(queue q)
{
return q.size;
}

int main(int argc, char** argv)
{
queue myQ;

initQueue(&myQ);
push(&myQ, 10);
push(&myQ, 11);
push(&myQ, 12);
push(&myQ, 13);

printQueue(myQ);
printf("%d\n", getTop(myQ));

pop(&myQ);
pop(&myQ);
printQueue(myQ);

printf("%d\n",getTop(myQ));

return 0;
}
85 changes: 85 additions & 0 deletions C/stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* The given file gives the basics of a stack. The following functions are the main functions on the stack:
1) push operation
2) pop operation.
3) get top
4) get stack size

This is a program written in C, with comments explaining the logic behind it.
*/


#include <stdio.h>

typedef struct _NODE {
int data;
struct _NODE* prev;
} node;

typedef struct _STACK{
int size;
struct _NODE* top;
} stack;

void initStack(stack* s) {
s->size = 0;
s->top = NULL;
}

void push(stack* s, int data) {
node* newNode = malloc(sizeof(node));

newNode->data = data;
newNode->prev = s->top;
s->top = newNode;
s->size++;
}

void pop(stack* s) {
node* delNode = s->top;
s->top = delNode->prev;
free(delNode);
s->size--;
}

int getSize(stack s) {
return s.size;
}

void printStack(stack s) {
node* dataPrint = NULL;
while (s.top != NULL) {
dataPrint = s.top;
printf("%d\n", dataPrint->data);
s.top = dataPrint->prev;
}
}


/*node getTop(stack s) {
return s->top;
}*/

int main (int argc, char ** argv) {
stack mySt;
initStack(&mySt);



push(&mySt, 10);
push(&mySt, 11);
push(&mySt, 12);
push(&mySt, 13);

printStack(mySt);
printf("%d\n", getSize(mySt));

pop(&mySt);
pop(&mySt);
push(&mySt, 20);
printStack(mySt);
printf("%d\n", getSize(mySt));




}