This code is about binary tree.
It can work well.
But after I press the enter key and get the correct answer,it turns out stopping working.WHY?
This is the answer
source code:
#include <stdio.h>
#include <stdlib.h>
typedef struct BiTNode
{
char data;
struct BiTNode* rchild;
struct BiTNode* lchild;
}BiTNode;
typedef BiTNode* BiTree;
int CreateBiTree(BiTree *T);
void Visit(BiTree T);
void PreOrder(BiTree T);
void InOrder(BiTree T);
void PostOrder(BiTree T);
int main(void)
{
BiTree T;
CreateBiTree(&T);
PreOrder(T);
return 0;
}
int CreateBiTree(BiTree *T)
{
char data;
scanf("%c",&data);
if(data=='#')
{
*T==NULL;
}
else
{
*T=(BiTree)malloc(sizeof(BiTNode));
(*T)->data=data;
CreateBiTree(&(*T)->lchild);
CreateBiTree(&(*T)->rchild);
}
return 0;
}
void Visit(BiTree T)
{
printf("%c",T);
}
void PreOrder(BiTree T)
{
if(T!=NULL)
{
Visit(T);
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
In your Code you have many issues :-
In CreateBiTree(BiTree *T) function, you only modified single pointer for root, rchild and lchild. you should define Bitree *T locally in this function.
what is *T==NULL? Are you initializing or comparing?
scanf("%c",&data); this statement will also create a problem in character case. this statement will wait for '\n' also. so you should write scanf(" %c",&data).
*T=(BiTree)malloc(sizeof(BiTNode)); this statement is also wrong, malloc return pointer, so you should do correct type casting. *T=(BiTree *)malloc(sizeof(BiTNode));
i have modified you source code for your reference, have look
//it can work
#include<stdio.h>
#include<stdlib.h>
using namespace std;
typedef struct BiTNode
{
char data;
struct BiTNode* rchild;
struct BiTNode* lchild;
}BiTNode;
typedef BiTNode BiTree;
BiTree *CreateBiTree();
void Visit(BiTree **T);
void PreOrder(BiTree *T);
void InOrder(BiTree T);
void PostOrder(BiTree T);
int main(void)
{
BiTree *T;
T=CreateBiTree();
PreOrder(T);
return 0;
}
BiTree *CreateBiTree()
{
BiTree *T;
char data;
scanf(" %c",&data);
printf("............%c\n",data);
if(data=='#')
return NULL;
T=(BiTree *)malloc(sizeof(BiTree));
T->data=data;
printf("Enter left child of %c:\n",data);
T->lchild=CreateBiTree();
printf("Enter right child of %c:\n",data);
T->rchild=CreateBiTree();
return T;
}
void Visit(BiTree **T)
{
printf("%c",(*T)->data);
}
void PreOrder(BiTree *T)
{
if(T!=NULL)
{
Visit(&T);
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
Related
why i am getting stack is empty every time?
I am trying to make a expression tree from postfix here.
what's the logical error here?? plus we are not allowed to declare any variable globally. so, i had to pass the stackarray and node every time in each time function calling.
i am posting the full code done by me please have a look, i know its might be a simple error but as a beginner please show some kindness.
please help. TIA :)
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXSIZE 10
struct ExpnTreeNode
{
struct ExpnTreeNode *lchild;
char data;
struct ExpnTreeNode *rchild;
};
struct stackarray
{
struct ExpnTreeNode *data[MAXSIZE];
int top;
};
void push(struct ExpnTreeNode *newnode, struct stackarray *s);
struct ExpnTreeNode* pop( struct stackarray *s);
int check(char c);
void operand(struct ExpnTreeNode *newnode,char m, struct stackarray *s);
void operators(struct ExpnTreeNode *newnode,char m, struct stackarray *s);
void printex(struct ExpnTreeNode *node);
int main(void)
{
struct ExpnTreeNode *p=NULL;
struct stackarray s={{NULL},-1} ;
int len;
int x;
int res;
int i;
char arr[30]={'\0'};
printf("\n\tEnter a postfix expression:");
scanf("%d",&arr);
struct ExpnTreeNode *newnode=NULL;
len=strlen(arr);
for(i=0;arr[i]!='\0';i++)
{
x=check(arr[i]);
if(x==1)
{
operand(newnode,arr[i],&s);
}
else if(x==2)
{
operators(newnode,arr[i],&s);
}
}
p=pop(&s);
printex(p);
return 0;
}
void push(struct ExpnTreeNode *newnode, struct stackarray *s)
{
if(s->top==MAXSIZE-1)
{
printf("\n\tStack is Full");//Stack is Full
}
else
{
s->top=s->top+1;
s->data[s->top]=newnode;
}
}
struct ExpnTreeNode* pop( struct stackarray *s)
{
if(s->top==-1)
{
printf("\n\tStack is Empty");
return;
}
s->top=s->top-1;
return(s->data[s->top]);
}
int check(char c)
{
if(c=='*' || c=='/' || c=='+' || c=='-')
{
return 2;
}
else
{
return 1;
}
}
void operand(struct ExpnTreeNode *newnode,char m, struct stackarray *s)
{
struct stack *fs=NULL;
fs=s;
newnode=(struct ExpnTreeNode *)calloc(1,sizeof(struct ExpnTreeNode));
newnode->data=m;
newnode->lchild=NULL;
newnode->rchild=NULL;
push(newnode,&fs);
}
void operators(struct ExpnTreeNode *newnode,char m, struct stackarray *s)
{
newnode=(struct ExpnTreeNode *)calloc(1,sizeof(struct ExpnTreeNode));
struct stack *fs=NULL;
fs=s;
newnode->data=m;
newnode->rchild=pop(&fs);
newnode->lchild=pop(&fs);
push(newnode,&fs);
}
void printex(struct ExpnTreeNode *node)
{
if(node!=NULL)
{
printf("\n\t%c",node->data);
printex(node->lchild);
printex(node->rchild);
}
}
I'm trying to create a node to create a linked list, but I'm getting the error
"IntelliSense: a value of type "void *" cannot be assigned to an entity of type "C""
Isn't the value of C which is a synonym for B: NULL not void?
Therefore shouldn't the initial node be created with NULL and then manipulation of the linked list will occur?
I haven't continued the insert function because the creation of the node is not working
#include <stdio.h>
#include <stdlib.h>
struct A
{
char data;
struct A *nextPtr;
};
typedef struct A B;
typedef B *C;
void insert(C *sPtr, char value);
void print(C cPtr);
void menu(void);
int main (void)
{
menu();
C startPtr = NULL;
char c;
int x;
for (x = 0; x <6; x++)
{
insert(&startPtr, c);
print(startPtr);
}
}
void menu(void)
{
puts("Enter 1 to Add: \nEnter 2 to Remove \nEnter 3 to quit");
}
void print (C cPtr)
{
puts("Names in the List");
printf("%c ->",cPtr->data);
}
void insert(C *sPtr, char value)
{
C nPtr;
C pPtr;
C cPtr;
nPtr = malloc( sizeof (B));
}
I'm trying to build a stack using a linked list but I get a EXC_BAD_ACCESS error in my linkedListStackInit method;
LinkedList.h
#ifndef LinkedListStack_h
#define LinkedListStack_h
#ifndef __TYPE
#define __TYPE
#define TYPE int
#define TYPE_SIZE sizeof(int)
#endif
#include <stdio.h>
struct Link;
struct LinkedListStack;
void linkedListStackInit(struct LinkedListStack *s);
void push(struct LinkedListStack *s, TYPE data);
void pop(struct LinkedListStack *s);
TYPE top(struct LinkedListStack *s);
int isEmpty(struct LinkedListStack *s);
#endif
LinkedList.c
#include <stdlib.h>
#include "LinkedListStack.h"
struct Link {
TYPE value;
struct Link *next;
};
struct LinkedListStack {
struct Link *firstLink;
};
void linkedListStackInit(struct LinkedListStack *s) {
s->firstLink = 0;
}
void push(struct LinkedListStack *s, TYPE data) {
struct Link *newLink = malloc(sizeof(struct Link));
// Assert?
newLink->next = s->firstLink;
newLink->value = data;
s->firstLink = newLink;
}
void pop(struct LinkedListStack *s) {
struct Link *temp = s->firstLink;
s->firstLink = s->firstLink->next;
free(temp);
}
TYPE top(struct LinkedListStack *s) {
return s->firstLink->value;
}
int isEmpty(struct LinkedListStack *s) {
if(s == NULL) {
return 0;
}
else {
return 1;
}
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "LinkedListStack.h"
int main(int argc, const char * argv[]) {
struct LinkedListStack *s;
linkedListStackInit(s);
return 0;
}
From your main method you are calling the function linkedListStackInit and passing stack (s) to it. But you haven't allocated memory to s before passing it to the linkedListStackInit function. The function linkedListStackInit doesn't allocate the memory either and tries to assign a value to its "firstlink" member. Try to do the following in your linkedListStackInit function and see if you can proceed further.
s = malloc(sizeof(struct LinkedListStack));
I have written a program on queues and dynamic memory allocation. This is what my program needs to do - insert values in to the queue and remove it from the queue; that simple.
But my problem is that it just prints the names of the variables the values are assigned to and the program goes not responding.
Here is my program :
#include <stdio.h>
#define MAX 180
struct cakes{
int spongecake;
int meringue;
int chocalate;
int red_velvet;
struct newcake *next;
};
struct Queue{
int front;
int rear;
int count;
int cake[10];
};
void init(struct Queue *);
int isFull(struct Queue *);
void insert(struct Queue *,int);
int isEmpty(struct Queue *);
int removes(struct Queue *);
void cake_order(struct cakes *);
void order_out(struct cakes *);
main()
{
struct cakes *head;
head=(struct cakes *)malloc(sizeof(struct cakes ));
cake_order(&head); //this is a seperate function and it works perfectly
head->next=(struct cakes *)malloc(sizeof(struct cakes));
order_out(&head->next);
}
void init(struct Queue *q)
{
q->front=0;
q->rear=10-1;
q->count=0;
}
int isFull(struct Queue *q)
{
if(q->count==10)
{
return 1;
}
else
{
return 0;
}
}
void insert(struct Queue *q,int x)
{
if(!isFull(q))
{
q->rear=(q->rear+1)%10;
q->cake[q->rear]=x;
q->count++;
}
}
int isEmpty(struct Queue *q)
{
if(q->count==0)
{
return 1;
}
else
{
return 0;
}
}
int removes(struct Queue *q)
{
int caked=NULL;
if(!isEmpty(q))
{
caked=q->cake[q->front];
q->front=(q->front+1)%10;
q->count--;
return caked;
}
}
void order_out(struct cakes *order)
{
struct Queue s;
int i;
order->spongecake=20;
order->meringue=75;
order->chocalate=40;
order->red_velvet=30;
init(&s);
for(i=0;i<10;i++)
{
insert(&s,order->chocalate);
insert(&s,order->spongecake);
insert(&s,order->meringue);
insert(&s,order->red_velvet);
}
while(!isEmpty(&s))
{
printf("%d",removes(&s));
}
}
What seems to be the problem here?
I am new to C, so yea am a bit slow when debugging in this language.
Thank you for your time.
Here is the output:
Lots of problems here, first it would be better if main was declared properly as in int main() and then it returned a value at the end e.g. return 0; like:
int main()
{
.... // code
return 0; // normally 0 is returned if execution has been successful
}
There seem to be other problems with the code as I wasn't able to compile it, for example there's no closing brace at the end of order_out() (right after the while loop).
Also would be good if you provided the cake_order() function.
It's also missing the includes for say stdlib.h, and on line 45 (head=(struct cakes *)malloc(sizeof(struct cakes ));) I've noticed you cast the result of malloc, which is not necessary.
And if I may further add, don't remember to free() the memory you've allocated with malloc(). I didn't see a single free() statement in your code.
Hello i am trying to return a struct from a function but i cant find a way to do so without declaring the struct as global. How can this be done? Here is the code (THIS WORKS AS IT IS)
...
void log_in();
struct node
{
char name_log[20];
int passlog;
int user_point;
}tmp;
int main()
{
...
else if(sel=='2')
{
log_in();
if (tmp.passlog==TRUE)
logged_in(tmp.name_log,tmp.user_point); //and here i want to use the retun values
}
void log_in()
{
... //make the changes in the struct
}
...
What i want to achieve is to place the struct node declaration within main but sadly it wont work. So here is what i am trying to do: (THIS DOESN'T WORK)
...
struct node log_in();
int main() {
...
else if(sel=='2') {
struct node //here is where i want to declare
{
char name_log[20];
int passlog;
int user_point;
}tmp;
log_in();
if (tmp.passlog==TRUE)
logged_in(tmp.name_log,tmp.user_point); //and here i want to use the retun values
}
struct node log_in()
{
...
return tmp;
}
...
else if(sel=='2') //or within this block but I don't know how.
{ struct node tmp;
tmp=log_in();
if (tmp.passlog==TRUE)
logged_in(tmp.name_log,tmp.user_point); //and here I want to use the return values
}
and inside the function log_in()
struct node log_in()
{
struct tmp
...
return tmp;
}
use a local variable inside the function and return this variable. Assign it to another variable inside main().
Declare the structure first, then create the variable temp. Like this:
struct node
{
char name_log[20];
int passlog;
int user_point;
};
Then you can create the local variable like
struct node tmp;
Pass a pointer to the struct node to your log_in function and have it return a boolean value so the caller can check whether logging in succeeded or didn't. (Note I'm trying to guess what you want to achieve, and I might be guessing wrong.)
#include <stdio.h>
#include <string.h>
struct node {
char name_log[20];
int passlog;
int user_point;
};
int log_in(char, struct node *);
int log_in(char sel, struct node * tmp) {
int ret = 0;
if (sel == '2') {
ret = 1;
strcpy( tmp->name_log, "Gonzo" );
tmp->passlog = 33;
tmp->user_point = 99;
}
return ret;
}
int main(int argc, char ** argv) {
struct node tmp;
char sel = argv[1][0];
if ( log_in(sel, &tmp) ) {
// tmp initialized
printf( "%s, %d, %d\n", tmp.name_log, tmp.passlog, tmp.user_point );
}
else {
// tmp not initialized
}
}
Call passing 2 on the command line. (If you don't, undefined behaviour.)
If you want to use some struct in 2 different routines - you must declare it outside of both of them since they both have to see how this struct is structured.
BTW - you invoke log_in but do not use its return value.
You can't operate on a type that is unknown. If log_in() doesn't know the definition of struct node, it can't use it directly. The only thing it can do is somehow receive a pointer to a variable of this type and then either treat it as raw data (sequence of bytes) or cast said pointer to a pointer to a known to log_in() type and work with that.
You can also redefine struct node inside of log_in(), which is a way of making log_in() operate on a known type:
void log_in(void*);
void logged_in(char*, int);
int main(void)
{
int sel = '2';
if (sel == '2')
{
struct node
{
char name_log[20];
int passlog;
int user_point;
} tmp;
log_in(&tmp);
if (tmp.passlog)
logged_in(tmp.name_log, tmp.user_point);
}
return 0;
}
void log_in(void* n)
{
struct node
{
char name_log[20];
int passlog;
int user_point;
} *p = n;
p->passlog = 1;
}
void logged_in(char* name, int point)
{
}
If you don't want to pass tmp by a formal reference into log_in(), you must make it available globally. For example like this:
void log_in(void);
void logged_in(char*, int);
void* pTmp;
int main(void)
{
int sel = '2';
if (sel == '2')
{
struct node
{
char name_log[20];
int passlog;
int user_point;
} tmp;
pTmp = &tmp;
log_in();
if (tmp.passlog)
logged_in(tmp.name_log, tmp.user_point);
}
return 0;
}
void log_in(void)
{
struct node
{
char name_log[20];
int passlog;
int user_point;
} *p = pTmp;
p->passlog = 1;
}
void logged_in(char* name, int point)
{
}