error in creating array of linked list - c

I have to put nodes of binary search tree of every level in a linked list. That is if the height of the tree is 'h' then 'h+1' linked lists would be created and then each linked list would have all the nodes of each level. For this I have thought of creating an array of linked list. But the nodes are not being inserted in the list I guess. The code is as follows:-
struct node{
int data;
struct node *left;
struct node *right;
};
struct linked_list
{
int data;
struct linked_list *next;
};
linkedlistofbst(struct node *new,struct linked_list *n1[], int level)
{
//printf("%d ",new->data);
if(new==NULL)
{
return;
}
if(n1[level]==NULL)
{
struct linked_list *a =(struct linked_list *)malloc(sizeof(struct linked_list));
a->data=new->data;
a->next=NULL;
n1[level]=a;
printf("%d ",a->data);
}
else
{
struct linked_list *b =(struct linked_list *)malloc(sizeof(struct linked_list));
while(n1[level]->next!=NULL)
{
n1[level]=n1[level]->next;
}
b->data=new->data;
b->next=NULL;
n1[level]=b;
}
linkedlistofbst(new->left,n1,level+1);
linkedlistofbst(new->right,n1,level+1);
}
main()
{
struct linked_list *l=(struct linked_list *)malloc((a+1)*sizeof(struct linked_list));//'a' is the height of the tree
linkedlistofbst(new,&l, 0);//new is the pointer to the root node of the tree.
}

You are right there is problem with the second argument, so do the following
Make the following changes in the main:
For defining an array of linked list of size a+1 and initializing them to NULL
struct linked_list **l=(struct linked_list **)malloc((a+1)*sizeof(struct linked_list*));
for(i=0;i<(a+1);++i)
l[i]=NULL;
Then call the method as
linkedlistofbst(new,l, 0);
Therefore your method must look like
linkedlistofbst(struct node *new,struct linked_list **l, int level)
also make the following modification in else as:
else
{
struct linked_list *ptr=n1[level];
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=(struct linked_list *)malloc(sizeof(struct linked_list));
ptr->next->data=new->data;
ptr->next->next=NULL;
}

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.

Working with linked list whose member is a binary search tree (BST)

I am new to C data structures and I am working with a linked list and a binary search tree. The linked list is composed of 2 members, a string type and a binary search tree type. Here is its definition:
typedef char *String;
struct LinkedList
{
int employer_id;
EmployeeBST employees;
struct LinkedList *next;
} *LinkedList;
struct list
{
ListNodePtr head;
} EmployeeList;
The employee BST definition on the other hand
typedef char *String;
typedef struct bstNode
{
String employeeName;
struct bstNode *left;
struct bstNode *right;
} *BSTNodePtr;
typedef struct bst
{
BSTNodePtr root;
} EmployeeBST;
I want to insert new members to the linked list, here is my insert function
struct listNode *newListNode(String unit_code, EmployeeBST employees)
{
struct listNode *newNode;
newNode = malloc(sizeof(struct listNode));
newNode->employer_id = 89;
newNode->employees;
}
The problem is what value should I assign to the newMode->employees? I have the insert function for the BST which is implemented as:
struct bstNode *insert(struct bstNode *bstNode, int employer_id)
{
if(bstNode == NULL)
{
return newBstNode(employer_id);
}
// traverse right and insert the node
if(employer_id < bstNode->employer_id)
{
bstNode->left = insert(bstNode->left, employer_id);
}else
{
bstNode->right = insert(bstNode->right, employer_id);
}
return bstNode;
}

How to use just one struct for doubly linked list

Lately, I've been giving myself a try as a beginner, at doubly-linked lists, here is my piece of code that allocates memory for a doubly-linked list:
typedef struct Node
{
int data;
struct Node *next;
struct Node *prev;
} NodeT;
typedef NodeT *NodePointer;
typedef struct secondtag
{
NodeT *head;
NodeT *tail;
}doublyll;
/* function prototypes */
doublyll *Create(void);
/* MAIN */
int main(void)
{
doublyll *L=Create();
}
// create_list: returns a pointer to a new empty list or NULL
doublyll *Create(void)
{
NodeT *NodePointer;
doublyll *new_list=malloc(sizeof(*new_list));
if(new_list!=NULL)
{
new_list->head = NULL;
new_list->tail = NULL;
}
NodePointer->next=0;
NodePointer->prev=0;
return new_list;
}
My question is the following: is there a way I can write the two structs I've made, Node and secondtag, into a single 1 struct?

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

List within a Tree?

A small query really with reference to Structs.
If I had a
Struct Node {
char *number;
struct Node *next;
}List;
and a tree Structure:
struct Node {
char *name;
char *number;
struct Node *right;
struct Node *left;
};
and I wanted to design it, such that each Node within my Tree, can each contain a LIST of Phone Numbers, is there a way in which I can do this, and if so, how exactly can I reference my struct List within my Tree?
EDIT:
Any ideas as to why this is seg faulting? Using the Structs recommended below.
TreeNode* AddNode(TreeNode *root, ListNode *list, char *name, char *phonenum) {
int comparison;
if ( root == NULL ) {
root = (TreeNode *)malloc(sizeof(TreeNode));
list = (ListNode *)malloc(sizeof(ListNode));
root->name = strdup(name); root->list->number = strdup(phonenum);
root->left = root->right = NULL;
You'd simply do it like so:
typedef struct Node {
char* name;
List* list;
struct Node *right;
struct Node *left;
} Node;
Then in order to get a local copy of the first element of the list in each node, you'd do something like the following:
Node* treenode; //this is pointing to some node in the tree
char* num_buffer = strdup(treenode->list->number);
//use num_buffer and then call free() on it when you're finished
If you wanted to get a number that was not the first number in the list, you would have to create a search function for your linked list.
Can you do something like this?
typedef struct ListNode
{
char *number;
struct ListNode *next;
} ListNode;
typedef struct TreeNode
{
char *name;
ListNode *numbers;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;

Resources