C/C++ Printing stack bottom to top with recursion - c

Hey I am trying to print a "stack" from bottom to top using recusion. Everything in the code is as should and only the print function should be modified. I am not getting any recursion print and unfortunately I do not know why.
struct element{
int i;
element *next;
};
struct stack{
element *top;
};
void print(stack *st);
int main(){
element *e5 = (element*) malloc(sizeof(element));
element *e4 = (element*) malloc(sizeof(element));
element *e3 = (element*) malloc(sizeof(element));
element *e2 = (element*) malloc(sizeof(element));
element *e1 = (element*) malloc(sizeof(element));
e5->i = 5; e5->next = NULL;
e4->i = 4; e4->next = e5;
e3->i = 3; e3->next = e4;
e2->i = 2; e2->next = e3;
e1->i = 1; e1->next = e2;
stack start;
start.top = e1;
element *e;
print(&start);
return 0;
}
void print(stack *st){
stack nxt;
nxt.top = st->top->next;
if (st->top != NULL){
print(&nxt);
printf("%i\n", st->top);
}
else
printf("%i\n", st->top->i);
}
Thank you for your input!

In your else you have a possible crash on this line:
printf("%i\n", st->top->i); //<-- possible crash here
You should be more clear about what errors you see from you program.
It looks to me like this will crash the program before anything is printed.

There are other problems (see Botje's comment), but also:
printf("%i\n", st->top);
What is the type of st->top? What happens if you print it with format %i?

Related

how to initializing a hash table in C

I have a program in C that creates a hash table.
memset is Okay but, i want to initialize with for loop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HSZ 127
#define HASHING(x) ((x)%HSZ)
struct node_t{
int val;
struct node_t *next;
};
struct node_t *hash_table[HSZ];
void init(void){
int i;
//memset(hash_table,0,sizeof(hash_table));
for(i=0; i<HSZ; i++){
hash_table[i]->val = 0;
hash_table[i]->next = NULL;
}
}
void insert_hash(int value){
int key = HASHING(value);
struct node_t *newNode = (struct node_t*)malloc(sizeof(struct node_t));
newNode->val = value;
newNode->next = NULL;
if(hash_table[key] == NULL){
hash_table[key] = newNode;
} else {
newNode->next = hash_table[key];
hash_table[key] = newNode;
}
}
int delete_hash(int value){
int key = HASHING(value);
if (hash_table[key] == NULL)
return 0;
struct node_t *delNode = NULL;
if (hash_table[key]->val == value){
delNode = hash_table[key];
hash_table[key] = hash_table[key]->next;
} else {
struct node_t *node = &hash_table[key];
struct node_t *next = hash_table[key]->next;
while (next){
if (next->val == value){
node->next = next->next;
delNode = next;
break;
}
node = next;
next = node->next;
}
}
return 1;
free(delNode);
}
void PrintAllHashData()
{
printf("###Print All Hash Data###\n");
for (int i = 0; i < HSZ; i++){
if (hash_table[i] != NULL){
printf("idx : %d ", i);
struct node_t *node = hash_table[i];
while (node->next){
printf("%d ", node->val);
node = node->next;
}
printf("%d\n", node->val);
}
}
}
int main(void){
init();
insert_hash(1);
insert_hash(3);
insert_hash(128);
PrintAllHashData();
}
look at this code.
for(i=0; i<HSZ; i++){
hash_table[i]->val = 0;
hash_table[i]->next = NULL;
}
The IDE I am using does not throw up a compilation error when I compile the code, but during the execution the code faults and is terminated/haulted. I tried debugging the code, it faults at this line and is stopped, I think BAD ACCESS points to Segmentation Error.
then, I changed this line to
for(i=0; i<HSZ; i++){
hash_table[i].val = 0;
hash_table[i]->next = NULL;
}
but, then I got the compilation error stating 'structure type require instead of 'struct node_t *'
I think that I don't understand clearly about struct in C.
How to fix this problem?
What you are dealing with is Undefined Behavior.
See, struct node_t *hash_table[HSZ];
So, hash_table is an array of HSZ (127) pointers of the data type struct node_t.
When you do,
for(i=0; i<HSZ; i++){
hash_table[i]->val = 0;
hash_table[i]->next = NULL;
}
hash_table[0] to hash_table[126] pointers are not pointing to anything.
So, each of them (or all of them) should be initialized first to point to an object of the type struct node_t and then you can initialize them. For that matter, Using a memset does not cause a problem because memset is filling the contents of the pointers with all zeros. There is difference between filling the pointers with all zeros and filling all zeros to the memory pointed by pointers.
Trying this,
for(i=0; i<HSZ; i++){
hash_table[i].val = 0;
hash_table[i]->next = NULL;
}
is plain wrong.
To fix the issue you are facing, you need to allocate memory dynamically using malloc. You can do the in your for loop.
for(i = 0; i < HSZ; i++)
{
//Allocate memory of the size struct_node_t
hash_table[i] = malloc(sizeof(struct node_t)); //Do not cast!
//Check if memory is allocated
if(hash_table[i] == NULL)
{
//Memory not allocated, set some error state to handle and break
break;
}
//Initialize to zero
hash_table[i]->val = 0;
hash_table[i]->next = NULL;
}
struct node_t{
int val;
struct node_t *next;
};
struct node_t *hash_table[HSZ];
when you have *hash_table[HSZ], this varible hash_table is a pointer. so whatever your action is , use hash_table-> ,syntax for pointer, mean point to somewhere.
a suggestion that when you use pointer you should always allocate memory hash_table[i] = malloc(sizeof(struct node_t));
struct node_t hash_table;
but if you initilize your varible like this, you can use hash_table.val = 0
so the way of assign value depend on how you declare your varibles
struct node_t *hash_table[HSZ];
gives you an array of pointers that are unset (i.e. not pointing to anything)
void init(void) {
int i;
// memset(hash_table,0,sizeof(hash_table));
for (i = 0; i < HSZ; i++) {
hash_table[i]->val = 0;
hash_table[i]->next = NULL;
tries writing to your invalid pointers which gives undefined behavior.
Either make the array an array of structs (instead of pointers):
struct node_t hash_table[HSZ];
...
/* note use of . instead of -> since we have structs not pointers */
hash_table[i].val = 0;
or allocate the necessary structs so the array points to something:
for (i = 0; i < HSZ; i++) {
hash_table[i] = malloc(sizeof(struct node_t));
hash_table[i]->val = 0;
hash_table[i]->next = NULL;
}

Getting a malloc error when not using printf()

#include <stdio.h>
#include <stdlib.h>
typedef struct lis
{
int num;
struct lis * next;
} list;
void fun(list ** h, int nu) {
*h = malloc(sizeof(list)*nu);
list *p = *h;
int i=1;
list * nextx;
while(i<=nu) {
nextx = p + 1;
p->num = i;
p->next = nextx;
//printf("%d\n", nextx);
p += 1;
i++;
}
p->next = NULL;
}
int main(int argc, char const *argv[])
{
list * first = NULL;
fun(&first,10);
free(first);
return 0;
}
I'm learning lists in c
whenever this code is run it gives an malloc error
if i comment out printf("%d\n", nextx); which shows next node it works fine.
what is happening?
In the last run of the loop, your code do:
nextx = p+1; // points one past the last array' element
p->num = nu-1; // ok
p->next = p+1; // probably not what you wanted, but not a fault per se
p += 1; // This is the cause of your problem
i++; // out of the loop...
p->next = NULL; // dereference out of array pointer!
Quit the loop one step before and then set the last element correctly:
while (i<nu) {
...
}
p->next = NULL;
p->num = nu-1;

Creating List using pointers

I want to create a linked list and keep two pointers. One for the head and one for the last element. I tried several approaches and just found a solution, but I don't know why my first solution don't work. The pointer in head->next always points on the last element, instead of the correct one.
int main(void){ // first solution
struct pointList *lastElement;
struct pointList *head;
struct pointList headHelp = {.next = NULL, .p.x = 5, .p.y = 5};
head = &headHelp;
int i = 0;
lastElement = &headHelp;
for( i = 8; i < 10; i++){
printf("LastElement: %d/%d\n", lastElement->p.x, lastElement->p.y);
struct pointList *helpPoint;
helpPoint = ((struct pointList*) malloc(sizeof(struct pointList)));
struct pointList newElement = *helpPoint;
newElement.next = NULL;
newElement.p.x = i;
newElement.p.y = i;
lastElement->next = &newElement;
lastElement = &newElement;
}
//printList(head);
printf("LastElement: %d/%d\n", lastElement->p.x, lastElement->p.y);
printf("head -> next: %d/%d\n", head->next->p.x, head->next->p.y);
printf("finish\n");
return 0;
}
output:
LastElement: 5/5
LastElement: 8/8
LastElement: 9/9
head -> next: 9/9
finish
but it should be : head -> next: 8/8
So the "next" pointer of head is changed in every loop execution
The solution which works looks like this:
int main(void){
struct pointList *lastElement;
struct pointList *head;
struct pointList headHelp = {.next = NULL, .p.x = 5, .p.y = 5};
head = &headHelp;
int i = 0;
lastElement = &headHelp;
for( i = 8; i < 10; i++){
printf("LastElement: %d/%d\n", lastElement->p.x, lastElement->p.y);
lastElement->next = ((struct pointList*) malloc(sizeof(struct pointList)));
lastElement->next->next = NULL;
lastElement->next->p.x = i;
lastElement->next->p.y = i;
lastElement = lastElement->next;
}
//printList(head);
printf("LastElement: %d/%d\n", lastElement->p.x, lastElement->p.y);
printf("head -> next: %d/%d\n", head->next->p.x, head->next->p.y);
printf("finish\n");
return 0;
}
used struct:
struct pointList {
struct point p;
struct pointList *prev;
};
The problem with the first code snippet is that you take and use the pointer to a local variable. The variable newElement have its scope inside the loop only, so when the loop iterates the current newElement variable goes out of scope and you are left with a stray pointer. This leads to undefined behavior.
You also allocate memory for helpPoint but you don't actually use it (when you initialize newElement using helpPoint you initialize one uninitialized structure with another uninitialized structure), and you do not free it anywhere leading to memory leaks.

Struct and Pointer Segmentation Error in C

can anyone help with this segmentation error i keep getting. this code is simple but the error is so hard to figure out.
struct Link {
int key;
unsigned data: 2;
struct Link *next;
struct Link *previous;
};
struct Link* addInOrder(struct Link *, struct Link);
int main() {
struct Link *head;
struct Link data1;
struct Link data2;
struct Link data3;
data1.key = 25;
data1.data = 1;
data1.next = NULL;
data2.key = 50;
data2.data = 0;
data2.next = NULL;
data3.key = 100;
data3.data = 2;
data3.next = NULL;
head = NULL;
head = addInOrder(head, data2);
}
struct Link* addInOrder(struct Link *srt, struct Link l) {
if(!srt) {
return &l;
}
struct Link *temp = srt;
while(temp->next && l.key > temp->key)
temp = temp->next;
printf("here\n");
if(l.key > temp->key) {
printf(" 1\n");
temp->next = &l;
l.previous = temp;
}
else {
printf(" 2\n");
l.previous = temp->previous;
l.next = temp;
printf( "2.2\n");
if(temp->previous) {
//printf("%i\n",temp->previous->key);
temp->previous->next = &l;
}
printf(" 2.3\n");
temp->previous = &l;
}
return srt;
}
i keep getting an error at the first line in addInOrder(). all the compiler says is Segmentation Error.
Edit: also, if i add printf("..."); right after the if statement and run it ... does not print
You're passing the second argument of addInOrder() by value (struct Link l). This creates a copy of the argument when you call the function, and in addInOrder(), l exists on the stack. You're then returning the address of the local variable and assigning it to head, but when the function exits, that variable is out of scope and is deallocated. So you're assigning an invalid address to head, and that results in a segfault.
The part (and everywhere &l is used)
if (!srt)
return &l;
is returning the address of a stack variable.
Your addInOrder function should probably have the signature
struct Link* addInOrder(struct Link* srt, struct Link* l);

C Having Trouble Resizing a Hash Table

I'll post snippets of the code here which (I think) are relevant to the problem, but I can pastebin if necessary. Probably posting more than enough code already :P
My program includes a hash table which needs to double when a certain hash bucket reaches 20 entries. Although I believe the logic to be good, and it compiles like a charm, it throws up a Segmentation Fault. The code runs like a charm when not resizing, but resizing messes things up.
Thanks for any help :)
Error
Program received signal SIGSEGV, Segmentation fault.
0x0000000000401012 in ml_add (ml=0x7fffffffe528, me=0x75a5a0) at mlist.c:74
74 while((cursorNode->next) != NULL){
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.80.el6_3.5.x86_64
(gdb) backtrace
#0 0x0000000000401012 in ml_add (ml=0x7fffffffe528, me=0x75a5a0) at mlist.c:74
#1 0x0000000000401554 in main (argc=1, argv=0x7fffffffe638) at finddupl.c:39
Structure of Hash Table
typedef struct bN { //linked list node containing data and next
MEntry *nestedEntry;
struct bN *next;
} bucketNode;
typedef struct bL { // bucket as linked list
struct bN *first;
int bucketSize;
} bucket;
struct mlist {
struct bL *currentTable; //bucket array
};
Add Function
int ml_add(MList **ml, MEntry *me){
MList *tempList;
tempList = *ml;
bucketNode *tempNode = (bucketNode *)malloc(sizeof(bucketNode));
tempNode->nestedEntry = me;
tempNode->next = NULL;
unsigned long currentHash = me_hash(me, tableSize);
if((tempList->currentTable[currentHash].bucketSize) == 0) {
tempList->currentTable[currentHash].first = tempNode;
tempList->currentTable[currentHash].bucketSize = (tempList->currentTable[currentHash].bucketSize) + 1;
}
else if((tempList->currentTable[currentHash].bucketSize) == 20){
printf("About to resize");
printf("About to resize");
tempList = ml_resize(&tempList, (tableSize * 2));
tableSize = tableSize * 2;
ml_add(&tempList,me);
}
else{
bucketNode *cursorNode;
cursorNode = tempList->currentTable[currentHash].first;
while((cursorNode->next) != NULL){
cursorNode = cursorNode->next;
}
cursorNode->next = tempNode;
tempList->currentTable[currentHash].bucketSize = (tempList->currentTable[currentHash].bucketSize) + 1;
return 1;
}
return 1;
}
Resize Function
MList *ml_resize(MList **ml, int newSize){
MList *oldList;
oldList = *ml;
MList *newList;
if ((newList = (MList *)malloc(sizeof(MList))) != NULL){
newList->currentTable = (bucket *)malloc(newSize * sizeof(bucket));
int i;
for(i = 0; i < newSize; i++){
newList->currentTable[i].first = NULL;
newList->currentTable[i].bucketSize = 0;
}
}
int j;
for(j = 0; j < tableSize; j++){
bucketNode *cursorNode = oldList->currentTable[j].first;
bucketNode *nextNode;
while(cursorNode != NULL){
nextNode = cursorNode->next;
ml_transfer(&newList, cursorNode, newSize);
cursorNode = nextNode;
}
}
free(oldList);
return newList;
}
Transfer to new list function
void ml_transfer(MList **ml, bucketNode *insertNode, int newSize){
MList *newList;
newList = *ml;
bucketNode *tempNode = insertNode;
tempNode->next = NULL;
unsigned long currentHash = me_hash((tempNode->nestedEntry), newSize);
if((newList->currentTable[currentHash].bucketSize) == 0) {
newList->currentTable[currentHash].first = tempNode;
newList->currentTable[currentHash].bucketSize = (newList->currentTable[currentHash].bucketSize) + 1;
}
else{
bucketNode *cursorNode;
cursorNode = newList->currentTable[currentHash].first;
while((cursorNode->next) != NULL){
cursorNode = cursorNode->next;
}
cursorNode->next = tempNode;
newList->currentTable[currentHash].bucketSize = (newList->currentTable[currentHash].bucketSize) + 1;
}
}
The problem most probably lies on the fact that the ml_add() function is failing to update the MList** ml parameter node whenever the hashtable is resized.
When the hashtable is resized, the old hashtable is destroyed (inside, ml_resize()), but the pointer to the resized, new hashtable is just updated in the tempList variable, that is just a local copy of *ml. You should also update *ml in order to modify the variable that is keeeping reference of the hashTable outside of the function, otherwise, it is left pointing to the deleted, invalid Hashtable. Try the following modification:
...
else if((tempList->currentTable[currentHash].bucketSize) == 20){
printf("About to resize");
printf("About to resize");
tempList = ml_resize(&tempList, (tableSize * 2));
tableSize = tableSize * 2;
ml_add(&tempList,me);
*ml = tempList; // this is necesary to fix the pointer outside the
// function, that still points to the hashtable
// memory freed by the resize function
}
...
Also please note the comments I made about two memory leaks existing in your code, and I would also take into account what #hexist pointed out that it is not necessary to insert at the end of the liked list at the head, simplifying the code and making it faster.

Resources