Unexpected output in a linked list - c

Sometime back I asked a question about linked list and got nice replies...Now I've written a new code using the suggestions but I've run into an error. The code is:
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *mknode()
{
return malloc(sizeof(node));
}
void create(node* h, int num)
{
int i;
node *temp=h;
for(i=0;i<num;i++)
{
temp->data=i;
if(i==(num-1))
temp->next=NULL;
else
temp->next=mknode();
temp=temp->next;
}
}
node* add_end(node *h,int num)
{
node *temp;
if(h==NULL)
{
h=mknode();
temp=h;
create(h,num);
}
else
{
temp=h;
while(h!=NULL){
h=h->next;}
h=mknode();
create(h,num);
}
return temp;
}
void display(node *h)
{
node *temp=h;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
}
int main()
{
node *head=NULL;
int num;
scanf("%d",&num);
head=add_end(head,num);
head=add_end(head,num);
display(head);
//printf("%d",list_len(head));
free(head);
return 0;
}
Now since I've called add_end twice for an input of 3 the output should be
0->1->2->0->1->2->
But instead I'm getting
0->1->2->
I've checked this much that the FOR loop inside create function is running 2n times for an input of n.
So the problem is that display function encounters a NULL but I can't figure out where in the code is it happening.
All help appreciated.
Thanking in advance
-user1614886

In add_end() you do not link the nodes correctly.
[blah blah]
else
{
temp=h;
while(h!=NULL){
h=h->next;}
h=mknode();
create(h,num);
}
You advance h until it is NULL but you should only move until h->next == NULL and append your next list there.

Related

C language:Binary search tree with strings

I have to create a program for a binary search tree for strings, which calculates how many times each word repeats, and it seems like I can't get the program to do as intended, because when printing it out, what should be printed first, are the words/characters smaller than the origin node, followed by the words/characters bigger than the origin node. But what my program does, it just prints it out in the order that I introduced the words/characters, I am still thinking about, how to realise the part of the program that will calculate the number of appearances of each word.
#include<stdio.h>
#include<stdlib.h>
#define MAX 15
typedef struct BST
{
char data[MAX];
struct BST *left;
struct BST *right;
} node;
node *create();
void insert(node *,node *);
void preorder(node *);
int main()
{
char ch;
node *root=NULL,*temp;
do
{
temp=create();
if(root==NULL)
{
root=temp;
}
else
{
insert(root,temp);
}
printf("\nDo you want to enter more(y/n)?");
ch=getch();
}
while(ch=='y'||ch=='Y');
printf("\nPreorder Traversal: ");
preorder(root);
return 0;
}
node *create()
{
node *temp;
printf("\nEnter data:");
temp=(node*)malloc(sizeof(node));
fgets(&temp->data,MAX,stdin);
temp->left=temp->right=NULL;
return temp;
}
void insert(node *root,node *temp)
{
if(temp->data<root->data)
{
if(root->left!=NULL)
insert(root->left,temp);
else
root->left=temp;
}
if(temp->data>root->data)
{
if(root->right!=NULL)
insert(root->right,temp);
else
root->right=temp;
}
}
void preorder(node *root)
{
if(root!=NULL)
{
printf("%s ",root->data);
preorder(root->left);
preorder(root->right);
}
}
Here's what I came up with, with the help of somebody in the comments, and it works as intended
void insert(node *root,node *temp)
{
int cmp_rezult=strcmp(temp->data,root->data);
printf("\nCompare:%d ", cmp_rezult);
if(cmp_rezult<0)
{
if(root->left!=NULL)
insert(root->left,temp);
else
root->left=temp;
}
if(cmp_rezult>0)
{
if(root->right!=NULL)
insert(root->right,temp);
else
root->right=temp;
}
}
In insert you do:
temp->data<root->data
but that's not how you compare strings in C.
To compare strings use strcmp
Further it seems that insert miss a code block for handling "equal".
So something like:
int cmp_result = strcmp(temp, data<root->data);
if (cmp_result < 0)
{
// Handle less than
}
else if (cmp_result > 0)
{
// Handle greater than
}
else // i.e. cmp_result == 0
{
// Handle equal
}

can anyone find out why is the tree not linking

i am writing a code to create a binary tree but something is going wrong i tried to debug but could not find can anyone find it.
code that i have tried is as follow..
header files are..
#include< stdio.h>
#include< stdlib.h>
structure is..
struct node {
int data;
struct node *left;
struct node *right;
}
other declaration..
*head,*p=NULL;
int count=0;
create() function is..
void create(int m) {
if(count==0)
{
p=(struct node *)malloc(sizeof(struct node));
p->data=m;
p->left=NULL;
p->right=NULL;
head=p;
count++;
}
else {
p=(struct node *)malloc(sizeof(struct node));
p->data=m;
p->left=NULL;
p->right=NULL;
}
}
main function is..
int main()
{
int n,i,m;
scanf("%d",&n);
for(i=0;i<n;i++) {
scanf("%d",&m);
if(i==0) {create(m);}
if(i>0) {
while(1) {
if(m < p->data)
p=p->left;
else
p=p->right;
if(p==NULL)
{
create(m);
break;
}
}
}
p=head;
}
printf("%d",p->left->data); //printing the data
return 0;
}
Think, when i = 1, then count is also 1,
if(m<p->data)
{
p=p->left;
}
what will be happened when p->left is a null? Now p is a null pointer, right? Then you did this...
if(p==NULL)
{
create(m);
break;
}
Lets see, what is happening in create(m). Now in create() else portion will work.
else
{
p=(struct node *)malloc(sizeof(struct node));
p->data=m;
p->left=NULL;
p->right=NULL;
}
It is also OK. Now you have a child of head. Reference of this child is in p. Right? But the mistake which you have done is,
p=head;
That means, what have you done is gone. Now there is no reference of child of head. p is keeping reference of head. See, head->left or head->right is always NULL whatever you keep in p. So you have to create a node where you find a NULL, not in p. It is not giving you any result.
Hope, it will help you. :)

Making of linked list by head and node

here's my code in C for making of linked list. Its giving runtime error after the while loop gets executed for one time. Plz help me in correcting my code. (totally confused that where's the error.) I am making a head node first and then adding child nodes to it.
#include <stdio.h>
#include <stdlib.h>
typedef struct node nd;
typedef nd *link;
struct node{
int data;
link next;
};
typedef struct {
int size;
link head;
}list;
void create(link temp)
{
link new;
new=(link)malloc(sizeof(nd));
printf("enter data: ");
scanf("%d",new->data);
temp->next=new;
temp=temp->next;
}
list createlist()
{
list sl;
sl.size=0;
sl.head=0;
return sl;
}
int main()
{
list sl;
sl=createlist();
link temp;
temp=sl.head;
char c;
while (1)
{
printf("Add node?: ");
scanf(" %c",&c);
if (c=='y')
{
create(temp);
sl.size++;
}
else
break;
}
return 0;
}
your createlist() function is returning a reference to a local variable that goes out of scope after it returns. You should instead return a heap based value:
list* createlist() {
list* sl = (list*)malloc(sizeof(list));
sl->size=0;
sl->head=0;
return sl;
}
Initially temp points to NULL. temp = sl.head;
In create(temp) temp->next = new;
You are dereferencing a NULL, address 0x0. I get a segmentation fault when I do that.
Need to change the algorithm.
A debugger shows this problem immediately.
You could use a pointer to pointer for temp. It would be easier to read if you didn't use a typedef for a pointer to node. I haven't tested this, but it should be close:
nd ** create(nd **temp)
{
nd *new;
new=(nd *)malloc(sizeof(nd)); /* this cast shouldn't be needed */
printf("enter data: ");
scanf("%d",&(new->data));
new->next = NULL;
*temp = new;
return &(new->next);
}
/* ... */
int main()
{
nd **temp;
temp = &(sl.head);
/* ... */
temp = create(temp);
/* ... */
}

What is wrong with this insertion sort using linked list code in c?

I am getting error while executing this. Compiler does not give any error but when executed it give random output.
What am i doing is taking input from user & storing them to linked list & then implementing insertion sort.
( I am able to call the subroutine insertion sort, so I guess the problem lies within subroutine only)
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
};
typedef struct node *NODEPTR;
NODEPTR getnode();
NODEPTR insertionSortLinkedList(NODEPTR head);
int main()
{
int i,n,temp;
NODEPTR head,lptr,prevlptr;
printf("No of input integers to be sorted\n");
scanf("%d",&n);
if (n<2){printf("n should be atleast 2 \n");return 0;}
printf("\nType space-separated array of %d integers\n",n);
scanf("%d",&temp);
head=getnode();
head->info=temp;
head->next=NULL;
prevlptr=head;
for (i=0;i<n-1;i++)
{
scanf("%d",&temp);
lptr=getnode();
lptr->info=temp;
prevlptr->next=lptr;
lptr->next=NULL;
prevlptr=lptr;
}
head=insertionSortLinkedList(head);
lptr=head;
while(lptr!=NULL)
{
printf("%d ",lptr->info);
prevlptr=lptr;
lptr=lptr->next;
free(prevlptr);
}
return 0;
}
NODEPTR getnode()
{
NODEPTR p;
p=(struct node*)malloc(sizeof(struct node));
return p;
}
NODEPTR insertionSort(NODEPTR head)
{
NODEPTR listptr,tempptr,prevptr,prevtempptr;
prevptr=head;
listptr=prevptr->next;
while(listptr!=NULL)
{
while (listptr->info < prevptr->info)
{
prevptr->next=listptr->next;
tempptr=head;
prevtempptr=head;
while(tempptr->info <= listptr->info)
{
prevtempptr=tempptr;
tempptr=tempptr->next;
}
if(tempptr->info == prevtempptr->info)
{
listptr->next=head;
head=listptr;
}
else
{e
listptr->next=prevtempptr->next;
prevtempptr->next=listptr;
}
listptr=prevptr->next;
if (listptr==NULL)
break;
}
prevptr=prevptr->next;
if (prevptr=NULL)
break;
listptr=prevptr->next;
}
return(head);
}
What is wrong with my code and how do I fix it?
The 6th line form last
if (prevptr=NULL)
it should be
if (prevptr==NULL)
Single "=" does the assignment instead of comparison.

What is wrong with my array of linked lists implementation as all elements get over-written?

I'm trying to implement a hash table as an array of linked lists. Currently I'm trying to have a simple hash table where the key is the index of the array and value is a singly linked list for implementing chaining.
This is the code that I've written so far:
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int value;
struct Node *next;
};
struct Node *hashtable[7];
int empty(int index)
{
if(hashtable[index]==NULL)
return 0;
return 1;
}
void addNode(int frame,struct Node **iter)
{
if(*iter==NULL)
*iter=malloc(sizeof(struct Node));
else
{
while((*iter)->next != NULL)
(*iter)=(*iter)->next;
(*iter)->next=malloc(sizeof(struct Node));
(*iter)=(*iter)->next;
}
(*iter)->value=frame;
(*iter)->next=NULL;
}
void print()
{
int i;
struct Node **iter;
for(i=0;i<7;i++)
{
iter=&hashtable[i];
while(*iter !=NULL)
{
printf("%d%s%d\n",(*iter)->value,"--",i);
(*iter)=(*iter)->next;
}
}
}
int main()
{
int i=0,count=7;
for(i=0;i<7;i++)
hashtable[i]=NULL;
i=empty(1);
printf("%d",i);
do
{
printf("Enter no:\n");
scanf("%d",&i);
struct Node** temp;
temp=&hashtable[i-1%7];
addNode(rand(),temp);
count--;
print();
} while(count > 0);
return 0;
}
When I'm calling print, I can only see one element added to one particular index, which is the last element that was added, what am I doing wrong here?
void add_node(int frame,struct Node **iter)
{
/* find (pointer to) NULL pointer at end of chain */
for ( ; *iter; iter = &(*iter)->next ) {;}
*iter = malloc(sizeof **iter );
(*iter)->value = frame;
(*iter)->next = NULL;
}

Resources