Linked list basic advice for beginner is needed - c

Teacher said write program with four functions (print size or add, remove and print node). In line 17 error comes (Cannot convert int* into node). I can't find other way to represent this line so please help. Since it's my first experience with linked list, you can expect tons of error.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node{ // Declaring the node (2 way)
int n;
struct node *next;
struct node *prev;
};
int *header; //Declaring header and size counter
int size;
int *finder(int number) //Function that return address of selected node
{
int *adr;
int c;
struct node *temp;
temp=header;
c=0;
while(c==number)
{
temp=temp->next;
c=c+1;
}
return adr;
}
void insert(int data,int number) //insert new node after specified node
{
int *adr;
adr=finder(number);
struct node *current;
struct node *previous;
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
current=adr;
previous=current->prev;
temp->n=data;
temp->next=current;
temp->prev=current->prev;
previous->next=temp;
current->prev=temp;
size=size+1;
}
void remove(int number) //remove node after selected one
{
int *adr;
adr=finder(number);
struct node *current;
struct node *previous;
struct node *neeext;
current=adr;
previous= current->prev;
neeext= current->next;
previous->next= neeext;
neeext->prev= previous;
}
void print(int number) //print data of node
{
int *adr;
adr=finder(number);
struct node *current;
printf("%d",current->n);
}
int main() //main function
{
int i,j,d;
size=0;
for(i=1;i<5;i=i+0)
{
if(i==1)
{
printf("%d",size);
}
scanf("%d",&j);
if(i==2)
{
scanf("%d",&d);
insert(d,j);
printf("inserted");
}
if(i==3)
{
remove(j);
printf("removed");
}
if(i==4)
{
print(j);
}
if(i==5)
{
return 0;
}
scanf("%d",&i);
printf("\n");
}
}

You should declare your header variable as struct node *header;, not as int*

Related

Structure, pointer and binary search tree in c

The below code is able to add only one element to tree.
Please check what is wrong with this code.
I tried creating a binary search tree with 8 nodes with values taken from array, and when I print the tree only the first element is printed i.e the first element of array.
May be I am not good with working of structure and pointer. Can you say more about pointers and structure in c.
I thank for your help.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *left;
struct node *right;
};
struct node *root=NULL;
void add(struct node**,struct node*);
void createtree(struct node **root){
int arr[8]={20,45,17,56,40,32,25,48};
int size=sizeof(arr)/sizeof(int);
struct node *temp=NULL;
for(int i=0;i<size;i++)
{
temp=(struct node *)malloc(sizeof(
struct node));
temp->data=arr[i];
temp->left=NULL;
temp->right=NULL;
add(root,temp);
}
}
void add(struct node **root,struct node *temp){
if(*root==NULL){
*root=temp;
return;
}
struct node *curr=*root;
if(temp->data< curr->data){
struct node *temper=curr->left;
add(&temper,temp);
}
else
{
struct node *temper=curr->right;
add(&temper,temp);
}
}
void print(struct node *root){
if(root!=NULL){
print(root->left);
printf("%d ",root->data);
print(root->right);
}
}
int main(){
createtree(&root);
print(root);
return 0;
}

Problem in linked list while using struct variable

Code for linked list using memory allocation:
#include<stdio.h>
struct node
{
int val;
struct node *point;
};
int main()
{
struct node *head;
head=(struct node* ) malloc(sizeof(struct node ));
void create(struct node* start);
void print(struct node* start);
create(head);
print(head);
return 0;
}
void create(struct node* start)
{
int test,a=1;
struct node* cur;
cur=start;
while(scanf("%d",&test)!=EOF)
{ if(a!=1)
{
cur=cur->point;
}
cur->point=(struct node* ) malloc(sizeof(struct node ));
cur->val=test;
a++;
printf("---------------");
printf("%d\n",cur);
printf("%d\n",cur->val);
printf("%d\n",cur->point);
printf("%d\n",a);
printf("---------------");
}
cur->point=NULL;
return;
}
void print(struct node* start)
{
struct node* cur;
cur=start;
while(cur!=NULL)
{
printf("%d ",cur->val);
cur=cur->point;
}
return;
}
Code for linked list using struct variable:
#include<stdio.h>
struct node
{
int val;
struct node *point;
};
int main()
{
struct node *head,link;
head=&link;
void create(struct node* start);
void print(struct node* start);
create(head);
print(head);
return 0;
}
void create(struct node* start)
{
int test,a=1;
struct node* cur;
cur=start;
while(scanf("%d",&test)!=EOF)
{ if(a!=1)
{
cur=cur->point;
}
struct node link;
cur->point=&link;
cur->val=test;
a++;
printf("---------------");
printf("%d\n",cur);
printf("%d\n",cur->val);
printf("%d\n",cur->point);
printf("%d\n",&link);
printf("%d\n",a);
printf("---------------");
}
cur->point=NULL;
return;
}
void print(struct node* start)
{
struct node* cur;
cur=start;
while(cur!=NULL)
{
printf("%d ",cur->val);
cur=cur->point;
}
return;
}
When I use 2nd code it doesn't work correctly. It prints just two values.
In while loop of create() I use struct node type variable declaration.
That's why it should it would declare a new struct node type link variable and a new memory (struct node link) address should be seen in every execution.
So what's the problem?
I am a newbie learning data structure using C. Please explain in simple language.

Program crashes when inserting a node in a binary tree

I created the following library to insert,delete,search and print nodes in a binary tree.
#include <stdlib.h>
struct NODE
{
int code;
char subject[20];
struct NODE *left;
struct NODE *right;
};
void InOrder(struct NODE *R)
{
if (R==NULL)
return;
InOrder(R->left);
printf("%d %s\n",R->code,R->subject);
InOrder(R->right);
}
void PreOrder(struct NODE *R)
{
if (R==NULL)
return;
printf("%d %s\n",R->code,R->subject);
InOrder(R->left);
InOrder(R->right);
}
void PostOrder(struct NODE *R)
{
if (R==NULL)
return;
InOrder(R->left);
InOrder(R->right);
printf("%d %s\n",R->code,R->subject);
}
struct NODE *Search(struct NODE *R,int CODE,struct NODE **father)
{
if(R==NULL)
return NULL;
if(R->code==CODE)
{
*father=R;
return R;
}
if (CODE<R->code)
return Search(R->left,CODE,father);
else
return Search(R->right,CODE,father);
}
struct NODE * CreateNode(struct NODE T)
{
struct NODE *tmp;
tmp=(struct NODE *)malloc(sizeof(T));
*tmp=T;
tmp->left=tmp->right=NULL;
return tmp;
}
int Insert(struct NODE **R,struct NODE ND)
{
struct NODE *cur,*fath=NULL;
cur=Search(*R,ND.code,&fath);
if (cur)
return 0;
cur=CreateNode(ND);
if(fath==NULL)
*R=cur;
else
if(fath->code>ND.code)
fath->left=cur;
else
fath->right=cur;
return 1;
}
struct NODE *MinOfMax (struct NODE *ND)
{
struct NODE *tmp;
if (ND==NULL)
return NULL;
if(ND->right==NULL)
return NULL;
tmp=ND->right;
while(tmp->left!=NULL)
tmp=tmp->left;
return tmp;
}
struct NODE* Delete(struct NODE *R, int code)
{
if (R==NULL)
return R;
if (code<R->code)
R->left=Delete(R->left,code);
else if (code>R->code)
R->right=Delete(R->right,code);
else
{
if (R->left==NULL)
{
struct NODE *temp=R->right;
free(R);
return temp;
}
else if (R->right==NULL)
{
struct NODE *temp=R->left;
free(R);
return temp;
}
struct NODE *temp=MinOfMax(R->right);
R->code=temp->code;
R->right=Delete(R->right,temp->code);
}
return R;
}
When i try to insert a node in the binary tree,the program crashes.Here is my main:
int main(int argc,char* argv[])
{
typedef struct NODE NODE;
NODE *root=NULL;
NODE tmp;
Insert(&root,tmp);
return 0;
}
I tried to assign static values (for example code=100 and subject="Physics") but still the program crashes.Should i malloc something,change anything in my header file or do something entirely different?I'm stuck here for hours without finding any solution.Most insert functions out there assume that i only have one integer as data in the node,but i need to pass the entire node.
Your code basically does nothing. It seems you copy-pasted it from somewhere. I tried to figure it out and here's a code example. Basically you've to initializate a new node in the main when you try to insert it.
Note that's just an example, i didn't a full test.
int main(int argc,char* argv[])
{
typedef struct NODE NODE;
NODE *root=NULL;
NODE *tmp = malloc(sizeof(struct NODE));
tmp->code = 1; /*Just a number*/
strcpy(tmp->subject,"prova"); /*Put something in it*/
Insert(&root,*tmp); /* Try to insert it*/
PreOrder(root); /*Try to see if it has been inserted*/
return 0;
}
Your tmp node, which is going to be the newly inserted node is used uninitialized in your main(). Your compiler could have warned you for this, if you had used -Wall flag.
So let's take a look in your insert function:
int Insert(struct NODE **R, struct NODE ND)
{
struct NODE *cur,*fath=NULL;
cur = Search(*R, ND.code, &fath); // ND.code is junk, since ND is uninitialized
...
return 1;
}
which likely causes the segmentation fault.
root is too, you could initialize it to NULL in main().
Not the cause of your problem, but Do I cast the result of malloc? No.

Typedef's and nodes

This code is supposed to build a simple linked list containing integers from 0 to 20. I keep getting the error: unknown type name 'node' when I compile my code for what seems every instance of node in the program. I'm not sure if I have to define them or there is a bigger flaw in the code.
#include<stdio.h>
#include<stdlib.h>
int main() {
int i;
struct node *first=NULL;
for(i=1;i<=20;i++)
first=insertrear(first,i);
dispnodes(first);
sum(first);
return 0;
}
typedef struct node {
int data;
struct node *link;
};
node* getnode(node *temp,int i) {
temp=(node*)malloc(sizeof(node));
temp->data=i;
temp->link=NULL;
return temp;
}
node* insertrear(node *first,int a) {
node *temp=NULL,*i;
temp= getnode(temp,a);
if(first==NULL) {
first=temp;
return first;
}
for(i=first;i->link!=NULL;i=i->link);
i->link=temp;
return first;
}
void dispnodes(node *first) {
int j;
if(first==NULL) {
printf("\nlist empty");
return;
}
node *i;
for(i=first,j=0;i!=NULL;j++,i=i->link)
printf("\nNode #%d contains %d ",j,i->data);
}
void sum(node *first) {
node *i;
int total=0;
for(i=first;i!=NULL;i=i->link)
total+=i->data;
printf("\nThe sum total of all nodes in this list is %d",total);
}
You either need to specify a label for the typedef struct node or not typedef the struct node.
The way you are using it, you can do following.
/* Forward declare typedef _node to node */
typedef struct node_ node;
/* Define struct node_ */
struct node_ {
int data;
node *link;
};
You should have the forward declaration above your main(), so that you can use it there as well.
typedef struct node {
int data;
struct node *link;
}node;
int main() {
int i;
struct node *first=NULL;
for(i=1;i<=20;i++)
first=insertrear(first,i);
dispnodes(first);
sum(first);
return 0;
}
There are two things you have to modify:
you have to declare the the struct ,typedef and all the functions you are calling before main function.Compiler should be provided with the declaration before it encounters any of them.
Also you haven't provided a label to the typedef to structure node,you can do something like this:
typedef struct node {
int data;
struct node *link;
} Node ;
or use struct node instead of node to declare a variable of struct node type
Here is the code after modification:
typedef struct node
{
int data;
struct node *link;
} Node ;
void dispnodes(Node *first);
node* insertrear(Node *first,int a);
void sum(Node *first);
int main() {
int i;
struct node *first=NULL;
for(i=1;i<=20;i++)
first=insertrear(first,i);
dispnodes(first);
sum(first);
return 0;
}
// rest of the code

program to find the largest element in stack while implementing linked list but the data is not being accesed

#include<stdio.h>
#include<stdlib.h>
//structure//
struct node
{
int data;
struct node *link;
};
//function to push elements into stack//
void push(struct node *top_ref,int x)
{
struct node *new=(struct node *)malloc(sizeof(struct node));
new->data=x;
new->link=top_ref;
top_ref=new;
}
//function to get largest element//
int largest(struct node *top_ref)
{
int temp=0;
while(top_ref!=NULL)
{
if(temp<top_ref->data)
{
temp=top_ref->data;
}
top_ref=top_ref->link;
}
return temp;
}
//function to print the stack//
void print(struct node *top_ref)
{
while(top_ref!=NULL)
{
printf("%d->",top_ref->data);
top_ref=top_ref->link;
}
printf("\n");
}
//main function//
int main()
{
struct node *stack=(struct node *)malloc(sizeof(struct node));
stack=NULL;
push(stack,1);
push(stack,5);
push(stack,6);
push(stack,3);
print(stack);
printf("the largest no. is %d\n",largest(stack));
}
trying to find the largest no. in a stack.
there is a problem in line 20, 32 top_ref->data is not being accesed
top_ref is the last node of the linked list
new to linked list and data structures
the output i am getting is
empty line
the largest no. is 0
The point here is stack is a pointer to another pointer so should be taken as a double pointer in push function.
#include<stdio.h>
#include<stdlib.h>
//structure//
struct node
{
int data;
struct node *link;
};
//function to push elements into stack//
void push(struct node **top_ref,int x)
{
struct node * tmp=(struct node *)malloc(sizeof(struct node));
tmp->data=x;
tmp->link=NULL;
if(top_ref==NULL){
*top_ref=tmp;
}
else{
tmp->link= *top_ref;
*top_ref=tmp;
}
}
//function to get largest element//
int largest(struct node *top_ref)
{
int temp=0;
while(top_ref!=NULL)
{
if(temp<top_ref->data)
{
temp=top_ref->data;
}
top_ref=top_ref->link;
}
return temp;
}
//function to print the stack//
void print(struct node *top_ref)
{
while(top_ref!=NULL)
{
printf("%d->",top_ref->data);
top_ref=top_ref->link;
}
printf("\n");
}
//main function//
int main()
{
struct node *stack;
stack=NULL;
//stack=NULL;
push(&stack,1);
push(&stack,5);
push(&stack,6);
push(&stack,3);
print(stack);
printf("the largest no. is %d\n",largest(stack));
}

Resources