Typedef's and nodes - c

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

Related

Typedef vs tructure tag

I'm learning link list. I create structure called Node to store the data and link to the next node. I have two doubts.
I dont understand why we are using the structure we are creating inside the same strucutre as element to store link. Is looks like a nested structure, will the element likn will have link.data and link.link in it? It is confusing
struct Node{
int data;
struct Node* link;
}node;
I created a typedef for the structure Node as node, so that I can declare variables and use as datatype inside the code. But it gives me error. I'm missing some understanding here.
If I use struct Node representation throught my code, it works fine. If I change it to node, i see lot of error. (except when I use it inside sizeof function)
My working code:
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node* link;
}node;
struct Node head;
void Insert(int X);
void Print();
int main(){
head = NULL;
printf("How laby elements you need?\n");
int n,i,x;
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the elements:\n");
scanf("%d",&x);
Insert(x);
Print();
}
}
void Insert(int x)
{
struct Node* temp = (struct Node*)malloc(sizeof(node));
temp->data = x;
temp->link = head;
head = temp;
}
void Print(){
struct Node* temp = head;
while(temp != NULL)
{
printf("The list is: %d\n",temp->data);
temp = temp->link;
}
}
My not working code:
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node* link;
}node;
node* head; // Error
void Insert(int X);
void Print();
int main(){
head = NULL;
printf("How laby elements you need?\n");
int n,i,x;
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the elements:\n");
scanf("%d",&x);
Insert(x);
Print();
}
}
void Insert(int x)
{
node* temp = (node*)malloc(sizeof(node)); // Error
temp->data = x;
temp->link = head;
head = temp;
}
void Print(){
node* temp = head; // Error
while(temp != NULL)
{
printf("The list is: %d\n",temp->data);
temp = temp->link;
}
}
I dont understand why we are using the structure we are creating inside the same strucutre as element to store link. Is looks like a nested structure
The struct Node structure doesn't contain itself. That would be impossible, as it would have infinite size.
Rather, it contains a struct Node * pointer. Specifically, this pointer will be used to locate the next node in the list. But that doesn't make the next node part of the current node.
I created a typedef for the structure Node as node
No, you didn't. There's no typedef in the code you posted.
struct Node {
int data;
struct Node* link;
} node;
is short for
struct Node {
int data;
struct Node* link;
}
struct Node node;
This creates a variable of type struct Node called node.
typedef struct Node {
int data;
struct Node* link;
} node;
is short for
struct Node {
int data;
struct Node* link;
};
typedef struct Node node;
This creates a type called node.
Note that I prefer to use lowercase for variables, and CamelCase for types, so I wouldn't use node for a type; I'd use Node instead.
typedef struct Node {
int data;
struct Node* link;
} Node;
I dont understand why we are using the structure we are creating
inside the same strucutre as element to store link. Is looks like a
nested structure, will the element likn will have link.data and
link.link in it? It is confusing
In this declaration
struct Node{
int data;
struct Node* link;
};
the data member link does not have the type struct Node. It has the pointer type struct Node *. So there is no nested structures. Each node of the list stores a pointer to the next node.
You forgot to place the typedef specifier in this declaration
struct Node{
int data;
struct Node* link;
}node;
As a result the name node in the above declaration means an object of the type struct Node. So the compiler issues an error for the following declaration
node* head;
and other usages of the name node in the program because node in this case is an identifier of an object instead of a type specifier.
You have to write
typedef struct Node{
int data;
struct Node* link;
}node;
node* head;
In this case the name node denotes a type specifier that is a typedef name for the type specifier struct Node..
This statement in main
head = NULL;
is redundant. The pointer head was already initialized as a null pointer in its declaration in the file scope because this declaration
node* head;
is equivalent to
node* head = NULL;
Pay attention to that it is not a good idea when functions depend on global variables as in your program on the pointer head declared in the file scope.
Do not forget at least to write a function that will free all the allocated memory for the list.

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;
}

My C program doesn`t work unless I add a printf before everything else

I tried to run the code and it terminates and doesn`t print anything, but if I include a printf statement as the first line in the main, it works. Why?
#include <stdio.h>
#include <stdlib.h>
typedef struct node TNode;
typedef struct list TList;
struct list{
TNode *node;
};
struct node{
int data;
TNode *next;
};
TList* buildList(){
TList *list;
list = (TList*)malloc(sizeof(TList));
list->node->next = NULL;
printf("\nList was built successfully\n");
return list;
}
int main(){
TList *myList = buildList();
myList->node->data = 5;
printf("\nData: %d\n", myList->node->data);
return 0;
}
You are accessing list->node->next without having initialized list->node. Allocate memory for the node and assign a pointer to it to list->node.
#include <stdio.h>
#include <stdlib.h>
typedef struct node TNode;
typedef struct list TList;
struct list{
TNode *node;
};
struct node{
int data;
TNode *next;
};
TList* buildList(){
TList *list;
list = (TList*)malloc(sizeof(TList));
list->node = (TNode*)malloc(sizeof(TNode)); // <---
list->node->next = NULL;
printf("\nList was built successfully\n");
return list;
}
int main(){
TList *myList = buildList();
myList->node->data = 5;
printf("\nData: %d\n", myList->node->data);
return 0;
}

adding to linked list generically nullptr issue

I am trying to add to a linked list generically, because i have 2 linked lists and I call the same function for the two lists.
void addToList(void* (*createNode)(void *data,char* name), int (compare)(void *a1, void *a2), void *(*getNext)(void), void(*setNext)(void *node, void* data), void *data,char* name, void **list) {
void *node = createNode(data,name);
void *head = *list;
if (head == NULL) { // if list is empty
*list = node;
}
else if (compare(head, node)) { // if first element is greater than node to add (meaning we need to add node to head of list)
setNext(node, list);
*list = node;
}
else {
// node will be inserted someplace other than head
void *current = head;
while (getNext(current) != NULL && compare(node, getNext(current))) {
// loop until we are at the end of the list OR until we have found an element greater than us
// basically, current will be the place to put the new node in
current = getNext(current);
}
setNext(node, getNext(current));
setNext(current, node);
}
Here is the struct and list and node
struct Stud {//my struct contains 2 lists
int id;
float gradeAverage;
float incomeAverage;
struct gradeList *gradelist;
struct incomeList *incomelist;
};
typedef struct Stud Students;
struct gradeNode {//the grade node with what is inside it
char courseName[20];
int grade;
struct gradeNode *next;
struct gradeNode *prev;
};
struct gradeList {//my first list with head and tail
struct gradeNode *head;
struct gradeNode *tail;
int size;
};
struct incomeNode {//my secound node
char *workplace;
float income;
struct incomeNode *next;
struct incomeNode *prev;
};
struct incomeList {//the secount list
struct incomeNode *head;
struct incomeNode *tail;
int size;
};
Here are the functions that I used
void* get_next_Income(void* head) {//used above in the generic call
return((struct incomeNode *)head)->next;
}
void* get_next_Grade(void* head) {//used above to get the income
return(((struct gradeNode *)head)->next);
}
int compareGradeNode(void *a1, void *a2) {//compares the node iam adding to the exicting node and adding accordingly
return ((struct gradeNode*)a1)->grade > ((struct gradeNode*)a2)->grade;
}
int compareIncomeNode(void *a1, void *a2) {
return ((struct incomeNode*)a1)->income>((struct incomeNode*)a2)-
>income;
}
void setNextGradeNode(void* node, void* data) {//seting the next graded
((struct gradeNode*)node)->next = data;
}
void setNextIncomeNode(void* node, void* data) {//setting the next income
((struct incomeNode*)node)->next = data;
}
Students students[30];
int numOfStuds = 0;
void* createGradeNode(void* data,char*name) {
struct gradeNode* nd=(struct GradeNode*)malloc(sizeof(struct gradeNode));
strcpy(nd->courseName, name);
assert(nd != NULL);
nd->grade = data;//setting the new node to the data the user entered!
nd->next = NULL;
return nd;//and return it
}
The problem is that I got a null ptr in the code part below.
I am using visual studio and can't solve it.
Please help.
Sorry for the long code, I am new to c.
void *head = *list;
if (head == NULL) { // if list is empty
*list = node;

Linked list basic advice for beginner is needed

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*

Resources