I wanna write a program about linked list that gets a number and if the number was equal to the one of the nodes' data , gives the number of that node .
like the datas in 3 nodes are
123
56
78
and it gets a number like 56 and its equal to the second node's data so the output should be 2.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
node *next;
};
node* cnode();
void find(node *first,int i,int d);
int main()
{
node *first,*last;
int n,i,x,d;
printf(" How many nodes ?\t");
scanf("%d",&x);
for(i=1;i<=x;i++){
last=cnode();
last->next=first;
first=last;
}
printf("\n enter a particular data:\t");
scanf("%d",&d);
printf("\n number of the particular node:\t");
find(first,i,d);
}
void find(node *first,int i,int d){
int count=0;
while (first != NULL)
{
if (first->data == d)
count++;
first = first->next;
}
if(count != 0){
printf("%d",count);
}
if(count == 0){
printf("\n NOT FOUND ! ");
}
}
According to the C Standard the function main without parameters shall be declared like
int main( void )
Secondly this declaration of the structure
struct node{
int data;
node *next;
};
is not a valid C declaration.
You should write
struct node{
int data;
struct node *next;
};
typedef struct node node;
or
typedef struct node{
int data;
struct node *next;
} node;
You have to initialize at least node first with NULL. Otherwise the program will have undefined behavior.
node *first = NULL,*last;
The parameter i is not used within the function find. So it may be removed.
void find(node *first, int d);
The function definition can look at least like
void find( node *first, int d )
{
int count = 0;
while ( first != NULL && first->data != d )
{
first = first->next;
++count;
}
if ( first != NULL )
{
printf("%d",count);
}
else
{
printf("\n NOT FOUND ! ");
}
}
Related
I have to create a binary search tree by inserting in every node data about pharmaceutical drug . The nodes are sorted by a char NAME .I've got an error , a segmentation fault error at strcmp(name,(*parent)->name) < 0 .Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct date
{
int dd,mm,yyyy;
};
typedef struct node
{
char name[50];
float price;
int amount;
struct date date_rel;
struct date date_exp;
struct node *left , *right;
} node;
node *insert_node(node **parent,char name[])
{
node *current = NULL;
if(*parent == NULL)
{
current = (node *) malloc(sizeof(node));
strcpy(current->name,name);
printf("\nPrice =");
scanf("%f",¤t->price);
printf("\nAmount =");
scanf("%d",¤t->amount);
printf("\nRelease date =");
scanf("%d.%d.%d",¤t->date_rel.dd,¤t->date_rel.mm,¤t->date_rel.yyyy);
printf("\nExpiration date =");
scanf("%d.%d.%d",¤t->date_exp.dd,¤t->date_exp.mm,¤t->date_exp.yyyy);
*parent = current;
}
else
{
if(strcmp(name,(*parent)->name) < 0)
{
(*parent)->left = insert_node(&(*parent)->left,name);
}
else
{
(*parent)->right = insert_node(&(*parent)->right,name);
}
}
return *parent;
}
int main()
{
node *root = NULL;
char name[50] ;
int total_prod;
printf("The total number of medicines is:");
scanf("%d",&total_prod);
while(total_prod != 0)
{
printf("Name =");
scanf("%s",name);
insert_node(&root,name);
total_prod--;
}
return 0;
}
Most likely it's because you don't initialize the left and right pointers when *parent is NULL. As they are uninitialized they will have an indeterminate value, and in reality it will be seemingly random, and most likely not equal to NULL which leads you to use them as valid pointers and you will have undefined behavior and get the crash.
Before the *parent = current assignment you need to initialize the left and right pointers of current to NULL.
#include<stdio.h>
#include<stdlib.h>
struct list
{
int a;
int b;
int c;
struct list *next;
};
struct list* addlistele(struct list*,int,int,int);
/* List c element */
void listc()
{
printf(" soon...\n");
}
void printlist(list)
{
struct list* temp;
temp=list;
while(temp!=NULL)
{
printf("a:%d,b;%d,c:%d\n",temp->a,temp->b,temp->c);
temp=temp->next;
}
}
/* List element */
struct list* addlistele(struct list* listadd,int b,int d,int m)
{
int i;
struct list* temp;
struct list* addelement=(struct list*)malloc(sizeof(struct list));
addelement->a=b;
addelement->b=d;
addelement->c=m;
addelement->next=NULL;
if(listadd==NULL)
{
printf("entering");
return addelement;
}
else
{
temp=listadd;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=addelement;
}
return listadd;
}
int main()
{
int ch,i,a,b,c;
struct list *element,*list;
element=(struct list*)malloc(sizeof(struct list));
printf("Choose any one of the option \n");
printf("1.List All \n 2.List c \n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter values \n");
for(i=0;i<2;i++)
{
scanf("%d %d %d \n",&a,&b,&c);
list=addlistele(element,a,b,c);
}
printlist(list);
break;
case 2:listc(); break;
default:break;
}
}
Hi all, i have written the code like the above one.In that when i gave inputs
> Choose any one of the option
> 1.List All
> 2.List c 1 Enter values 2 3 4 1 2 3
The output is
a:0,b;0,c:0
a:2,b;3,c:4
a:1,b;2,c:3
and also it is not adding the element first i mean it is not entering into this loop
if(listadd==NULL)
{
printf("entering");
return addelement;
}
how to make the head element to be NULL and also i don't know how 0 is coming first.Could anybody can tell me what will be the issue?
In main() you create head element and pass it to the addlistele() function.
element=(struct list*)malloc(sizeof(struct list));
...
list=addlistele(element,a,b,c);
You are seeing this first element which does not have valid values that you expected.
Solution would be you malloc() the element in the function rather than in main() and do not allocate element in main().
replace
struct list *element,*list;
element=(struct list*)malloc(sizeof(struct list));
with
struct list *list=NULL;
then
replace
list=addlistele(element,a,b,c);
with
list=addlistele(list,a,b,c);
Also
replace
void printlist(list)
with
void printlist(struct list *list)
and
replace scanf("%d %d %d \n",&a,&b,&c); with scanf("%d %d %d",&a,&b,&c);
This exercise is not really new to me, but it still is quite advance for me. Being a student, I would like to kindly ask for your help regarding this matter.
I need to understand, not just know, how AVL node deletion works. I've been given this C code that I have to complete to demonstrate how AVL insertion and deletion happens. I've done the basics. It's just the balancing and the rotation that got me dizzy.
Here's the main C file:
#include<stdio.h>
#include<stdlib.h>
#include "avl.h"
#define N 10
#define BALANCED 0
#define LEFT_LEANING 1
#define RIGHT_LEANING 2
int main(){
AVL *root=NULL;
int choice=0,x;
while((choice=menu())!=3){
printf("Value: ");
scanf("%i",&x);
switch(choice){
case 1: insert_value(&root,x);
view(root,2);
break;
case 2: delete_value(&root,x);
view(root,2);
}
}
}
here's the avl header file:
#define N 10
#define BALANCED 0
#define LEFT_LEANING 1
#define RIGHT_LEANING 2
//structure for an avl_node
typedef struct node_tag{
int x, height;
struct node_tag *parent;
struct node_tag *left;
struct node_tag *right;
}avl_node;
//print menu and get user choice
int menu(){
int choice;
printf("\n MENU ");
printf("\n[1] - Insert");
printf("\n[2] - Delete");
printf("\n[3] - Exit");
printf("\nYour choice: ");
scanf("%i",&choice);
return choice;
}
//find maximum of 2 numbers
int max(int a,int b){
return(a>b?a:b);
}
//finds the minimum value of the BST
avl_node* minimum(avl_node *rootptr){
if(rootptr!=NULL){
while(rootptr->left!=NULL) rootptr=rootptr->left;
}
return (rootptr);
}
//update height of a given node
void updateheight(avl_node *temp){
if(temp!=NULL)
temp->height = max(temp->left==NULL?-1:temp->left->height,temp->right==NULL?-1:temp->right->height)+1;
}
//left rotate the subtree
void left_rotate(avl_node **rootptr){
}
//right rotate the subtree
void right_rotate(avl_node **rootptr){
}
//determine rotation of node/s
void insert_fixup(avl_node **rootptr, avl_node *temp){
}
//insert value and create node
void insert_value(avl_node **rootptr, int x){
avl_node *temp;
temp = (avl_node *)malloc(sizeof(avl_node));
temp->x = x;
temp->height = 0;
temp->parent = temp->left = temp->right = NULL;
insert_node(rootptr,temp);
insert_fixup(rootptr,temp);
}
void view(avl_node *root, int tabs){
int i;
if(root != NULL){
view(root->right,tabs + 1);
for(i=0;i<tabs;i++) printf("\t");
printf("%2i\n",root->x);
view(root->left,tabs+1);
}
}//view the AVL
void swap(int *a, int *b){
int temp;
temp = *a; *a = *b; *b = temp;
}//swap values
//look for the successor of a node
avl_node* successor(avl_node *rootptr){
if(rootptr==NULL) return (rootptr);
else if(rootptr->right!=NULL) return (minimum(rootptr->right));
while(rootptr->parent!=NULL){
if(rootptr==rootptr->parent->right) rootptr=rootptr->parent;
else break;
}
return (rootptr->parent);
}
//function for searching the location of the node
avl_node* search(avl_node *root,int x){
if(root==NULL || root->x==x) return root;
else{
if(root->x>x) return(search(root->left,x));
else return(search(root->right,x));
}
}
//function for searching the value of the node
int search_node(avl_node *root, int x){
if(root==NULL) return 0;
if(root->x==x) return 1;
else{
if(root->x>x) return(search_node(root->left,x));
else return(search_node(root->right,x));
}
}
//function for deleting a node
void delete_value(avl_node** root,int x){
}
To understand, You should go through the Wiki Page and also try to understand pseudo code to understand node insertion and deletion in AVL tree. Also you can go through Presentation PPT to understand AVL tree. And to find status of your node from tree you can use search() function.
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;
}
Here is my program which creates a link list and also reverses it.
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *list=NULL;
struct node *root=NULL;
static int count=0;
struct node *create_node(int);//function to create node
void travel_list(void);
void create_list(int);
void reverse_list(void);
int main()
{
int i, j, choice;
printf("Enter a number this will be root of tree\n");
scanf("%d", &i);
create_list(i);
printf("Enter 1 to enter more numbers \n 0 to quit\n");
scanf("%d", &choice);
while (choice!=0){
printf("Enter a no for link list\n");
scanf("%d",&i);
// printf("going to create list in while\n");
create_list(i);
travel_list();
printf("Enter 1 to enter more numbers \n 0 to quit\n");
scanf("%d", &choice);
}
printf("reversing list\n");
reverse_list();
travel_list();
}
// end of function main
void create_list (int data)
{
struct node *t1,*t2;
//printf("in function create_list\n");
t1=create_node(data);
t2=list;
if( count!=0)
{
while(t2->next!=NULL)
{
t2=t2->next;
}
t2->next=t1;
count++;
}
else
{
root=t1;
list=t1;
count++;
}
}
struct node *create_node(int data)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp->data=data;
temp->next=NULL;
// printf("create node temp->data=%d\n",temp->data);
// printf("the adress of node created %p\n",temp);
return temp;
}
void travel_list(void )
{
struct node *t1;
t1=list;
printf("in travel list\n");
while(t1!=NULL)
{
printf("%d-->",t1->data);
t1=t1->next;
}
printf("\n");
}
void reverse_list(void)
{
struct node *t1,*t2,*t3;
t1=list;
t2=list->next;
t3=list->next->next;
int reverse=0;
if(reverse==0)
{
t1->next=NULL;
t2->next=t1;
t1=t2;
t2=t3;
t3=t3->next;
reverse++;
}
while(t3!=NULL)
{
t2->next=t1;
t1=t2;
t2=t3;
list=t1;
travel_list();
t3=t3->next;
}
t2->next=t1;
list=t2;
}
Above is a fully working code.
I want to know if there can be further enhancement to the above code?
Make your indentation and whitespace usage consistent
Use meaningful identifiers rather than t1, t2 and t3
Make the data member a generic type, for example void * rather than int.
Don't use global variables, pass struct node * pointers to your functions.