I have to write an implementation of a binary search tree that can handle a library's stock. It reads a text file with all the books and add the books to the tree in alphabetic order. I have been fighting with the Insertar() function code for DAYS and i can't make it work properly, it basically receives a pointer for the root of the tree along all the data related to the book. If the root is NULL, then it inits a node with all the values entered in the function and asings the memory direction to the null node. The problem is, its doing it locally and in the end it doesnt assigns it. Can somebody help me correct that specific function please?
Functions and Structs:
nodoArbol: The node
ArbolBin: Binary Tree, it has a pointer to a root node and an int with the number of elements
InitNodo: Inits the node, returns pointer to node
Raiz: Returns a pointer to the root of a Binary Tree
Clear,Clear_Aux: Clears a Tree
Ingresar: Insert() function and the source of the problem
Imprimir: rints the elements of a node.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct nodoArbol {
char nombre[51],autor[51];
int valor,stock,anno;
struct nodoArbol *der;
struct nodoArbol *izq;
} tNodoArbol;
typedef struct {
tNodoArbol *root;
int n;
} ArbolBin;
tNodoArbol* InitNodo(char *nombre,char *autor, int stock, int valor, int anno){
tNodoArbol *p;
p= (tNodoArbol*)malloc(sizeof(tNodoArbol));
strcpy(p->nombre, nombre);
strcpy(p->autor, autor);
p->stock = stock;
p->anno = anno;
p->valor = valor;
p->izq = NULL;
p->der = NULL;
return p;
}
tNodoArbol* Raiz(ArbolBin p){
return (&p)-> root;
}
void Init(ArbolBin *p){
p->root = NULL;
p->n = 0;
}
void clear_aux(tNodoArbol *nodo){
if (nodo == NULL){
return;
}
clear_aux(nodo->izq);
clear_aux(nodo->der);
free((void *) nodo);
}
void Clear(ArbolBin *p){
clear_aux(p->root);
p->root = NULL;
p->n = 0;
}
void Insertar (tNodoArbol *nodo, char *nombre,char *autor, int stock, int valor, int anno){
if (nodo == NULL){
nodo = (InitNodo(nombre,autor,stock,valor,anno));
}
else{
int result;
result = strcmp(nodo->nombre,nombre);
if (result>0){
Insertar (nodo->der, nombre,autor,stock,valor,anno);
}
else if (result<0){
Insertar (nodo->izq, nombre,autor,stock,valor,anno);
}
}
}
void Imprimir(tNodoArbol *nodo){
printf("Nombre:%s \n",nodo->nombre);
printf("Autor:%s \n",nodo->autor);
printf("Stock:%d \n",nodo->stock);
printf("Valor:%d \n",nodo->valor);
printf("anno:%d \n",nodo->anno);
}
int main(){
char a[50]= "holi",b[50] ="asdasdasd";
ArbolBin Tree;
tNodoArbol *Root;
Init(&Tree);
Root = Raiz(Tree);
Insertar(Root,a,b,2,1000,2014);
Imprimir(Root);
return 0;
}
tNodoArbol *Root;
Insertar(Root,a,b,2,1000,2014);
void Insertar (tNodoArbol *nodo, char *nombre,char *autor, int stock, int valor, int anno){
if (nodo == NULL){
nodo = (InitNodo(nombre,autor,stock,valor,anno));
}
else{
int result;
result = strcmp(nodo->nombre,nombre);
if (result>0){
Insertar (nodo->der, nombre,autor,stock,valor,anno);/*nodo just a pointer,node->der is illeagl*/
}
else if (result<0){
Insertar (nodo->izq, nombre,autor,stock,valor,anno);/*the same error */
}
}
}
-----------------------------------------------------------------------------------------
your declaration a pointer, you want through the Insertar() change the Root, you need use
Insertar(&Root,a,b,2,1000,2014), because the Root in the Insertar() is not the Root in the main() ,they just have the same value,we just copy the value of Root(main) to Root(Insertar).
---------------------------------------------------------------------------------------
void Insertar (tNodoArbol **nodo, char *nombre,char *autor, int stock, int valor, int anno){
if (*nodo == NULL){
*nodo = (InitNodo(nombre,autor,stock,valor,anno));
}
else{
int result;
result = strcmp((*nodo)->nombre,nombre);
if (result>0){
Insertar ((*nodo)->der, nombre,autor,stock,valor,anno);
}
else if (result<0){
Insertar ((*nodo)->izq, nombre,autor,stock,valor,anno);
}
}
}
Related
So basically what my code does is creates a WaitingRoom as the HEAD of a list and then it adds students in it ordered alphabetically, this works just fine, but when i try to add another watiningroom it gives me seg fault at the insertinRoom function at firts if.
This is my code, but when i run it with only one WaitingRoom created it works perfectly but if i add another one then it gives me segfault.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct studenti{
char name[100];
struct studenti* next;
}studenti;
typedef struct WaitingRoom{
int ID;
char parola[100];
struct WaitingRoom* next;
struct studenti* next_S;
}WaitingRoom;
studenti* CreateStudent(char* name)
{
studenti* S = (studenti*)malloc(sizeof(studenti));
strcpy(S->name,name);
S->next = NULL;
return S;
}
WaitingRoom* createWaitingRoom(int ID, char* parola){
WaitingRoom* WR = (WaitingRoom*)malloc(sizeof(WaitingRoom));
strcpy(WR->parola,parola);
WR->ID = ID;
WR->next = NULL;
return WR;
}
void insertinRoom(WaitingRoom* WR,char* name){
studenti* S;
S = CreateStudent(name);
if(WR->next_S == NULL || strcmp(WR->next_S->name,name)>0){
S->next=WR->next_S;
WR->next_S = S;
}
else{
studenti* current = WR->next_S;
while(current->next !=NULL && strcmp(current->next->name,name)>0)
{
current=current->next;
}
S->next = current->next;
current->next = S;
}
}
void printWaitingRoom(WaitingRoom* WR){
studenti* current = WR->next_S;
printf("Waiting Romom este %d cu parola %s\n",WR->ID,WR->parola);
while(current!=NULL){
printf("%s\n",current->name);
current = current->next;
}
}
void printall(WaitingRoom* WR){
WaitingRoom* current = WR;
while(current!=NULL){
printWaitingRoom(current);
current = current->next;
}
}
int main()
{
WaitingRoom* WR1 = createWaitingRoom(6969,"Muiedinamo");
insertinRoom(WR1,"BOOm1");
insertinRoom(WR1,"AOOm2");
insertinRoom(WR1,"DOOm3");
insertinRoom(WR1,"COOm4");
/*
WaitingRoom* WR2 = createWaitingRoom(1234,"AStae ");
insertinRoom(WR2,"B1");
insertinRoom(WR2,"AO2");
insertinRoom(WR2,"D3");
insertinRoom(WR2,"C4");
WR1->next = WR2;
*/
printWaitingRoom(WR1);
//printall(WR1);
return 0;
}
I was expecting to created my WaitingRoom and then connecting the waitingrooms and printing them 1 by 1 and the the hole list with "PrintAll" function.
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.
I have to make a list that arrange the people in decreasing order of their number('no' for my program). I tryed to make it by modifying the addNode function but I got no result(peoples do not arrange by their number). This is my code:
Header code:
#ifndef __EX__
#define __EX__
typedef struct Person{
char name[10];
float no;
struct Person *pNext;
} NODE, *pNODE, **ppNODE;
void addNode(ppNODE, pNODE);
void travers(pNODE, unsigned int*);
#endif
Functions folder:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <string.h>
#include "EX.h"
void addNode (ppNODE ppPrim, pNODE p){
pNODE q = (pNODE)malloc(sizeof(NODE));
assert(q!=NULL);
printf("Add name: \n");
scanf("%s", &q->name);
printf("\nAdd no: ");
scanf("%f", &q->no);
if (p == NULL || q->no < p->no) {
q->pNext = *ppPrim;
*ppPrim = q;
} else {
q->pNext = p->pNext;
p->pNext = q;
}
return;
}
void travers(pNODE pPrim, unsigned int *pLen){
*pLen = 0;
pNODE tmp = pPrim;
while (tmp != NULL){
puts (tmp->name);
fprintf(stdout, " no %.2f\n", tmp->no);
tmp = tmp->pNext;
(*pLen)++;
}
return;
}
Main folder:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <string.h>
#include "EX.h"
int main(){
unsigned int len;
pNODE prim = NULL;
int i;
for (i=0; i<=1; i++){
addNode(&prim, prim);
addNode(&prim, prim->pNext);
}
travers(prim, &len);
return 0;
}
When you insert a new node to the list, you must traverse the list until you find a suitable place to insert it. Your code takes a second argument, which isn't really needed and causes confusion, and only looks at that.
The code to insert a code q at the end of a list that is defined by its head is:
Node *prev = NULL;
Node *p = *head;
while (p) {
prev = p;
p = p->pNext;
}
q->pNext = p;
if (prev == NULL) {
*head = q;
} else {
prev->pNext = q;
}
You can get rid of keeping track of the previous node and the distinction between inserting at the head and inserting after that by traversing the list with a pointer to node pointer:
Node **p = &head;
while (*p && (*p)->no < q->no) {
p = &(*p)->pNext;
}
q->pNext = *p;
*p = q;
In this concise code, p holds the address of the head at first and the address of the pNext pointer of the previous node. Both can be updated via *p.
You can now use this code to traverse only as far as the numbers associated with each node are smaller than the one of the node to insert. here's a complete program:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct Node Node;
void addNode(Node **p, const char *name, float no);
void travers(Node *pPrim, unsigned int *pLen);
struct Node {
char name[10];
float no;
Node *pNext;
};
void addNode(Node **p, const char *name, float no)
{
Node *q = malloc(sizeof(*q));
assert(q != NULL);
snprintf(q->name, sizeof(q->name), "%s", name);
q->no = no;
while (*p && (*p)->no < q->no) {
p = &(*p)->pNext;
}
q->pNext = *p;
*p = q;
}
void traverse(const Node *pPrim, unsigned int *pLen)
{
*pLen = 0;
while (pPrim != NULL) {
fprintf(stdout, "%-12s%.2f\n", pPrim->name, pPrim->no);
pPrim = pPrim->pNext;
(*pLen)++;
}
}
int main()
{
unsigned int len;
Node *prim = NULL;
addNode(&prim, "Alice", 0.23);
addNode(&prim, "Bob", 0.08);
addNode(&prim, "Charlie", 0.64);
addNode(&prim, "Dora", 0.82);
traverse(prim, &len);
printf("\n%u entries.\n", len);
return 0;
}
Things to node:
I've used Node * and Node ** instead of the typedeffed pNODE and ppNODE. In my opinion using the C pointer syntax is clearer.
You should separate taking user input from adding a node.
In your code you shouldn't pass the address of the char array when scanning a string, just the char array. (It happens to work, but it isn't correct. The compiler should warn you about that.)
Is there any way to take a string input(as like we take for any integer) by linked list??
For example:This code is showing run time error:
struct node
{
char c;
struct node *link;
};
while(1)
{
val=getch();
if(val!=10)
add(&a[i],val);
else
break;
}
and I want to take any input string like - "asdfghj", of which string length was not known?
Given that you have a LinkedList-class that acts as an interface to the linked list and that it has the function addNode() that adds a node in a correct manner to the list.
I also assume that what you want to know is how to make every char in the inputted string a node in the linked list and that you know how to manage a linked list.
And assuming you're using C++11
int main()
{
LinkedList list;
string input;
cin >> input;
for(auto i: input)
{
list.addNode(i);
}
}
example for C
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
char c;
struct node *link;
} Node;
typedef struct s_ {
Node *top;
Node *curr;
} String;
Node *Node_new(char ch){
Node *p = calloc(1, sizeof *p);
p->c = ch;
return p;
}
String *String_new(void){
String *p = calloc(1, sizeof *p);
return p;
}
void String_drop(String *s){
Node *p = s->top;
while(p){
s->curr = p;
p = p->link;
free(s->curr);
}
//s->top = s->curr = NULL;
free(s);
}
void String_add(String *s, char c){
if(s->top == NULL){
s->curr = s->top = Node_new(c);
} else {
s->curr = s->curr->link = Node_new(c);
}
}
String *get_string(FILE *fp){
String *s = String_new();
int ch;
while(EOF!=(ch=fgetc(fp)) && ch !='\n'){
String_add(s, (char)ch);
}
return s;
}
void put_string(String *s){
Node *p;
for(p = s->top; p ; p = p->link)
putchar(p->c);
putchar('\n');
}
int main(void) {
String *s = get_string(stdin);
put_string(s);
String_drop(s);
return 0;
}
You can think easily. As You just can declare a string variable instead of char. And after that You can take input normally by creating a struct variable. For example:
#include <bits/stdc++.h>
using namespace std;
struct node
{
string s;
struct node *link;
};
int main(){
node ob;
cin>>ob.s;
cout<<ob.s;
}
I am implementing an avl tree for my assignment.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
struct TreeNode {
char *item;
struct TreeNode *left;
struct TreeNode *right;
signed char balance;
};
typedef struct TreeNode Node;
void _print_avl (Node *, int , const char *);
Node * get_new_node (char *);
int avl_insert(Node *, char *);
void print_avl (Node *);
void avl_swr(Node*);
int main (int argc, char *argv[])
{
Node *root = get_new_node("thura");
avl_insert(root, "thur2");
print_avl(root);
avl_insert(root, "thur1");
return 0;
}
int avl_insert(Node *root, char *item)
{
assert(root);
if( strcmp(item, root->item) < 0) {
if(!root->left) {
root->left = get_new_node(item);
if(--(root->balance)) return 1;
return 0;
} else {
if(avl_insert(root->left, item)) {
if( root->balance-- < 0) {
avl_swr(root); //Rotate the node right.
print_avl(root); //Here, the tree is corrupted.
return 0;
}
return 1;
}
}
} else {
if(!root->right) {
root->right = get_new_node(item);
if(++(root->balance)) return 1;
return 0;
}
else {
if(avl_insert(root->right, item)) {
root->balance++;
return 1;
}
}
}
return 0;
}
void avl_swr(Node* root)
{
Node *node = root;
root = node->left;
node->left = NULL;
node->balance = 0;
root->right = node;
root->balance++;
print_avl(root); // It is working fine here.
}
Node * get_new_node (char *item) {
Node * node = (Node *)malloc(sizeof(Node));
node->item = item;
node->left = NULL;
node->right = NULL;
node->balance = 0;
return node;
}
void print_avl (Node *node)
{
_print_avl(node, 0, "\t\t");
}
void _print_avl (Node *node, int depth, const char *delim)
{
if(!node)
return;
int i = 0;
while(i++ < depth) printf("%s", delim);
printf("--> %s:%d\n", node->item, node->balance);
depth++;
if(node->left)
_print_avl (node->left, depth, delim);
if(node->right)
_print_avl (node->right, depth, delim);
}
The problem is when I rotate the tree, using avl_swr (), it is successfully rotated according to the print_avl (), but when the function returns to the caller, the tree is corrupted. Any ideas?
The problem with avl_swr() is related to the function signature: void avl_swr(Node* root) and the assignment: root = node->left;
The root pointer is not being updated when the function returns (only a local copy within the function is being updated). The signature should be: void avl_swr(Node** root) in order to have the desired result.
The copy of the pointer is updated. You need to pass in a pointer to a pointer in your rotate function.
That's because the root variable in avl_insert does not change in avl_swr. When you pass it to avl_swr, a copy of the pointer is made. You change this pointer.
Change the calls to root = avl_swr(...) and have avl_swr return the root.
not 100% sure, but I do see one problem. In avl_swr() you change root to left subtree. So when you print out in avl_swr() you'll have root = "thur2". But when you return to avl_insert(), root there is unchanged, still pointing to "thura", which now has no children. So when you print that root it shows no children. Perhaps that's what you mean by corrupted?
The solution is obviously to change the "root" in avl_insert(). You can do this by having avl_swr return the new root value, or by changing the parameter from "Node* root" to "Node** root" so that change in avl_swr is "passed back" to avl_insert