C :: Write Algorithm Using Stack
Jul 22, 2014write an algorithm using stack to determine if an input of string is in the form xCy where y is the reverse of x.x and y are strings of A and B. eg : AABACABAA
View 8 Replieswrite an algorithm using stack to determine if an input of string is in the form xCy where y is the reverse of x.x and y are strings of A and B. eg : AABACABAA
View 8 Repliesi try to write a postfix calculator program using stack in C++ the in put must be the infix expression but can dont know how to write a infix expression in put.
this is my code :
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <conio.h>
using namespace std;
}
[code]....
How to write Algorithm / Design
View 7 Replies View RelatedWrite program that do the algorithm of serial divider( You can divide to number in binary form and result also in binary.
View 2 Replies View RelatedCan you declare a stack globally?
Code:
stack< int > stk;
I'm getting a stack overflow error because for large numbers, this code I'm working on allocates too much on the stack.
Is there a way of tracking stack allocations specifically?
Will multithreading solve my problem if each thread is doing the static allocations?
Would I really have to use malloc or new every time I wanted to use memory just to make my code scale to huge numbers?
I usually check if a bit is set using:
Code: bit = (number >> n) & 1; where `n` is the bit I want to check...
But I came across a stackoverflow answer saying:
bit = number & (1 << x); will not put the value of bit x into bit unless bit has type _Bool (<stdbool.h>).
Otherwise, bit = !!(number & (1 << x)); will..
So why is this? why the double !?
I am getting corrupted stack variable for this piece of bolded code. (starCounter is corrupted)...
Code:
int i;
int j;
int s;
double starCounter[18]= {0};
double lowestfreq = 0;
double frequencyChecker [18] = {0};
[Code] .....
I am curious as to what is happening to my pointers and everything once I call malloc to create space for 5 integers to try to make a stack.
Code:
#include <stdio.h>
#include <stdlib.h>
void add(int * TOP, int * stack);
int main() {
int *stack = NULL;
int *TOP = NULL;
stack = (int *)malloc (5 * sizeof(int));
[Code] ....
I am guessing that when I initialize stack to malloc, stack now stores the starting address of where the space is taken out, and also assigns TOP that address too. I get the choice from the user (b) to get the instruction to try to push on the stack. I was told that the if statement in the function checks if the stack has passed the bounds of 5 elements. If not, it assigns the scanned variable and puts it into what TOP is pointing to and increments TOP to the next address. It is not working and am wanting to see where my logic is wrong.
Code:
#include <stdio.h>#include <unistd.h>
#include <stdlib.h>
#define STACKSIZE 100
struct stackk
{
int top;
int items[STACKSIZE];
};
typedef struct stackk *s;
[Code]...
Validation opening/closing brackets.
I had the entire validation working until my professor said that my Pop function had to be a "void" not a "Char". Which destroyed my previous validation and now how to validate this.
I will only be posting the validating function.
All the characters are being thrown in a Stack. So I am using Pop/Top/Push/isEmpty/isFull. That will be shown in my validating code.
Correct Validations:
1. {}()[]
2. [()]
3. {([])}
Incorrect Validations:
1.[)
2.[(]]
3.{])
My main issue is that it validates "[)" correct.
I am pretty positive I must have over complicated my validation.
I keep getting the error "Stack around the variable 'odd' was corrupted." how can I fix this here is the program:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
ifstream infile; ofstream outfile;
[Code] ....
I am trying to implement a stack class which has struct data type as its private member. I am getiing the following error ,
bash-3.2$ g++ stack_str_arr.cpp
stack_str_arr.cpp: In member function ‘void stack::push(int)’:
stack_str_arr.cpp:39: error: ‘top’ was not declared in this scope
stack_str_arr.cpp: At global scope:
stack_str_arr.cpp:43: error: no ‘void stack::display()’ member function declared in class ‘stack’
Code:
#include<iostream>
using namespace std;
#define MAX 5
class stack {
public :
stack();
[Code] ....
Here is my error message: Warning1warning C4717: 'getx' : recursive on all control paths, function will cause runtime stack overflow
and here is my code I will Bold the part that is giving me problems!
class point
{
public:
point();
[Code]....
In c89/90 is there a way to retrieve a pointer from stack or whatever that would behave as this would once cast to the correct type? This is more of a curiosity than a requirement.
View 6 Replies View RelatedI am unable to make any logical algorithm to access middle of stack. I am using stack implemented with linkedlists, I have following functions available for stack like push,pop,top.
View 6 Replies View RelatedSuppose if i code like this,
while( true ) {
int x;
// do some thing;
// break on some condition match;
}
whether memory is allocated for ints x one time or each time it hits int x.
I'm following a course with coursera (algorithm 1) and my program for the assignment crashes inexorably (many are experimenting the same thing there, the assignment asks for a recursive program for solving a big graph of 500000 nodes). I'm using a version of g++ from MinGW with the editor Geany and windows7. The current setup is:
g++ -std=c++11 -Wall -c "%f"
g++ -std=c++11 -Wall -o "%e" "%f"
How can I set the stack 'unlimited'?
I want to convert the following program into a non-recursive code using a stack and a loop:
void write(int n) {
if (n>0) {
write(n-1);
cout << n << " ";
write(n-1);
}
}
And here is the code I am using but that currently does not work, and I do not know why:
stack<int> S;
S.push(n);
while (not S.empty()) {
int k = S.top();
[Code] .....
How to simulate that recursion. I thought it was enough to push into the stack the equivalent recursive call.
I have been trying to solve a problem when this error came to me :
" stack around the variable 'X' is corrupted "
the program is working correctly and it gave me the wanted result but why does this error appear ?
this is my code :
class interval {
public:
int a;// lower bound
int b;// upper bound
interval(int aValue, int bValue) {set(aValue,bValue);};
[code]....
I do some research about getting the coordinates from the list items but without success. I would like to make that when i press down my mouse button that little blocks fall "out" my mouse and stack on top of eachother.
that first works. When i press the mouse button i draw squares and the fall to the bottom of the screen, but the stack part not.
here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
[Code]....
Given a stack with an equal amount of even and odd numbers: 4, 22, 15, 3. The output should be in any order: [even], [odd], [even], [odd].
int main() {
Stack<int, 4> stk; stk.ClearStack(); int x;
for (int i = 0; i < 4; i++) {
cin >> x;
stk.PushStack(x);
[Code] ....
Output is:
Wed Feb 19 20:09:10 2014
4 7 5 22 //input
22 5 4 //output
Press any key to continue . . .
I know the logic fails if the first number is not even, but I cant seem to figure out another way to do it.
I am trying to use pointers in a stack program. But i am failing. my push function takes in the array,item im pushing, the reference to the top, and the size of the array.
void push(char stack[], char item, int *top, int max_size) {
if(&top==(max_size-1)) {
printf("Stack is Full");
return;
} else {
*top=+1;
stack[*top]=item;
} return;
}
And in the main function i pass a randomly pick char value, print it first , and pass it to the array using the push function
#define STACK_SIZE 10
#define STACK_EMPTY -1
int main(){
char s[STACK_SIZE];
int s_top = STACK_EMPTY; // The index of the top of the stack
int *top= s_top;
int max_size=STACK_SIZE;
[Code] .....
I know I am not referencing and dereferencing correctly and probably other stuff also. but i get these 2 errors:
stack.c: In function "push":
stack.c:23:9: warning: comparison between pointer and integer [enabled by default]
if(&top==(max_size-1))
[Code] ....
I am trying to make a parenthesis counter using stack in C. But my code is always returning that my expression is invalid.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#define SIZE 100
#define True 1
#define False 0
struct stack {
int top;
char items[SIZE];
[Code] ....
I'm having trouble using a stack's functions when passing it by reference.
I'm currently messing around with passing a pointer to the stack instead, but to no avail.
The problem is in my placeInto function. I will label, in my code, where the program stops.
The program is an RPN Calculator, designed to receive keyboard input of an infix mathematical string, convert the infix string into a postfix mathematical string, and then solve the string. The program then outputs the postfix string and the solution.
The program loops through until closed via force-close, as was assigned.
*/
#include <iostream>
#include <string>
#include <stack>
using namespace std;
///////////////////////
// function prototypes //
///////////////////////
// u = unfinished
/* 1*/void welcome(); // welcome screen
[code]....
The following program is designed to demonstrate class templates. The program will evaluate postfix expressions input to the console by pushing them in a stack, and then popping them and evaluating them as such, (ex. entering 3 4 + would equal 3+4 = 7).
The code is below. We are not to modify it, but to fill in the blanks, the places filled in indicated with two asterisks for a line, and one on each side for a part of a line. If I didn't know what to enter (if anything), I put three ?s. If you want to copy and compile for yourself, look for all the *s and ?s.
1) I'm turning up all sorts of errors in the main program file (prog5.cpp) having to do with stacktype.cpp. It has been removed from the program, as it is included at the end of stackType.h. Most of them are "cannot convert 'this' pointer from StackType to StackType<stack> &'. How do I fix that?
2) The program supposedly lacks a default constructor, and it keeps turning up that 's' is an array of unknown size (do I call StackType or stack or what?).
stackType.h Code: #pragma once// Catherine Stringfellow and Trey Brumley
// A Stack is a data type, which stores values in an order where values are added to the top of the stack when pushed,
// and when popped, remove and return the value from the top of the stack.
// Class specification for Stack ADT in file StackType.h
using namespace std;
static const int MAXITEMS = 50;
[code].....