My code is basically functions used for making/using a stack. I've tried almost everything, but I don't know why my program is displaying this error:
Error: Syntax error before 'struct'
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
#define CAPACITY 128
struct stack_struct {
ElemType items[CAPACITY];
int top;
};
StackPtr stk_create(){
StackPtr s = malloc(sizeof(struct stack_struct));
s->top = -1; // stack initially empty
return s;
}
// TODO
StackPtr stk_clone(StackPtr s) {
return NULL; // temporary placeholder
}
void stk_free(StackPtr s) {
free(s);
}
int stk_push(StackPtr s, ElemType val){
if(s->top == CAPACITY - 1)
struct stack_struct * temp;
temp = (struct stack_struct*)malloc(sizeof(struct stack_struct));
s->top++;
s->items[s->top] = val;
return 1;
}
ElemType stk_pop(StackPtr s){
if(s->top == -1)
abort(); // library function which terminates program!!!
s->top--;
return s->items[s->top+1];
}
int stk_is_full(StackPtr s){
return s->top == CAPACITY-1;
}
int stk_is_empty(StackPtr s){
return s->top == -1;
}
int stk_size(StackPtr s) {
return s->top+1;
}
void stk_clear(StackPtr s){
s->top = -1;
}
void stk_print(StackPtr s) {
int i;
printf("\n----TOP-----\n");
for(i=s->top; i>=0; i--) {
printf(FORMAT_STRING, s->items[i]);
}
printf("---BOTTOM---\n");
}
int main() {
StackPtr sptr;
sptr = stk_create();
stk_push(sptr, 1.7);
stk_push(sptr, 3.14);
stk_print(sptr);
stk_pop(sptr);
stk_print(sptr);
stk_free(sptr);
}
As I could see, function stack_push should look like this
int stk_push(StackPtr s, ElemType val){
if(stk_is_full(s))
return -1; // stack already full, we couldn't push new elem
s->top++;
s->items[s->top] = val;
return 1;
}
I think error in this line (line 35 in your source code):
struct stack_struct * temp;
. Let's try
typedef struct stack_struct * temp;
or change declare struct
struct stack_struct {
ElemType items[CAPACITY];
int top;} stack;
and then call
stack* temp;
in line 35.
Related
void insertSkipList(SkipList* list, void* I){
Node* new_node=createNode(I, randomLevel(list->max_level));
if (new_node->size > list->max_level)
Node* actual_node=list->head;
unsigned int k;
for (k = list->max_level;k>=1;k--){
if (actual_node->next[k] == NULL || list->compare(I, actual_node->next[k]->item)<0){
if (k < new_node->size) {
new_node->next[k] = actual_node->next[k];
actual_node->next[k]=new_node;
}
else{
actual_node->next = &actual_node->next[k];
k=k+1;
}
}
}
}
I'm having troubles with the command list->compare(I, actual_node->next[k]->item
as it doesn't execute at all. The problem is most likely here actual_node->next[k]->item but I can't see why.
These are the definitions of node and list
typedef struct _SkipList SkipList;
typedef struct _Node Node;
struct _SkipList {
Node *head;
unsigned int max_level;
int (*compare)(void*, void*);
};
struct _Node {
Node **next;
unsigned int size;
void *item;
};
The complete minimal reproducible example contained within the question itself is:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_LENGTH 20
#define MAX_HEIGHT 5
typedef struct _SkipList SkipList;
typedef struct _Node Node;
struct _SkipList {
Node *head;
unsigned int max_level;
int (*compare)(void*, void*);
};
struct _Node {
Node **next;
unsigned int size;
void *item;
};
unsigned int randomLevel(unsigned int height);
static int compare_int(void* x_void,void* y_void){
int x=(int)x_void;
int y=(int)y_void;
return x-y;
}
static Node* createNode(void* item, unsigned int lvl) {
Node* n = (Node*) malloc(sizeof(Node));
if(n == NULL) {
printf("\nError! Node memory not allocated.");
exit(0);
}
n->item = item;
n->next = NULL;
n->size = lvl;
return n;
}
SkipList* createSkipList(unsigned int height, int (*compare)(void*, void*)){
SkipList* skiplist = (SkipList*) malloc(sizeof(SkipList));
if(skiplist == NULL) {
printf("\nError! Skiplist memory not allocated.");
exit(0);
}
skiplist->head=createNode(NULL,height);
skiplist->max_level=1;
skiplist->compare=(*compare);
return skiplist;
}
void insertSkipList(SkipList* list, void* I){
Node* new_node=createNode(I, randomLevel(list->max_level));
if (new_node->size > list->max_level)
list->max_level = new_node->size;
Node* actual_node=list->head;
unsigned int k;
printf("here it's before the loop\n");
for (k = list->max_level;k>=1;k--){
if (actual_node->next[k] == NULL || list->compare(I, actual_node->next[k]->item)<0){ //here the code stops completely
if (k < new_node->size) {
new_node->next[k] = actual_node->next[k];
actual_node->next[k]=new_node;
}
}
else{
actual_node->next = &actual_node->next[k];
k=k+1;
}
}
printf("here it's after the loop (and actually this wont get printed idk why\n");
}
unsigned int randomLevel(unsigned int height){
unsigned int lvl = 1;
time_t t;
srand((unsigned) time(&t));
while (rand() < 0.5 && lvl < height)
lvl = lvl + 1;
return lvl;
}
int main() //creates a skiplist that goes from 0 to MAX_LENGTH
{
skiplist=createSkipList(MAX_HEIGHT,(*compare_int));
int found[MAX_LENGTH];
int expected[MAX_LENGTH];
for(int i=0;i<MAX_LENGTH;i++){
insertSkipList(skiplist,(void*) i);
}
return 0;
}
Basically I made a create_app() function to allocate 2 nodes in the stack, each having a pointer to an array[max]; undo() pops the last element, and before returning it, it adds it into the REDO node's array. redo() does the opposite, pops the last element in it's array, putting it into Undo's array before returning it. What did I do wrong ?
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define EMPTY_TOS (-1)
typedef struct node *node_ptr;
struct node
{
int arr_size;
int tos;
int *arr_stack;
node_ptr next;
};
typedef node_ptr STACK;
STACK
create_app(int max)
{
STACK UNDO = (STACK) malloc(sizeof(struct node));
STACK REDO = (STACK) malloc(sizeof(struct node));
{
UNDO->arr_stack == (int *) malloc(max * sizeof(int));
REDO->arr_stack == (int *) malloc(max * sizeof(int));
if(UNDO->arr_stack != NULL){printf("Out of space!");}
else
{
UNDO->tos = EMPTY_TOS;
REDO->tos = EMPTY_TOS;
UNDO->arr_size = max;
REDO->arr_size = max;
UNDO->next = REDO;
REDO->next = UNDO;
return UNDO;
}
}
}
int
isEmpty(STACK S)
{
return(S->tos==-1);
}
int
isFull(STACK S)
{
return(S->tos>=S->arr_size-1);
}
void
push(int x, STACK S)
{
if(isFull(S)){printf("Stack full!");}
else
{
S->arr_stack[++S->tos] = x;
}
}
int
undo(STACK S)
{
if(isEmpty(S)){printf("Nothing to undo!");}
else
{
S->next->arr_stack[++S->next->tos] = S->arr_stack[S->tos];
printf("%d",S->arr_stack[S->tos--]);
}
}
int
redo(STACK S)
{
if(isEmpty(S->next)){printf("Nothing to redo!");}
else
{
int temp = S->next->arr_stack[S->next->tos];
push(S->next->arr_stack[S->next->tos], S);
S->next->tos--;
printf("%d",temp);
}
}
int main()
{
STACK app = create_app(5);
push(1,app);
push(2,app);
push(3,app);
undo(app);
undo(app);
redo(app);
redo(app);
/* Expected output: 3223 */
return 0;
}
Some small errors were in your code, like these ones in create_app() which seem like typos.
UNDO->arr_stack == (int *) malloc(max * sizeof(int));
REDO->arr_stack == (int *) malloc(max * sizeof(int));
^
|
if(UNDO->arr_stack != NULL){printf("Out of space!");}
^
|
...
and some int returning functions did not return anything in the else part which gave some warnings.
Here is the modified code, which worked fine for me
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define EMPTY_TOS (-1)
typedef struct node* node_ptr;
struct node
{
int arr_size;
int tos;
int *arr_stack;
node_ptr next;
};
typedef node_ptr STACK;
STACK
create_app(int max)
{
STACK UNDO = (STACK) malloc(sizeof(struct node));
STACK REDO = (STACK) malloc(sizeof(struct node));
{
UNDO->arr_stack = (int *) malloc(max * sizeof(int));
REDO->arr_stack = (int *) malloc(max * sizeof(int));
if(UNDO->arr_stack == NULL){printf("Out of space!");
return NULL;}
else
{
UNDO->tos = EMPTY_TOS;
REDO->tos = EMPTY_TOS;
UNDO->arr_size = max;
REDO->arr_size = max;
UNDO->next = REDO;
REDO->next = UNDO;
return UNDO;
}
}
}
int
isEmpty(STACK S)
{
return (S->tos == -1);
}
int
isFull(STACK S)
{
return (S->tos >= S->arr_size-1);
}
void
push(int x, STACK S)
{
if(isFull(S)){printf("Stack full!");}
else
{
S->arr_stack[++S->tos] = x;
}
}
void
undo(STACK S)
{
if(isEmpty(S)){printf("Nothing to undo!");}
else
{
S->next->arr_stack[++S->next->tos] = S->arr_stack[S->tos];
printf("%d",S->arr_stack[S->tos--]);
}
}
void
redo(STACK S)
{
if(isEmpty(S->next)){printf("Nothing to redo!");}
else
{
int temp = S->next->arr_stack[S->next->tos];
push(S->next->arr_stack[S->next->tos], S);
S->next->tos--;
printf("%d",temp);
}
}
int main()
{
STACK app = create_app(5);
push(1,app);
push(2,app);
push(3,app);
undo(app);
undo(app);
redo(app);
redo(app);
/* Expected output: 3223 */
return 0;
}
Result:
3223
However, always take precaution in deallocating the memory malloced using free().
I'm trying a C programming code which will convert a decimal number into binary number and the binary values will be stored in a stack
According to my code, when running it shows some error responses. When trying to display the binary number using peek method, the application runs without an end.
#include<stdio.h>
#include<stdlib.h>
#define MAX 50
typedef enum{FALSE, TRUE} boolean;
typedef struct stack{
int top;
int a[MAX];
} stack;
void CreateStack(stack *s){
s->top = -1;
}
boolean isEmpty(stack *s){
return (s->top == -1);
}
boolean isFull(stack *s){
return(s->top == MAX - 1);
}
void push(stack *s, int data){
if(isFull(s)){
exit(1);
}
else{
s->top = s->top + 1;
s->a[s->top] = data;
}
}
int pop(stack *s){
if(isEmpty(s)){
exit(1);
}
else{
return s->a[s->top];
s->top = s->top - 1;
}
}
int peek(stack *s){
return s->a[s->top];
}
void binary(stack *s, int num){
int n;
while(num != 0){
if(!isFull(s)){
n = num % 2;
push(s,n);
num = num / 2;
}
else{
exit(1);
}
}
}
void main() {
stack s;
CreateStack(&s);
int num,n;
printf("Enter the decimal number: ");
scanf("%d",&num);
binary(&s,num);
printf("Top = %d\n",peek(&s));
while(!isEmpty(&s)){
printf("%d ",pop(&s));
}
}
pop() contains dead code — it returns value but top decrement placed after the return operator and will be newer reached.
int pop(stack *s){
if(isEmpty(s)){
exit(1);
}
else{
return s->a[s->top];
s->top = s->top - 1; // <------------
}
}
Change it in following manner:
int pop(stack *s){
if(isEmpty(s)){
exit(1);
}
else{
int temp = s->a[s->top];
s->top = s->top - 1;
return temp;
}
}
p.s. As for me, else keyword can be omitted here but it is taste issue:
int pop(stack *s) {
int temp;
if(isEmpty(s))
exit(1);
temp = s->a[s->top];
s->top = s->top - 1;
return temp;
}
I'm implementing a graph traversal breadth-first search that I found here. However, their implementation involves integers and without any linked list. I was playing around with it a little bit I have no luck in getting any results because it doesn't seem to work as intended.
This is the code that I currently have:
(main.c)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
typedef struct s_list
{
struct s_list *next;
void *content;
} t_list;
typedef struct s_queue
{
t_list *first;
t_list *last;
} t_queue;
typedef struct s_node
{
struct s_node *next;
int vertex;
} t_node;
typedef struct s_graph
{
t_node **adj_lists;
int *visited;
int total_vertices;
} t_graph;
/*Graph functions*/
t_node *create_node(int vertex);
t_graph *create_graph(int vertices);
void add_edge(t_graph *graph, int src, int dst);
void bfs(t_graph *graph, int start_vertex);
/*Misc functions*/
void my_putstr(char *str);
void *my_memalloc(size_t size);
void *my_memset(void *ptr, int value, size_t num);
void my_bzero(void *s, size_t n);
/*Queue functions*/
t_queue *init_queue(void);
void enqueue(t_queue *queue, void *content);
void *dequeue(t_queue *queue);
void *peek_queue(t_queue *queue);
int is_empty(t_queue *queue);
void my_print_queue(t_queue *queue);
t_node *create_node(int val)
{
t_node *new_node;
new_node = (t_node*)my_memalloc(sizeof(t_node));
new_node->vertex = val;
new_node->next = NULL;
return (new_node);
}
t_graph *create_graph(int vertices)
{
int i;
t_graph *graph;
i = 0;
graph = my_memalloc(sizeof(t_graph));
graph->total_vertices = vertices;
printf("graph->total_vertices: %d\n", vertices);
graph->adj_lists = (t_node**)my_memalloc(sizeof(t_node));
graph->visited = my_memalloc(sizeof(int) * vertices);
while (i < vertices)
{
graph->adj_lists[i] = NULL;
graph->visited[i] = 0;
i++;
}
return (graph);
}
void add_edge(t_graph *graph, int src, int dst)
{
t_node *new_node;
new_node = create_node(dst);
new_node->next = graph->adj_lists[src];
graph->adj_lists[src] = new_node;
new_node = create_node(src);
new_node->next = graph->adj_lists[dst];
graph->adj_lists[dst] = new_node;
}
void bfs(t_graph *graph, int start_vertex)
{
t_queue *queue;
queue = init_queue();
graph->visited[start_vertex] = 1;
printf("start_vertex before enqueue %d\n", start_vertex);
my_print_queue(queue);
enqueue(queue, &start_vertex);
printf("start_vertex after enqueue %d\n", start_vertex);
while (!is_empty(queue))
{
my_print_queue(queue);
int current_vertex;
t_node *tmp;
current_vertex = (int)dequeue(queue);
printf("Visited %d nodes\n", current_vertex);
tmp = graph->adj_lists[current_vertex];
while (tmp)
{
int adj_vertex;
adj_vertex = tmp->vertex;
if (graph->visited[adj_vertex] == 0)
{
graph->visited[adj_vertex] = 1;
printf("%d\n", graph->visited[adj_vertex]);
enqueue(queue, &adj_vertex);
my_print_queue(queue);
}
tmp = tmp->next;
}
}
}
t_queue *init_queue(void)
{
t_queue *node;
node = (t_queue *)my_memalloc(sizeof(t_queue));
node->first = NULL;
node->last = NULL;
return (node);
}
void enqueue(t_queue *queue, void *content)
{
t_list *node;
node = (t_list *)my_memalloc(sizeof(t_list));
node->content = content;
node->next = NULL;
if (!queue->last)
{
queue->last = node;
queue->first = node;
}
else
{
queue->last->next = node;
queue->last = queue->last->next;
}
return ;
}
void *dequeue(t_queue *queue)
{
t_list *tmp;
tmp = queue->first;
if (!tmp)
return (NULL);
else
{
queue->first = tmp->next;
return (tmp->content);
}
}
void *peek_queue(t_queue *queue)
{
if (queue->first == NULL)
return (NULL);
return (queue->first->content);
}
int is_empty(t_queue *queue)
{
return (queue->first == NULL);
}
void my_print_queue(t_queue *queue)
{
if (is_empty(queue))
my_putstr("Empty queue\n");
else
{
while (!is_empty(queue))
{
int val = *(int *)queue->first->content;
printf("%d \n", val);
dequeue(queue);
}
}
}
void my_putstr(char *str)
{
int i;
i = 0;
while (str[i])
write(1, &str[i++], 1);
}
void *my_memalloc(size_t size)
{
char *str;
str = ((void*)malloc(size));
if (!str)
return (NULL);
my_bzero(str, size);
return (str);
}
void *my_memset(void *ptr, int value, size_t num)
{
unsigned char *uptr;
uptr = (unsigned char*)ptr;
while (num--)
*uptr++ = (unsigned char)value;
return (ptr);
}
void my_bzero(void *s, size_t n)
{
my_memset(s, 0, n);
}
int main(void)
{
t_graph *graph;
graph = create_graph(3);
add_edge(graph, 0, 1);
add_edge(graph, 0, 2);
add_edge(graph, 2, 4);
bfs(graph, 2);
return (0);
}
I did some research like type-casting a void pointer to make it into a char or int, or any other data type. What happens is that the create graph does it's creation after calling it; but, when it comes to the bfs, it doesn't show the correct output after. It never prints the visited vertices. I'm getting a result of
graph->total_vertices: 3
start_vertex before enqueue 2
Empty queue
start_vertex after enqueue 2
2
Visited 0 nodes
The expected output should be something like this:
Queue contains
0 Resetting queueVisited 0
Queue contains
2 1 Visited 2
Queue contains
1 4 Visited 1
Queue contains
4 Resetting queueVisited 4
I've been trying to figure out by myself to the point that I'm burning out. What am I doing wrong in here?
While posting this, I will keep debugging on my side and see what it does with a couple print statements.
I can point out the following mistakes:
my_print_queue destroys your queue. So anything after it's call works with empty queue.
You don't fill visited with to zeroes. By default their values is pretty much arbitrary. Since you compare their values with 0, it makes sense that comparison fails.
#include <stdio.h>
#include <stdlib.h>
/* Report an error and abort */
#define FATAL_ERROR(message) \
{ \
fprintf(stderr,"In %s(%d) [function %s]: %s\n", \
__FILE__, __LINE__, __FUNCTION__ , (message) ); \
abort(); \
} \
/* Report a posix error (similar to perror) and abort */
#define FATAL_PERROR(errcode) FATAL_ERROR(strerror(errcode))
void* Malloc(size_t n)
{
void* new = malloc(n);
if(new==NULL) FATAL_ERROR("Out of memory.");
return new;
}
typedef struct twit{
char data[141]; //contains the actual data
//struct twit *prev; //pointer to previous node (Closer to front)
struct twit *next; //pointer to next node (Closer to back)
}twit;
typedef struct twitbuffer{
twit *first;
twit *last;
int size;
}twit_buffer;
/*
function for create a new buffer
*/
void new_twitbuffer(twit_buffer *a)
{
a=Malloc(sizeof(twit)*12000);
a->first = a->last = NULL;
a->size = 0;
return;
}
int twitbuffer_empty(twit_buffer *a) {
if(a->first == NULL)
return 1;
else
return 0;
}
/*
function to insert a new twit in the buffer
*/
void insertTwit(twit_buffer *a, char *data)
{
twit new;
if (strlen(&data)<=140){
strcpy(&new.data,data);
}
else{
printf("Twit > 140 characters...");
}
if (new.data == NULL) {
//errno = ENOMEM;
printf("error!");
return;
}
if(a->first==NULL){
a->first = a->last = &new;
}else{
a->last->next=&new;
a->last=&new;
}
new.next= NULL;
a->size++;
return;
}
char* popTwit(twit_buffer *a) {
if (twitbuffer_empty(a)) {
return NULL;
}
char *data;
//strcpy(&data,a->first->data);
data=a->first->data;
if (a->first == a->last)
a->first = a->last = NULL;
else
a->first = a->first->next;
a->size--;
return data;
}
twit_buffer mytwitbuffer;
int main()
{
new_twitbuffer(&mytwitbuffer);
//printf("a=%d",mytwitbuffer);
char *a = "first twit\n";
char *b = "second twit\n";
char *c = "third twit\n";
insertTwit(&mytwitbuffer, a);
insertTwit(&mytwitbuffer, b);
insertTwit(&mytwitbuffer, c);
char *poppp;
poppp = popTwit(&mytwitbuffer);
printf("%s", poppp);
poppp = popTwit(&mytwitbuffer);
printf("%s", poppp);
poppp = popTwit(&mytwitbuffer);
printf("%s", poppp);
}
This is my code for an implementation of a queue. When i execute this i take this result:
thir�it
(null)(null)
This means that the two first insertions are not being done correctly and the third goes in stdout in a "paranormal" way! Do you have any ideas?
You are storing a reference to the local variable 'new' in your twit buffer. You should Malloc it, instead Of declaring It local