NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include <stdio.h>
#include <math.h>

/*** THE DATA STRUCTURES ***/
#define N 10
typedef struct {
int start, end;
int data[N];
} Queue;
//Using array implement all essential functions of a queue: enqueue, dequque, count, isEmpty, isFull
void enqueue(Queue* q, int x) {
//TODO: enqeue x in a circular queue (2 lines)
q->data[q->end] = x;
q->end = (q->end+1+N)%N;
//TODO ENDS
}

int dequeue(Queue* q){
//TODO: remove and return one element from circular queue (3 lines)
int temp = q->data[q->start];
q->start = (q->start+ 1+N)%N;
return temp;
//TODO ENDS
}

int count(Queue* q) {
//TODO: Return total number of elements in the queue (1 line)
return (q->end - q->start + N)%N;
//TODO ENDS
}

int isEmpty(Queue* q) {
//TODO: Return 0 if queue is not empty (1 line)
return count(q)==0;
//TODO ENDS
}

int isFull(Queue* q) {
//TODO: Return 0 if queue is not full (1 line)
return count(q)+1 == N;
//TODO ENDS
}

typedef struct {
int cur;
Queue q[2];
} Stack;

//Using two queues implement all essential functions of a stack: isStackFull, isStackEmpty, push and pop
int isStackFull(Stack* s) {
//TODO: Check if current queue is full and return appropriate value (1 line)
return isFull(&s->q[s->cur]);
//TODO ENDS
}

int isStackEmpty(Stack* s) {
//TODO: Check if current queue is empty and return appropriate value (1 line)
return isEmpty(&s->q[s->cur]);
//TODO ENDS
}

void push(Stack* s, int x) {
//TODO: Enqueue the incoming value to the current queue (1 line)
enqueue(&s->q[s->cur],x);
//TODO ENDS
}

int pop(Stack* s) {
//TODO: Move all elements from current queue to other queue except the last one.
//Return the last element's value from the current queue but also remember to swap current queue with other queue (4 lines)
for(int i=1;i<count(&s->q[s->cur]);++i)
enqueue(&s->q[1-s->cur],dequeue(&s->q[s->cur]));
s->cur = 1-s->cur;
return dequeue(&s->q[1-s->cur]);
//TODO ENDS
}

/*** MAIN DRIVER ***/
int main () {
int integers[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 4, -1};
//ASSIGNMENTS
Queue q1 = {0,0};

for(int i = 0; integers[i]!=-1; ++i) {
if(!isFull(&q1))
enqueue(&q1, integers[i]);
else
printf("adding %d,n", integers[i]);
}

while(!isEmpty(&q1))
printf("%d ", dequeue(&q1));
printf("n");

Stack s1 = {0};

for(int i = 0; integers[i]!=-1; ++i) {
if(!isStackFull(&s1))
push(&s1, integers[i]);
else
printf("adding %d, but fulln", integers[i]);
}

while(!isStackEmpty(&s1))
printf("%d ", pop(&s1));
printf("n");
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.