| :: [intv] string manipulation problem using stack., :: | ||||
| HOME |
|
[Date Prev][Date Next][Date Index] [intv] string manipulation problem using stack.,------------------------------------------ CC=g++ COPTS = -g DEPENDS= stack.o CFLAGS= $(COPTS) $(DEFINES) all: stack main stack: stack.cpp stack.h $(CC) $(CFLAGS) -c $@.cpp $(LIBS) main: stack $(CC) $(CFLAGS) -o $@ $@.cpp $(DEPENDS) clean: rm *.o *~ main ------------------------------------------ #ifndef STACK_H #define STACK_H #include <iostream> const int MAXSTACKSIZE = 40; class stack{
private:
char *stacklist[MAXSTACKSIZE];
int top;
public:
stack(void);
void push(char *Item);
char *pop(void);
char *peek(void);
int isEmpty();
int isFull();
};#endif ------------------------------------------ #include <iostream> #include "stack.h" using namespace std; stack::stack(){
top = -1;
}void stack::push(char *Item){
if (!isFull()){
top++;
stacklist[top] = Item;
}else{
cout << "Stack Overflow" << endl;
}
}char *stack::pop(void){
if (!isEmpty()){
char *tem;
tem = stacklist[top];
top--;
return tem;
}else{
cout << "Stack Underflow" << endl;
}
}char *stack::peek(void){
if (!isEmpty()){
return stacklist[top];
}else{
cout << "Attempt to peek into an empty stack" << endl;
}
}int stack::isEmpty(void){
if (top == -1) return 1; else return 0;
}int stack::isFull(void){
if (top == (MAXSTACKSIZE-1)) return 1; else return 0;
}
------------------------------------------//Problem: Different ways in which arguments can be passed to a function. //Save as: #include <iostream> #include "stack.h" using namespace std;
x = (char *)malloc(sizeof(char)*50); strcpy(x, "/home/shashank/../WWW/../index.html"); cout << x << endl; char *y; y = (char *) malloc(sizeof(char)*10); char *z; z = (char *) malloc(sizeof(char)*100); y = strtok(x, "/");
while(y != NULL){
if (strcmp(y, "..") == 0){
if (!S.isEmpty()) S.pop();
}else{
S.push(y);
}
y = strtok(NULL, "/");} while (!S.isEmpty()){
T.push(S.pop());
} z = strcpy(z, "/");
while (!T.isEmpty()){
strcat(z, T.pop());
strcat(z, "/");
}cout << z << endl; } |