not woking c program for SLL [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Q please help me this program is not working properly.
not displaying the value.this program is an example of singly linked list which I am trying to run on c.
`
#include<stdio.h>
#include<stdlib.h> //malloc defined
struct node
{
int data;
struct node *next;
};
add() //add function
{
int value;
struct node *n;
n=(struct node*)malloc(sizeof(struct node)); //mem allocation
printf("enter the value to add\n");
scanf("%d",&value);
n->data=value;
n->next=NULL;
// n=n->next;
// n->next=NULL;
}
delete() //delete function
{
// n=n->next;
struct node *n; //declaration
printf("the node deleted is %d",n->data);
free(n);
}
display() //display function
{
struct node *n;
while(n!=NULL)
{
printf("%d",n->data);
n=n->next;
}
}
int main()
{
int ch;
while(1)
{
printf("do you want to add node press 1\n");
printf("do you want to delete node press 2\n");
printf("do you want to display node press 3\n");
printf("do you want to exit press 4\n");
scanf("%d",&ch);
switch(ch)
{
case 1:add();
break;
case 2:delete();
break;
case 3:display();
break;
case 4:exit(0);
default: printf("wrong choice!!!\n");
}
}
return 0;
getch();
}
please help me this program is not working properly.
not displaying the value.this program is an example of singly linked list which I am trying to run on c.

printf("%d",n->data); is not printed because:
struct node *n; // n is not determined.
while(n != NULL) // undefined behaviour
n is different from the other n that is in the add() function. You never passed the struct to the other functions so it will not do what you wanted it to do.

in the function display(), the local variable 'n' is only declared but not defined, so here 'n' is only a wild pointer. It's happened in the function delete() as well.
it's not a correct way to implement the single linked list. In the function add() what you wrote is only create a node, but not add a node to linked list.

#include<stdio.h>
#include<stdlib.h> //malloc defined
struct node
{
int data;
struct node *next;
}*n,*p;
create() //add function
{
int value;
n=(struct node*)malloc(sizeof(struct node)); //mem allocation
printf("enter the value to add\n");
scanf("%d",&value);
n->data=value;
n->next=NULL;
}
add()
{
int value;
// struct node *p;
p=(struct node*)malloc(sizeof(struct node));
printf("enter the value to add next\n");
scanf("%d",&value);
n->next=p;
p->data=value;
p->next=NULL;
}
delete() //delete function
{
printf("the node deleted is %d",p->data);
n->next=NULL;
free(p);
}
display() //display function
{
while(n!=NULL)
{
printf("%d\n",n->data);
n=n->next;
}
}
int main()
{
int ch;
while(1)
{
printf("do you want to create node press 1\n");
printf("do you want to add node press 2\n");
printf("do you want to delete node press 3\n");
printf("do you want to display node press 4\n");
printf("do you want to exit press 5\n");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
break;
case 2:add();
break;
case 3:delete();
break;
case 4:display();
break;
case 5:exit(0);
default: printf("wrong choice!!!\n");
}
}
return 0;
getch();
}

Related

Node value changing automatically in C

I'm creating a program for binary search trees and this is a function that I'm using. As seen in the output, the value of
root->rightChild is changing for unknown reasons and the program end without showing any errors.
typedef struct node * BST;
struct node
{
struct node *leftChild;
int data;
struct node *rightChild;
};
BST temp=NULL, ptr=NULL, root=NULL, prev=NULL;
BST newNode()
{
BST X;
X=(BST)malloc(sizeof(BST));
X->leftChild=X->rightChild=NULL;
return X;
}
void createBST(int elem)
{
temp=newNode();
temp->data=elem;
if(!root)
{
root=temp;
printf("\nroot-?rightChild= %p",root->rightChild);
printf("\nroot-?leftChild= %p",root->leftChild);
}
else
{
printf("\nroot-?rightChild= %p",root->rightChild);
printf("\nroot-?leftChild= %p",root->leftChild);
prev=NULL;
ptr=root;
while(ptr!=NULL)
{
prev=ptr;
ptr=(ptr->data<temp->data)?ptr->rightChild:ptr->leftChild;
}
if(prev->data<temp->data)
prev->rightChild=temp;
else
prev->leftChild=temp;
}
}
int main()
{
int choice;
while(1)
{
printf("\nPick a Binary Search Tree operation\n1) Create a Binary Search Tree\n2) Traverse the Binary Search Tree\n3) Seach for a KEY element in the Binary Search Tree\n4) Exit\n>| ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
int num,elem;
printf("\nEnter the number of elements to be inserted into the Binary Search Tree: ");
scanf("%d",&num);
printf("\nEnter the elements to be inserted into the Binary Search Tree: ");
for(int i=1;i<=num;i++)
{
scanf("%d",&elem);
createBST(elem);
}
}
break;
case 2:
traverBST();
break;
case 3:
searchBST();
break;
case 4:
exitProgram();
break;
default:
printf("\nInvalid Choice\n");
}
}
return 0;
}
This is the output of the code:
This statement has a typo
X=(BST)malloc(sizeof(BST));
^^^
That is the call of malloc allocates a memory for a pointer to node instead of for a node itself.
There must be
X=(BST)malloc(sizeof( *X));
or
X=(BST)malloc(sizeof( struct node ));
As a result of the typo the program has undefined behavior.

Linked List- inserting a new node [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
#include<stdio.h>
#include<stdlib.h>
//Linked list implementation
typedef struct SLL{
int info;
struct SLL *link;
}Node;
Node *head=NULL;
// Node *rear=NULL;
void insert_rear(int x)
{
Node *temp=malloc(sizeof(Node));
Node *temp1=NULL;
if(temp==NULL) /* When malloc is unable to fetch Memory */
{
printf("\n Insufficient memory");
}
if(head==NULL) /* When there is no node created */
{
temp->info=x;
temp->link=head;
head=temp;
}
else
temp1=head;
while(temp1->link!=NULL)
{
temp1=temp1->link;
}
printf("\n Temp1=%d",temp1);
temp->info=x;
temp->link=NULL;
temp1->link=temp;
}
void insert_front(int x)
{
Node *temp=malloc(sizeof(Node));
if(temp==NULL) /* When malloc is unable to fetch Memory */
{
printf("\n Insufficient memory");
}
temp->info=x;
temp->link=head;
head=temp;
}
void display()
{
int i=0;
Node *temp=head;
printf("\n List Elements: \n ");
while(temp!=NULL)
{
printf(" %d) %d",++i,temp->info);
temp=temp->link;
printf("\t Link= %u \n",temp);
} printf("\n");
}
void main()
{
int x,choice,i;
printf("\n To insert at front enter 1 \n To insert at rear enter 2 \n To exit enter 4 \n");
while(choice!=4)
{
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter an ELEMENT to be inserted at FRONT \n");
scanf("%d",&x);
insert_front(x);
display();
break;
case 2: printf("Enter an ELEMENT to be inserted at LAST \n");
scanf("%d",&x);
insert_rear(x);
display();
break;
}//End of switch
}//End of while
}//End of main
I was coding this linked list program and I came up with a problem in insert_rear() function.
When I add few elements using insert_front() and then add elements at rear along with the existing Nodes using insert_rear() the programs works perfectly fine.
But when I try to add a Node without any existing Nodes using insert_rear() my program does not work for some reason.
So I took some time messing with my program and removed the following portion of code to see if I'm able to add a new node without having any existing node:
else
temp1=head;
while(temp1->link!=NULL)
{
temp1=temp1->link;
}
printf("\n Temp1=%d",temp1);
temp->info=x;
temp->link=NULL;
temp1->link=temp;
}
and it does work work, that is with only the following code I'm able to add to a new node before having any existing nodes
if(head==NULL) /* When there are no existing nodes created */
{
temp->info=x;
temp->link=head;
head=temp;
}
but along with the else condition my code does not work and program crashes.
Please help me correct this error. I have a feeling I did something stupid which I'm not unable to find.
When the list is empty and you add the first element you forget to quit and instead continue down in your function. Try something like this instead
void insert_rear(int x)
{
Node *temp=malloc(sizeof(Node));
Node *temp1=NULL;
temp->info=x;
temp->link=NULL;
if(temp==NULL) /* When malloc is unable to fetch Memory */
{
printf("\n Insufficient memory");
abort();
}
if(head==NULL) /* When there is no node created */
{
head=temp;
}
else
{
temp1=head;
while(temp1->link!=NULL)
{
temp1=temp1->link;
}
printf("\n Temp1=%d",temp1);
temp1->link=temp;
}
}
You should be careful with the use of global variables, best ist to avoid them altogether.
In the function insert_front() you change the head of the list...
Johannes

C Program to copy one binary search tree to another

So, here i have come up with Binary search tree prgram, where i am creating 2 binary trees tmp and tmp2, where i am trying to copy whole tmp2 to tmp, the node which is taken as input from user. But i am getting some segmentation fault, also i am not so sure if the logic is right.
Here is the whole program, please lemme know where is it going wrong in t_cpy() or please just fix it for me..
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *rlink;
struct node *llink;
}*tmp=NULL,*tmp2=NULL,*tmp3=NULL;
typedef struct node NODE;
NODE *create();
void inorder(NODE *);
void insert(NODE *);
void t_cpy(NODE *,NODE *);
int main()
{
int n,m;
do
{
printf("\n1.create tree 1\n2.Insert element to tree1\n3.create tree 2\n4.Insert element to tree2\n5.Inorder tree1\n6.Inorder tree2\n7.Copy tree2 to tree1\n8.exit\n\n");
printf("\nEnter ur choice: ");
scanf("%d",&m);
switch(m)
{
case 1: tmp=create();
break;
case 2: insert(tmp);
break;
case 3: tmp2=create();
break;
case 4:
insert(tmp2);
break;
case 5: printf("\n\nInorder Tree1: ");
inorder(tmp);
break;
case 6: printf("\n\nInorder Tree 2: ");
inorder(tmp2);
break;
case 7: t_cpy(tmp,tmp2);
break;
case 8: return(0);
}
}while(n!=8);
return(0);
}
void insert(NODE *root)
{
NODE *newnode;
if(root==NULL)
{
newnode=create();
root=newnode;
}
else
{
newnode=create();
while(1)
{
if(newnode->data<root->data)
{
if(root->llink==NULL)
{
root->llink=newnode;
break;
}
root=root->llink;
}
if(newnode->data>root->data)
{
if(root->rlink==NULL)
{
root->rlink=newnode;
break;
}
root=root->rlink;
}
}
}
}
NODE *create()
{
NODE *newnode;
int n;
newnode=(NODE *)malloc(sizeof(NODE));
printf("\n\nEnter the Data ");
scanf("%d",&n);
newnode->data=n;
newnode->llink=NULL;
newnode->rlink=NULL;
return(newnode);
}
void t_cpy(NODE *t1,NODE *t2)
{
int val,opt=0;
NODE *temp;
if(t1==NULL || t2==NULL)
{
printf("Can not copy !\n");
}
inorder(t1);
printf("\nEnter the node value where tree 2 should be copied\n");
scanf("%d",&val);
temp=t1;
while(temp!=NULL)
{
if(val<temp->data)
temp=temp->llink;
else
temp=temp->rlink;
}
if(temp->llink!=NULL || temp->rlink!=NULL)
printf("Not possible to copy tree to this node\n");
else
{
printf("Copy tree to \n 1.Left Node \n 2.Right Node\n Enter your choice : ");
scanf("%d",&opt);
if(opt==1)
{
temp->llink=t2;
}
else if(opt==2)
{
temp->rlink=t2;
}
else
printf("Invalid choice\n");
}
printf("Tree1 after copying is\n");
inorder(temp);
}
void inorder(NODE *tmp)
{
if(tmp!=NULL)
{
inorder(tmp->llink);
printf("%d",tmp->data);
inorder(tmp->rlink);
}
}
EDIT : Thanks to #xaxxon , who helped me with this.
Just update the while to make it work :
while(temp!=NULL&&temp->data!=val)
{
if(val<temp->data)
temp=temp->llink;
else
temp=temp->rlink;
if(temp->llink==NULL && temp->rlink==NULL && temp->data!=val)
{
printf("Invalid Node value entered !\n");
//break;
return 0;
}
and, Now it works fine for if entered value is present in the tree.
Thanks :)
Among other possible problems, you traverse temp until it is null, and on the next line you dereference it.
while(temp!=NULL)
{
if(val<temp->data)
temp=temp->llink;
else
temp=temp->rlink;
}
if(temp->llink!=NULL || temp->rlink!=NULL)
printf("Not possible to copy tree to this node\n");
You most likely mean to break out of this loop if val == temp->data, but you don't. Also, you still need to check to see if temp is null after the loop in case you didn't find val in your tree. Most likely you just meant to say:
if(temp==NULL)
printf("Not possible to copy tree to this node\n");
Also, you can't ask which side of the found node the user wants to copy a tree to. If you have a binary search tree, it has to be the side where the value should go. If you say to copy it to the right side, but all the values are less than the node, it's no longer a BST. In fact, you can't even ask where the value should go and still have a binary search tree. Each node has to be traversed from the root of the tree you want to put the other tree into to maintain the BST mechanics.
When you first use insert(tmp) the value of tmp does not change after you call insert(). Pass the address of tmp to insert(), using a *root within it instead of root.

Inorder successor of Threaded Binary Tree

I am writing a c program to create threaded binary tree and then to find INORDER SUCCESSOR of a particular node. For this, i am displaying inorder sequence for the TBT constructed and then asking user to input the node to which successor is to be displayed.. I have written function to do this. But i am not getting successor for the FIRST NODE .. Last node's successor is 0 any ways its working fine.. Can any one help me fix this ?
Here is the whole program :
#include<stdio.h>
#include <stdlib.h>
struct tbtnode {
int data;
struct tbtnode *left,*right;
int lbit,rbit,flag;
int child;
}*root=NULL;
typedef struct tbtnode TBT;
TBT *insuc(TBT *t);
void inorder(TBT *);
void create(TBT *);
void create(TBT *root)
{
int x,op,flag,y;
flag=0;
char ch;
TBT *curr=root;
TBT *q,*p;
do
{
printf("\nCurrent node %d \n\n 1.Left Direction.\n\n2.Right Direction",curr->data);
printf("\nEnter ur choice :");
scanf("%d",&op);
switch(op)
{
case 1: if(curr->lbit==1)
{
printf("Enter left child of %d : ",curr->data);
scanf("%d",&x);
q=(TBT *)malloc(sizeof(TBT));
q->data=x;
q->lbit=q->rbit=1;
q->left=curr->left;
q->right=curr;
curr->left=q;
curr->lbit=0;
q->child=0;
flag=1;
}
else
curr=curr->left;
break;
case 2: if(curr->rbit==1)
{
printf("Enter right child of %d :",curr->data);
scanf("%d",&x);
q=(TBT *)malloc(sizeof(TBT));
q->data=x;
q->lbit=q->rbit=1;
q->left=curr;
q->right=curr->right;
curr->right=q;
curr->rbit=0;
q->child=1;
flag=1;
}
else
curr=curr->right;
break;
}
}while(flag==0);
}
void inorder(TBT *head)
{
TBT *t;
t=head->left;
printf("\n");
while(t->lbit==0)
t=t->left;
while(t!=head)
{
printf(" %d",t->data);
t=insuc(t);
}
}
TBT *insuc(TBT *t)
{
if(t->rbit==0)
{
t=t->right;
while(t->lbit==0)
t=t->left;
return(t);
}
else
return(t->right);
}
void inorder_successor(TBT *head,int x)
{
TBT *t;
t=head->left;
printf("\n");
while(t->lbit==0)
t=t->left;
while(t!=head)
{
t=insuc(t);
if(t->data==x)
{
t=insuc(t);
printf(" %d",t->data);
}
}
}
int main()
{
int op,x,n,i=0,item;
char ch;
TBT *head,*root,*succ; //here head indicates dummy variable
head=(TBT *)malloc(sizeof(TBT));
head->left=head;
head->right=head;
head->lbit=1;
head->rbit=1;
do
{
printf("\n****Threaded binary tree operations****");
printf("\n1)create\n2)inorder\n3)Successor\n4)exit");
printf("\nEnter ur choice: ");
scanf("%d",&op);
switch(op)
{
case 1:
printf("\nEnter Number Of Nodes :");
scanf("%d",&n);
printf("\nEnter root data: ");
scanf("%d",&x);
root=(TBT *)malloc(sizeof(TBT));
root->data=x;
root->lbit=root->rbit=1;
root->child=0;
root->left=head->left;
head->left=root;
head->lbit=0;
root->right=head->right;
for(i=0;i<n-1;i++)
create(root);
break;
case 2:
printf("\nInorder Traversal Is:\n");
inorder(head);
break;
case 3: printf("Enter the node to which successor is to be found\n");
scanf("%d",&item);
inorder_successor(head,item);
break;
case 4:
return(0);
break;
}
}while(op<=4);
return 0;
}
please fix - inorder_successor() function for me..
Thank you
You have many problems with your code. To start with you never check for NULL pointers. To continue, you have both a global and a local variable in main called root.
The last thing means that when you allocate memory for root in main, you allocate for the local variable, and the global variable will still be NULL.
There are also other things that look weird, like you assigning the left and right pointer of head to itself.
I think you need to lay it all out on paper first, both how you have it now and how you want it to be. It will help you to better visualize the tree.

Why my doubly linked list's C implementation creating duplicate values?

I coded for doubly linked list implementation in C. In that, after making insertion of values, i am getting duplication of values. i.e. the last value given by me duplicated in all list items.
My code is as follows
header.h
#include<stdio.h>
#include<stdlib.h>
typedef struct doubly_list
{
int id;
char *name;
struct doubly_list *next;
struct doubly_list *prev;
}node;
void insertfirst(node **,int ,char *);
void insertlast(node **,int ,char *);
doubly_list_insert.c
#include"header.h"
void insertfirst(node **head,int id,char *name)
{
node *tmp=(node *)malloc(sizeof(node));
if(NULL == tmp)
{
printf("\nMemory allocation failed\n");
exit(1);
}
tmp->id=id;
tmp->name=name;
tmp->prev=NULL;
if(*head== NULL)
{
tmp->next=NULL;
*head=tmp;
}
else
{
tmp->next=*head;
(*head)->prev=tmp;
*head=tmp;
}
}
void insertlast(node **head,int id,char *name)
{
if(*head==NULL)
{
insertfirst(head,id,name);
return;
}
node *last=*head;
node *tmp=(node *)malloc(sizeof(node));
if(NULL == tmp)
{
printf("\nMemory allocation failed\n");
exit(1);
}
tmp->id=id;
tmp->name=name;
tmp->next=NULL;
while(last->next!=NULL)
{
last=last->next;
}
last->next=tmp;
tmp->prev=last;
}
doubly_list_traverse.c
#include"header.h"
void traverse(node *head)
{
node *tmp=head;
if(head==NULL)
{
printf("\nList is empty\n");
exit(1);
}
while(tmp!=NULL)
{
printf("%d --> %s\n",tmp->id,tmp->name);
tmp=tmp->next;
}
}
And, here comes the main file,
main.c
#include"header.h"
int main()
{
int choice;
int id;
char name[15];
node *root=NULL;
system("clear");
while(1)
{
printf("\n1.Insert First\n");
printf("\n2.Insert Last\n");
printf("\n3.Traverse\n");
printf("\n4.Exit\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the employee id : ");
scanf("%d",&id);
printf("\nEnter the employee name : ");
scanf("%s",name);
insertfirst(&root,id,name);
break;
case 2:
printf("\nEnter the employee id : ");
scanf("%d",&id);
printf("\nEnter the employee name : ");
scanf("%s",name);
insertlast(&root,id,name);
break;
case 3:
traverse(root);
break;
case 4:
return 0;
break;
default:
printf("\nPlease enter valid choices\n");
}
}
}
During execution its getting input from me properly,if i insert only one data either first or last.
But if i insert a second one, there comes the problem.
In my case, the id value remains the same. But the 2nd input's name value is duplicated in 1st value.
Why this is happening? Is it anything wrong in passing arguments?
When you create a new node, you set the node name by just copying the pointer to the name. You have to copy the string not the pointer. The strdup function is perfect for this:
tmp->name=strdup(name);
Remember to free the name when you free the nodes.
Edit
What happens when you call insertfirst the first time, is that the name field of the first node points to the name array in main. When you fetch the name for the second node, the contents of the array in main is updated with the new name, and since the pointer in the first node points to that array it seems like the name is duplicated.

Resources