SlideShare a Scribd company logo
1 of 8
Download to read offline
// C program for array implementation of stack
#include
#include
#include
// 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 ", 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 get top item from stack
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 ", pop(stack));
printf("Top item is %d ", peek(stack));
return 0;
}
// C program for array implementation of queue
#include
#include
#include
// A structure to represent a queue
struct Queue
{
int front, rear, size;
unsigned capacity;
int* array;
};
// function to create a queue of given capacity. It initializes size of
// queue as 0
struct Queue* createQueue(unsigned capacity)
{
struct Queue* queue = (struct Queue*) malloc(sizeof(struct Queue));
queue->capacity = capacity;
queue->front = queue->size = 0;
queue->rear = capacity - 1; // This is important, see the enqueue
queue->array = (int*) malloc(queue->capacity * sizeof(int));
return queue;
}
// Queue is full when size becomes equal to the capacity
int isFull(struct Queue* queue)
{ return (queue->size == queue->capacity); }
// Queue is empty when size is 0
int isEmpty(struct Queue* queue)
{ return (queue->size == 0); }
// Function to add an item to the queue. It changes rear and size
void enqueue(struct Queue* queue, int item)
{
if (isFull(queue))
return;
queue->rear = (queue->rear + 1)%queue->capacity;
queue->array[queue->rear] = item;
queue->size = queue->size + 1;
printf("%d enqueued to queue ", item);
}
// Function to remove an item from queue. It changes front and size
int dequeue(struct Queue* queue)
{
if (isEmpty(queue))
return INT_MIN;
int item = queue->array[queue->front];
queue->front = (queue->front + 1)%queue->capacity;
queue->size = queue->size - 1;
return item;
}
// Function to get front of queue
int front(struct Queue* queue)
{
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->front];
}
// Function to get rear of queue
int rear(struct Queue* queue)
{
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->rear];
}
// Driver program to test above functions./
int main()
{
struct Queue* queue = createQueue(1000);
enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
enqueue(queue, 40);
printf("%d dequeued from queue ", dequeue(queue));
printf("Front item is %d ", front(queue));
printf("Rear item is %d ", rear(queue));
return 0;
}
Solution
// C program for array implementation of stack
#include
#include
#include
// 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 ", 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 get top item from stack
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 ", pop(stack));
printf("Top item is %d ", peek(stack));
return 0;
}
// C program for array implementation of queue
#include
#include
#include
// A structure to represent a queue
struct Queue
{
int front, rear, size;
unsigned capacity;
int* array;
};
// function to create a queue of given capacity. It initializes size of
// queue as 0
struct Queue* createQueue(unsigned capacity)
{
struct Queue* queue = (struct Queue*) malloc(sizeof(struct Queue));
queue->capacity = capacity;
queue->front = queue->size = 0;
queue->rear = capacity - 1; // This is important, see the enqueue
queue->array = (int*) malloc(queue->capacity * sizeof(int));
return queue;
}
// Queue is full when size becomes equal to the capacity
int isFull(struct Queue* queue)
{ return (queue->size == queue->capacity); }
// Queue is empty when size is 0
int isEmpty(struct Queue* queue)
{ return (queue->size == 0); }
// Function to add an item to the queue. It changes rear and size
void enqueue(struct Queue* queue, int item)
{
if (isFull(queue))
return;
queue->rear = (queue->rear + 1)%queue->capacity;
queue->array[queue->rear] = item;
queue->size = queue->size + 1;
printf("%d enqueued to queue ", item);
}
// Function to remove an item from queue. It changes front and size
int dequeue(struct Queue* queue)
{
if (isEmpty(queue))
return INT_MIN;
int item = queue->array[queue->front];
queue->front = (queue->front + 1)%queue->capacity;
queue->size = queue->size - 1;
return item;
}
// Function to get front of queue
int front(struct Queue* queue)
{
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->front];
}
// Function to get rear of queue
int rear(struct Queue* queue)
{
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->rear];
}
// Driver program to test above functions./
int main()
{
struct Queue* queue = createQueue(1000);
enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
enqueue(queue, 40);
printf("%d dequeued from queue ", dequeue(queue));
printf("Front item is %d ", front(queue));
printf("Rear item is %d ", rear(queue));
return 0;
}

More Related Content

Similar to C program for array implementation of stack#include stdio.h.pdf

please tell me what lines do i alter to make this stack a queue. tel.pdf
please tell me what lines do i alter to make this stack a queue. tel.pdfplease tell me what lines do i alter to make this stack a queue. tel.pdf
please tell me what lines do i alter to make this stack a queue. tel.pdfagarshailenterprises
 
Stacks and queue
Stacks and queueStacks and queue
Stacks and queueAmit Vats
 
Were writing code for a project that dynamically allocates an arra.pdf
Were writing code for a project that dynamically allocates an arra.pdfWere writing code for a project that dynamically allocates an arra.pdf
Were writing code for a project that dynamically allocates an arra.pdffsenterprises
 
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docxNew folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docxcurwenmichaela
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applicationsAhsan Mansiv
 
1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdfafgt2012
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arraystameemyousaf
 
template-typename T- class Array { public- ---------------------------.pdf
template-typename T- class Array { public- ---------------------------.pdftemplate-typename T- class Array { public- ---------------------------.pdf
template-typename T- class Array { public- ---------------------------.pdfashokadyes
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfHelp please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfarorastores
 
Stack Implementation
Stack ImplementationStack Implementation
Stack ImplementationZidny Nafan
 
Modifications highlighted in bold lettersDropOutStack.javaim.pdf
Modifications highlighted in bold lettersDropOutStack.javaim.pdfModifications highlighted in bold lettersDropOutStack.javaim.pdf
Modifications highlighted in bold lettersDropOutStack.javaim.pdfLalkamal2
 

Similar to C program for array implementation of stack#include stdio.h.pdf (20)

Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
please tell me what lines do i alter to make this stack a queue. tel.pdf
please tell me what lines do i alter to make this stack a queue. tel.pdfplease tell me what lines do i alter to make this stack a queue. tel.pdf
please tell me what lines do i alter to make this stack a queue. tel.pdf
 
Stacks and queue
Stacks and queueStacks and queue
Stacks and queue
 
Were writing code for a project that dynamically allocates an arra.pdf
Were writing code for a project that dynamically allocates an arra.pdfWere writing code for a project that dynamically allocates an arra.pdf
Were writing code for a project that dynamically allocates an arra.pdf
 
104332 sri vidhya eng notes
104332 sri vidhya eng notes104332 sri vidhya eng notes
104332 sri vidhya eng notes
 
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docxNew folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
New folderjsjfArrayStack.classpackage jsjf;publicsynchronize.docx
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applications
 
1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 
template-typename T- class Array { public- ---------------------------.pdf
template-typename T- class Array { public- ---------------------------.pdftemplate-typename T- class Array { public- ---------------------------.pdf
template-typename T- class Array { public- ---------------------------.pdf
 
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdfHelp please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
Help please, I have attached LinkedList.cpp and LinkedList.hPlease.pdf
 
Stack Implementation
Stack ImplementationStack Implementation
Stack Implementation
 
Modifications highlighted in bold lettersDropOutStack.javaim.pdf
Modifications highlighted in bold lettersDropOutStack.javaim.pdfModifications highlighted in bold lettersDropOutStack.javaim.pdf
Modifications highlighted in bold lettersDropOutStack.javaim.pdf
 
Algo>Stacks
Algo>StacksAlgo>Stacks
Algo>Stacks
 

More from mohammadirfan136964

2. Ligamentum arteriosumThe aortic arch loops over the left pulmo.pdf
2. Ligamentum arteriosumThe aortic arch loops over the left pulmo.pdf2. Ligamentum arteriosumThe aortic arch loops over the left pulmo.pdf
2. Ligamentum arteriosumThe aortic arch loops over the left pulmo.pdfmohammadirfan136964
 
moles should be equal to complete consumption so.pdf
                     moles should be equal to complete consumption  so.pdf                     moles should be equal to complete consumption  so.pdf
moles should be equal to complete consumption so.pdfmohammadirfan136964
 
TrueOne of the fundamental rules which control the PLSS system is .pdf
TrueOne of the fundamental rules which control the PLSS system is .pdfTrueOne of the fundamental rules which control the PLSS system is .pdf
TrueOne of the fundamental rules which control the PLSS system is .pdfmohammadirfan136964
 
The stronger the attractions betweenparticles (mo.pdf
                     The stronger the attractions betweenparticles (mo.pdf                     The stronger the attractions betweenparticles (mo.pdf
The stronger the attractions betweenparticles (mo.pdfmohammadirfan136964
 
a. It gives benzalacetone b. acetone will underg.pdf
                     a. It gives benzalacetone  b. acetone will underg.pdf                     a. It gives benzalacetone  b. acetone will underg.pdf
a. It gives benzalacetone b. acetone will underg.pdfmohammadirfan136964
 
what is the mean, median, mode and the midrange for 191, 167,257,1.pdf
what is the mean, median, mode and the midrange for  191, 167,257,1.pdfwhat is the mean, median, mode and the midrange for  191, 167,257,1.pdf
what is the mean, median, mode and the midrange for 191, 167,257,1.pdfmohammadirfan136964
 
Disodium malonate is an ionic sodium salt that is soluble in water..pdf
Disodium malonate is an ionic sodium salt that is soluble in water..pdfDisodium malonate is an ionic sodium salt that is soluble in water..pdf
Disodium malonate is an ionic sodium salt that is soluble in water..pdfmohammadirfan136964
 
They showed that GFP protein could be expressed in the organisms wit.pdf
They showed that GFP protein could be expressed in the organisms wit.pdfThey showed that GFP protein could be expressed in the organisms wit.pdf
They showed that GFP protein could be expressed in the organisms wit.pdfmohammadirfan136964
 
Thepossible mechanism by which a 75kD nuclear protein is imported in.pdf
Thepossible mechanism by which a 75kD nuclear protein is imported in.pdfThepossible mechanism by which a 75kD nuclear protein is imported in.pdf
Thepossible mechanism by which a 75kD nuclear protein is imported in.pdfmohammadirfan136964
 
The neurons are nervous cells which play the central role in generat.pdf
The neurons are nervous cells which play the central role in generat.pdfThe neurons are nervous cells which play the central role in generat.pdf
The neurons are nervous cells which play the central role in generat.pdfmohammadirfan136964
 
The parts of the respiratory tract which have nonkeratinized stratif.pdf
The parts of the respiratory tract which have nonkeratinized stratif.pdfThe parts of the respiratory tract which have nonkeratinized stratif.pdf
The parts of the respiratory tract which have nonkeratinized stratif.pdfmohammadirfan136964
 
The impact of Brexit would be visible on the financial instituations.pdf
The impact of Brexit would be visible on the financial instituations.pdfThe impact of Brexit would be visible on the financial instituations.pdf
The impact of Brexit would be visible on the financial instituations.pdfmohammadirfan136964
 
The alkaline earth metals burn in oxygen to form the monoxide, MO wh.pdf
The alkaline earth metals burn in oxygen to form the monoxide, MO wh.pdfThe alkaline earth metals burn in oxygen to form the monoxide, MO wh.pdf
The alkaline earth metals burn in oxygen to form the monoxide, MO wh.pdfmohammadirfan136964
 
Squid molluscCrab ArthropodsCrayfish ArthropodsPolychaete .pdf
Squid molluscCrab ArthropodsCrayfish ArthropodsPolychaete .pdfSquid molluscCrab ArthropodsCrayfish ArthropodsPolychaete .pdf
Squid molluscCrab ArthropodsCrayfish ArthropodsPolychaete .pdfmohammadirfan136964
 
The code you written is little bit confusing and lengthy. Here is th.pdf
The code you written is little bit confusing and lengthy. Here is th.pdfThe code you written is little bit confusing and lengthy. Here is th.pdf
The code you written is little bit confusing and lengthy. Here is th.pdfmohammadirfan136964
 
Ques-1Normala flora and microbiota of nose and mouth are non-infe.pdf
Ques-1Normala flora and microbiota of nose and mouth are non-infe.pdfQues-1Normala flora and microbiota of nose and mouth are non-infe.pdf
Ques-1Normala flora and microbiota of nose and mouth are non-infe.pdfmohammadirfan136964
 
Solution.According to IASB framework INCOME is the one of element .pdf
Solution.According to IASB framework INCOME is the one of element .pdfSolution.According to IASB framework INCOME is the one of element .pdf
Solution.According to IASB framework INCOME is the one of element .pdfmohammadirfan136964
 
7. Answer is A.Cytochromes are protien containing iron or heme gro.pdf
7. Answer is A.Cytochromes are protien containing iron or heme gro.pdf7. Answer is A.Cytochromes are protien containing iron or heme gro.pdf
7. Answer is A.Cytochromes are protien containing iron or heme gro.pdfmohammadirfan136964
 
A and C. RGB analog component video uses no compression, imposes no .pdf
A and C. RGB analog component video uses no compression, imposes no .pdfA and C. RGB analog component video uses no compression, imposes no .pdf
A and C. RGB analog component video uses no compression, imposes no .pdfmohammadirfan136964
 
Public key authentication is the most secure colution and utilizes a.pdf
Public key authentication is the most secure colution and utilizes a.pdfPublic key authentication is the most secure colution and utilizes a.pdf
Public key authentication is the most secure colution and utilizes a.pdfmohammadirfan136964
 

More from mohammadirfan136964 (20)

2. Ligamentum arteriosumThe aortic arch loops over the left pulmo.pdf
2. Ligamentum arteriosumThe aortic arch loops over the left pulmo.pdf2. Ligamentum arteriosumThe aortic arch loops over the left pulmo.pdf
2. Ligamentum arteriosumThe aortic arch loops over the left pulmo.pdf
 
moles should be equal to complete consumption so.pdf
                     moles should be equal to complete consumption  so.pdf                     moles should be equal to complete consumption  so.pdf
moles should be equal to complete consumption so.pdf
 
TrueOne of the fundamental rules which control the PLSS system is .pdf
TrueOne of the fundamental rules which control the PLSS system is .pdfTrueOne of the fundamental rules which control the PLSS system is .pdf
TrueOne of the fundamental rules which control the PLSS system is .pdf
 
The stronger the attractions betweenparticles (mo.pdf
                     The stronger the attractions betweenparticles (mo.pdf                     The stronger the attractions betweenparticles (mo.pdf
The stronger the attractions betweenparticles (mo.pdf
 
a. It gives benzalacetone b. acetone will underg.pdf
                     a. It gives benzalacetone  b. acetone will underg.pdf                     a. It gives benzalacetone  b. acetone will underg.pdf
a. It gives benzalacetone b. acetone will underg.pdf
 
what is the mean, median, mode and the midrange for 191, 167,257,1.pdf
what is the mean, median, mode and the midrange for  191, 167,257,1.pdfwhat is the mean, median, mode and the midrange for  191, 167,257,1.pdf
what is the mean, median, mode and the midrange for 191, 167,257,1.pdf
 
Disodium malonate is an ionic sodium salt that is soluble in water..pdf
Disodium malonate is an ionic sodium salt that is soluble in water..pdfDisodium malonate is an ionic sodium salt that is soluble in water..pdf
Disodium malonate is an ionic sodium salt that is soluble in water..pdf
 
They showed that GFP protein could be expressed in the organisms wit.pdf
They showed that GFP protein could be expressed in the organisms wit.pdfThey showed that GFP protein could be expressed in the organisms wit.pdf
They showed that GFP protein could be expressed in the organisms wit.pdf
 
Thepossible mechanism by which a 75kD nuclear protein is imported in.pdf
Thepossible mechanism by which a 75kD nuclear protein is imported in.pdfThepossible mechanism by which a 75kD nuclear protein is imported in.pdf
Thepossible mechanism by which a 75kD nuclear protein is imported in.pdf
 
The neurons are nervous cells which play the central role in generat.pdf
The neurons are nervous cells which play the central role in generat.pdfThe neurons are nervous cells which play the central role in generat.pdf
The neurons are nervous cells which play the central role in generat.pdf
 
The parts of the respiratory tract which have nonkeratinized stratif.pdf
The parts of the respiratory tract which have nonkeratinized stratif.pdfThe parts of the respiratory tract which have nonkeratinized stratif.pdf
The parts of the respiratory tract which have nonkeratinized stratif.pdf
 
The impact of Brexit would be visible on the financial instituations.pdf
The impact of Brexit would be visible on the financial instituations.pdfThe impact of Brexit would be visible on the financial instituations.pdf
The impact of Brexit would be visible on the financial instituations.pdf
 
The alkaline earth metals burn in oxygen to form the monoxide, MO wh.pdf
The alkaline earth metals burn in oxygen to form the monoxide, MO wh.pdfThe alkaline earth metals burn in oxygen to form the monoxide, MO wh.pdf
The alkaline earth metals burn in oxygen to form the monoxide, MO wh.pdf
 
Squid molluscCrab ArthropodsCrayfish ArthropodsPolychaete .pdf
Squid molluscCrab ArthropodsCrayfish ArthropodsPolychaete .pdfSquid molluscCrab ArthropodsCrayfish ArthropodsPolychaete .pdf
Squid molluscCrab ArthropodsCrayfish ArthropodsPolychaete .pdf
 
The code you written is little bit confusing and lengthy. Here is th.pdf
The code you written is little bit confusing and lengthy. Here is th.pdfThe code you written is little bit confusing and lengthy. Here is th.pdf
The code you written is little bit confusing and lengthy. Here is th.pdf
 
Ques-1Normala flora and microbiota of nose and mouth are non-infe.pdf
Ques-1Normala flora and microbiota of nose and mouth are non-infe.pdfQues-1Normala flora and microbiota of nose and mouth are non-infe.pdf
Ques-1Normala flora and microbiota of nose and mouth are non-infe.pdf
 
Solution.According to IASB framework INCOME is the one of element .pdf
Solution.According to IASB framework INCOME is the one of element .pdfSolution.According to IASB framework INCOME is the one of element .pdf
Solution.According to IASB framework INCOME is the one of element .pdf
 
7. Answer is A.Cytochromes are protien containing iron or heme gro.pdf
7. Answer is A.Cytochromes are protien containing iron or heme gro.pdf7. Answer is A.Cytochromes are protien containing iron or heme gro.pdf
7. Answer is A.Cytochromes are protien containing iron or heme gro.pdf
 
A and C. RGB analog component video uses no compression, imposes no .pdf
A and C. RGB analog component video uses no compression, imposes no .pdfA and C. RGB analog component video uses no compression, imposes no .pdf
A and C. RGB analog component video uses no compression, imposes no .pdf
 
Public key authentication is the most secure colution and utilizes a.pdf
Public key authentication is the most secure colution and utilizes a.pdfPublic key authentication is the most secure colution and utilizes a.pdf
Public key authentication is the most secure colution and utilizes a.pdf
 

Recently uploaded

What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxCeline George
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use CasesTechSoup
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...EduSkills OECD
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaEADTU
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....Ritu480198
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of PlayPooky Knightsmith
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonhttgc7rh9c
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Ernest Hemingway's For Whom the Bell Tolls
Ernest Hemingway's For Whom the Bell TollsErnest Hemingway's For Whom the Bell Tolls
Ernest Hemingway's For Whom the Bell TollsPallavi Parmar
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 

Recently uploaded (20)

What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Ernest Hemingway's For Whom the Bell Tolls
Ernest Hemingway's For Whom the Bell TollsErnest Hemingway's For Whom the Bell Tolls
Ernest Hemingway's For Whom the Bell Tolls
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 

C program for array implementation of stack#include stdio.h.pdf

  • 1. // C program for array implementation of stack #include #include #include // 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 ", item); }
  • 2. // 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 get top item from stack 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 ", pop(stack)); printf("Top item is %d ", peek(stack)); return 0; } // C program for array implementation of queue #include #include #include // A structure to represent a queue struct Queue { int front, rear, size; unsigned capacity; int* array; };
  • 3. // function to create a queue of given capacity. It initializes size of // queue as 0 struct Queue* createQueue(unsigned capacity) { struct Queue* queue = (struct Queue*) malloc(sizeof(struct Queue)); queue->capacity = capacity; queue->front = queue->size = 0; queue->rear = capacity - 1; // This is important, see the enqueue queue->array = (int*) malloc(queue->capacity * sizeof(int)); return queue; } // Queue is full when size becomes equal to the capacity int isFull(struct Queue* queue) { return (queue->size == queue->capacity); } // Queue is empty when size is 0 int isEmpty(struct Queue* queue) { return (queue->size == 0); } // Function to add an item to the queue. It changes rear and size void enqueue(struct Queue* queue, int item) { if (isFull(queue)) return; queue->rear = (queue->rear + 1)%queue->capacity; queue->array[queue->rear] = item; queue->size = queue->size + 1; printf("%d enqueued to queue ", item); } // Function to remove an item from queue. It changes front and size int dequeue(struct Queue* queue) { if (isEmpty(queue)) return INT_MIN; int item = queue->array[queue->front]; queue->front = (queue->front + 1)%queue->capacity; queue->size = queue->size - 1; return item;
  • 4. } // Function to get front of queue int front(struct Queue* queue) { if (isEmpty(queue)) return INT_MIN; return queue->array[queue->front]; } // Function to get rear of queue int rear(struct Queue* queue) { if (isEmpty(queue)) return INT_MIN; return queue->array[queue->rear]; } // Driver program to test above functions./ int main() { struct Queue* queue = createQueue(1000); enqueue(queue, 10); enqueue(queue, 20); enqueue(queue, 30); enqueue(queue, 40); printf("%d dequeued from queue ", dequeue(queue)); printf("Front item is %d ", front(queue)); printf("Rear item is %d ", rear(queue)); return 0; } Solution // C program for array implementation of stack #include #include #include // A structure to represent a stack
  • 5. 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 ", 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--];
  • 6. } // Function to get top item from stack 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 ", pop(stack)); printf("Top item is %d ", peek(stack)); return 0; } // C program for array implementation of queue #include #include #include // A structure to represent a queue struct Queue { int front, rear, size; unsigned capacity; int* array; }; // function to create a queue of given capacity. It initializes size of // queue as 0 struct Queue* createQueue(unsigned capacity) { struct Queue* queue = (struct Queue*) malloc(sizeof(struct Queue)); queue->capacity = capacity;
  • 7. queue->front = queue->size = 0; queue->rear = capacity - 1; // This is important, see the enqueue queue->array = (int*) malloc(queue->capacity * sizeof(int)); return queue; } // Queue is full when size becomes equal to the capacity int isFull(struct Queue* queue) { return (queue->size == queue->capacity); } // Queue is empty when size is 0 int isEmpty(struct Queue* queue) { return (queue->size == 0); } // Function to add an item to the queue. It changes rear and size void enqueue(struct Queue* queue, int item) { if (isFull(queue)) return; queue->rear = (queue->rear + 1)%queue->capacity; queue->array[queue->rear] = item; queue->size = queue->size + 1; printf("%d enqueued to queue ", item); } // Function to remove an item from queue. It changes front and size int dequeue(struct Queue* queue) { if (isEmpty(queue)) return INT_MIN; int item = queue->array[queue->front]; queue->front = (queue->front + 1)%queue->capacity; queue->size = queue->size - 1; return item; } // Function to get front of queue int front(struct Queue* queue) { if (isEmpty(queue)) return INT_MIN;
  • 8. return queue->array[queue->front]; } // Function to get rear of queue int rear(struct Queue* queue) { if (isEmpty(queue)) return INT_MIN; return queue->array[queue->rear]; } // Driver program to test above functions./ int main() { struct Queue* queue = createQueue(1000); enqueue(queue, 10); enqueue(queue, 20); enqueue(queue, 30); enqueue(queue, 40); printf("%d dequeued from queue ", dequeue(queue)); printf("Front item is %d ", front(queue)); printf("Rear item is %d ", rear(queue)); return 0; }