xxxxxxxxxx
// C program for array implementation of stack
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
// A structure to represent a stack
struct Stack {
int top;
unsigned capacity;
int* array;
};
// function to create a stack of given capacity. It initializes size of
// stack as 0
struct Stack* createStack(unsigned capacity)
{
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top = -1;
stack->array = (int*)malloc(stack->capacity * sizeof(int));
return stack;
}
// Stack is full when top is equal to the last index
int isFull(struct Stack* stack)
{
return stack->top == stack->capacity - 1;
}
// Stack is empty when top is equal to -1
int isEmpty(struct Stack* stack)
{
return stack->top == -1;
}
// Function to add an item to stack. It increases top by 1
void push(struct Stack* stack, int item)
{
if (isFull(stack))
return;
stack->array[++stack->top] = item;
printf("%d pushed to stack\n", item);
}
// Function to remove an item from stack. It decreases top by 1
int pop(struct Stack* stack)
{
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top--];
}
// Function to return the top from stack without removing it
int peek(struct Stack* stack)
{
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top];
}
// Driver program to test above functions
int main()
{
struct Stack* stack = createStack(100);
push(stack, 10);
push(stack, 20);
push(stack, 30);
printf("%d popped from stack\n", pop(stack));
return 0;
}
xxxxxxxxxx
// C program for linked list implementation of stack
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
// A structure to represent a stack
struct StackNode {
int data;
struct StackNode* next;
};
struct StackNode* newNode(int data)
{
struct StackNode* stackNode =
(struct StackNode*)
malloc(sizeof(struct StackNode));
stackNode->data = data;
stackNode->next = NULL;
return stackNode;
}
int isEmpty(struct StackNode* root)
{
return !root;
}
void push(struct StackNode** root, int data)
{
struct StackNode* stackNode = newNode(data);
stackNode->next = *root;
*root = stackNode;
printf("%d pushed to stack\n", data);
}
int pop(struct StackNode** root)
{
if (isEmpty(*root))
return INT_MIN;
struct StackNode* temp = *root;
*root = (*root)->next;
int popped = temp->data;
free(temp);
return popped;
}
int peek(struct StackNode* root)
{
if (isEmpty(root))
return INT_MIN;
return root->data;
}
int main()
{
struct StackNode* root = NULL;
push(&root, 10);
push(&root, 20);
push(&root, 30);
printf("%d popped from stack\n", pop(&root));
printf("Top element is %d\n", peek(root));
return 0;
}
xxxxxxxxxx
#include <stdio.h>
#include <malloc.h>
#include "ADTStack.h"
struct Stack_t {
void** array;
int top;
int maxCapacity;
void* (*cp_elm)(void*);
void (*fr_elm)(void*);
char* (*lbl_elm)(void*);
};
Stack init(int max_size, void* (*cpy_elm)(void*), void (*free_elm)(void*), char* (*label_elm)(void*)) {
Stack ps;
if ((max_size<=0)||(!cpy_elm)||(!free_elm)||(!label_elm)) {
fprintf(stderr,"Fatal error\n");
return NULL;
}
ps = (Stack)malloc(sizeof(struct Stack_t));
if (!ps) {
fprintf(stderr,"Fatal error\n");
return NULL;
}
ps->array = malloc(max_size*sizeof(void*));
if (!(ps->array)) {
free(ps);
fprintf(stderr,"Fatal error\n");
return NULL;
}
ps->top = 0;
ps->maxCapacity = max_size;
ps->cp_elm = cpy_elm;
ps->fr_elm = free_elm;
ps->lbl_elm = label_elm;
return ps;
}
int push (Stack s, void* elm) {
if((!s)||(s->top==s->maxCapacity)) {
fprintf(stderr,"Fatal error\n");
return 0;
}
s->array[s->top] = s->cp_elm(elm);
s->top++;
return 1;
}
void* pop (Stack s) {
if ((!s)||(s->top==0)) {
fprintf(stderr,"Fatal error\n");
return NULL;
}
s->top--;
return s->array[s->top];
}
void clear (Stack s) {
if (!s) {
fprintf(stderr,"Fatal error\n");
return;
}
while (s->top > 0)
s->fr_elm(s->array[--s->top]);
}
int size(Stack s) {
if (!s) {
fprintf(stderr,"Fatal error\n");
return -1;
}
return s->top;
}
void print(Stack s) {
int cur;
char* label = NULL;
if (!s) {
fprintf(stderr,"Fatal error\n");
return;
}
cur = s->top;
while (cur > 0) {
label = s->lbl_elm(s->array[--cur]);
if (!label) {
fprintf(stderr,"Fatal error\n");
return;
} else {
printf("%s\n",label);
free(label);
}
}
}
void destroy (Stack s) {
if (!s) {
fprintf(stderr,"Fatal error\n");
return;
}
clear(s);
free(s->array);
free(s);
}
xxxxxxxxxx
#ifndef _ADTSTACK_H
#define _ADTSTACK_H
typedef struct Stack_t* Stack;
/* initializes the Stack; sets the maximal capacity to max_size
returns a pointer to a record on the HEAP
*/
Stack init(int max_size, void* (*cpy_elm)(void*), void (*free_elm)(void*), char* (*label_elm)(void*));
/* inserts a copy of the element to the top of the stack.
returns a non-zero value upon success, zero on fail.
fails if init was not called prior to that, or if the stack is full. */
int push (Stack s, void* elm);
/* removes the element at the top of the stack and returns it.
fails either if init was not called prior to that, or if the stack is empty */
void* pop (Stack s);
/* removes all elements from the stack; all elements are freed.
fails if init was not called prior to that */
void clear (Stack s);
/* returns the number of elements in the stack, or a negative value when fails.
fails either if init was not called prior to that, or if the stack is empty */
int size(Stack s);
/* prints the contents of the stack.
fails if init was not called prior to that */
void print(Stack s);
/* releases all the resources of the stack */
void destroy (Stack s);
#endif
xxxxxxxxxx
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Square(int x)
{
return x*x;
}
int SquareOfSum(int a, int b){
int c= Square(a+b);
return c;
}
int main(){
int total;
int d= 5, e = 6;
total = SquareOfSum(d,e);
printf("output =%d ", total);
return 0;
}