Hi all, me again. I had my programming working completely correctly, and that was using the GCC windows compiler.
Now I move onto fedora core, it's giving me alot of weird errors, errors that I can't see....
here is my complete code, again!
Code:
#include<stdio.h>
#include"stack.h"
stack_t* stack = NULL; //Global variable as both functions use it.
int stack_size = 0; //Size of stack (number of values inside it)
int main(int argv, char*argc[])
{
int a,b,c,d;
printf("Enter any 4 integers seperated by spaces ");
scanf("%d %d %d %d", &a,&b,&c,&d); //Read in user input
push(a, &stack); //Push each value onto the stack
push(b, &stack);
push(c, &stack);
push(d, &stack);
peek(&stack,stack_size); //Call peek
printf("::%d:: is popped off the stack\n", pop(&stack)); //Pop a value off the stack
peek(&stack,stack_size); //Call peek
printf("::%d:: is popped off the stack\n", pop(&stack)); //Pop a value off the stack
peek(&stack,stack_size); //Call peek
return 0;
}
/*::::::::::::::FUNCTIONS::::::::::::::*/
void push(int data, stack_t **stackptr) //Pushes integer onto the stack
{
stack_t* push_element; //Stack pointer
push_element = (stack_t*) malloc(sizeof(stack_t)); //Allocating memory
push_element->data = data; //Data assigned
push_element->next = (*stackptr); //Next is now equal to the address [which is of type pointer ] that stackptr [pointer to a pointer] points to.
(*stackptr) = push_element; //Address stackptr points to now stores new_element
stack_size++; //Increment stack size, as integer has been pushed
}
int pop(stack_t **stackptr) //Pop integer off the stack
{
int data; //Used to store data
stack_t* pop_element; //Stack pointer
if (stack_size != 0) //If stack is not empty
{
pop_element = (*stackptr); //pop_element is now equal to the address [which is of type pointer ] that stackptr [pointer to a pointer] points to.
data = pop_element->data; //Data is copied
(*stackptr) = pop_element->next; //Stackptr now points to pop_element->next.
free(pop_element); //Remove pointer after usage.
stack_size--; //Decrement stack size, as integer has been popped.
return data; //Return the data that has been popped
}
}
void peek(stack_t **stackptr, int size) //Displays all values in the stack
{
stack_t* peek_element; //Stack pointer
stack_t* store_element; //Stack pointer
int i;
int data;
store_element = (*stackptr); //Stack pointer stores address that *stackptr points to.
printf("Peeking at stack:: ");
for ( i=0; i < size; i++) //Loop untill i = stack size.
{
peek_element = (*stackptr); //Peek_element is now equal to the address [which is of type pointer ] that stackptr [pointer to a pointer] points to.
data = peek_element->data; //Data is copied
(*stackptr) = peek_element->next; //Stackptr now points to peek_element->next
printf("%d ", data); //Prints value of data
}
printf("\n"); //New line
(*stackptr) = store_element; //*stackptr now points to store_element, restoring it's orginal value.
}
The following are the errors I get...
stack2.c: In function `peek':
stack2.c:4: error: parameter `stack' is initialized
stack2.c:5: error: parameter `stack_size' is initialized
stack2.c:7: error: syntax error before '{' token
stack2.c:43: error: syntax error before "if"
stack2.c:60: error: syntax error before "store_element"
I can't see any errors in what I have done? I have bolded them so you can find them easily.
I'm really sorry to keep on asking, but I just cannot understand why this isn't working.