How to create a node inside a node in linked list, or we can say that a node of subnode in c - data-structures

#include <stdio.h> #include <stdlib.h>
struct node { int ('class'); struct node *link; SubNode();
};
void SubNode() {struct data{char student_name;int Roll_no.;char address;struct data *next;};
struct data* CreateNode()
{ struct data *t; t=(struct node *)malloc(sizeof(struct node)); return (t);};}
struct node START=NULL;struct node CreateNode() {struct node *n; n=(struct node *)malloc(sizeof(struct node)); return (n);
};

Related

preorder traversal using stack

i'm trying to implement binary tree, pre order traversal using stack.
here its popping the last left node and after that root=root->right doesnt seem to work. Pls help. here 7 is being popped out and is being displayed and after the the program is ending.
all the functions are working yet not the desired output
#include<stdio.h>
#include<stdlib.h>
#define maxsize 100
int a[maxsize];
int top=-1;
struct node{
int data;
struct node *left, *right;
};
struct node *newNode(int data)
{
struct node *nn;
nn=(struct node *)malloc(sizeof(struct node));
if(!nn)
return;
nn->data=data;
nn->left=nn->right=NULL;
return nn;
};
void push(struct node *root)
{
printf("pushcalled\n");
if(top!=maxsize-1)
a[++top]=root;
}
int isempty()
{
return(top==-1);
}
struct node *pop()
{
printf("popcalled\n");
if(top!=-1)
{
return a[top];
top--;
}
}
void deleteStack()
{
free(a[top--]);
}
void preorder(struct node *root)
{
while(1)
{
while(root)
{
printf("%d\t",root->data);
push(root);
root=root->left;
}
printf("hello\n");
if(isempty())
break;
printf("hello\n");
root=pop();
printf("Popped data is:%d\n",root->data);
root=root->right;
printf("right data is:%d\n",root->data);
}
deleteStack();
}
int main()
{
int data;
struct node *root=newNode(10);
root->left = newNode(11);
root->left->left = newNode(7);
root->right = newNode(9);
root->right->left = newNode(15);
root->right->right = newNode(8);
preorder(root);
return 0;
}
Your logic is correct but there are some errors in your code.
root=root->right;
printf("right data is:%d\n",root->data);
you should check whether the root is null or not before you try to access root->data. That is why you are getting a segmentation fault. So put a condition if(root!=NULL) above printf() statement.
Another mistake is in the implementation of stack's pop.
struct node *pop()
{
printf("popcalled\n");
if(top!=-1)
{
return a[top];
top--;
}
}
it should be like this,
struct node *pop()
{
printf("popcalled\n");
if(top!=-1)
{
struct node* temp = a[top];
top--;
return temp;
}
}
In your code, when you return a[top]; the line below it i.e. top--; never executes and the value of top remains same.

Building a trie for a set of strings

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <stdlib.h>
using namespace std;
struct trienode
{
map<char, struct trienode*> m;
bool endofword;
};
struct trienode* create_node()
{
struct trienode* new_node = (struct trienode*)malloc(sizeof(struct trienode));
new_node->endofword = false;
return new_node;
};
void word_insert(struct trienode* root, string word)
{
struct trienode* curr = root;
for (unsigned int i =0; i < word.size(); i++) {
if (curr->m.find(word[i]) == curr->m.end()) {
struct trienode* new_node = create_node();
***curr->m.insert(pair<char, struct trienode*>(word[i], new_node) );***
}
curr = curr->m.find(word[i])->second;
}
curr->endofword = true;
}
int main()
{
struct trienode* root = NULL;
root = create_node();
vector<string> v = {"aspirin", "aspetol", "astray", "atran", "chronic"};
for (unsigned int i =0; i < v.size(); i++) {
word_insert(root, v[i]);
}
}
I am trying to build a trie data structure to hold a set of strings. I have written a word_insert() function to insert a word into the trie. For inserting a word into the trie, I start at the root node, see if the map in the root node contains the char and if yes, I proceed to the next charachter. If the char is not present in the map of the node, I create another trienode and insert an entry into the map. However, when I am doing this, I see a problem with my code. My code hangs at the point where I try to insert the (char, struct trienode*) pair into the map.
Could someone tell me what is wrong with that? Thank you.

Run time error in linked list

#include <iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
void deletelist(Node*&head)
{
Node* temp=new Node;
temp=head;
while(head!=NULL)
{
head=head->next;
delete(temp);
temp=head;
}
}
int main() {
Node aman={1,NULL},manjot={2,&aman},sima={3,&manjot},jasbir={4,&sima};
Node* head=&jasbir;
deletelist(head);
return 0;
}
Why this is showing run time error (delete(temp); function is not working here but why)?
Working code for you. Take special note of two scopes in main.
#include <iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
void print(Node*head)
{
while(head!=NULL)
{
std::cout<<head->data<<'\n';
head=head->next;
}
}
int main() {
Node* head;
{
Node aman={1,NULL},manjot={2,&aman},sima={3,&manjot},jasbir={4,&sima};
head=&jasbir;
print(head);
}
{
Node aman={10,NULL},manjot={20,&aman},sima={30,&manjot},jasbir={40,&sima};
head=&jasbir;
print(head);
}
return 0;
}
example on coliru

double linke list

i am deleting a (3 or 4 th) node using double linked list...every time only first element is deleting ?what is the problem with the following code?
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *pre;
struct node *link;
};
//void addafter(struct node*,int,int);
main()
{
struct node *p=NULL;
add(&p,2);
add(&p,3);
add(&p,4);
add(&p,5);
add(&p,6);
add(&p,27);
add(&p,8);
add(&p,9);
add(&p,10);
//addafter(&p,7,20);
delete(&p ,6);
display(&p);
}
add(struct node **q,int n)
{
struct node *temp,*r;
temp=*q;
if(temp==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->pre=NULL;
temp->data=n;
temp->link=NULL;
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *pre;
struct node *link;
};
//void addafter(struct node*,int,int);
main()
{
struct node *p=NULL;
add(&p,2);
add(&p,3);
add(&p,4);
add(&p,5);
add(&p,6);
add(&p,27);
add(&p,8);
add(&p,9);
add(&p,10);
//addafter(&p,7,20);
delete(&p ,6);
display(&p);
}
add(struct node **q,int n)
{
struct node *temp,*r;
temp=*q;
if(temp==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->pre=NULL;
temp->data=n;
temp->link=NULL;
*q=temp;
}
else
{
while(temp->link!=NULL)
temp=temp->link;
r=(struct node*)malloc(sizeof(struct node));
r->pre=temp;
r->data=n;
r->link=NULL;
temp->link=r;
}
}
/*
addafter(struct node **q,int m,int n)
{
struct node *temp,*r,*s;
int i;
temp=*q;
for(i=1;i<m;i++)
temp=temp->link;
s=temp->link;
r=(struct node*)malloc(sizeof(struct node));
r->data=n;
r->link=temp->link;
r->pre=temp;
temp->link=r;
s->pre=r;
}
*/
/*display(struct node **q)
{
struct node *temp;
temp=*q;
while(temp!=NULL)
{
printf("\n %d\n",temp->data);
temp=temp->link;
}
}
*/
delete(struct node **q,int n)
{
struct node *temp,*old,*s;
temp=*q;
while(temp!=NULL)
{
if(temp->data=n)
{
if(temp == *q)
{
*q=temp->link;
free(temp);
return;
}
else
{
// s=temp->link;
old->link=temp->link;
// s->pre=old;
free(temp);
return;
}
}
else
{
old=temp;
temp=temp->link;
}
}
printf("data not found");
}
display(struct node **q)
{
struct node *temp;
temp=*q;
while(temp!=NULL)
{
printf("\n %d\n",temp->data);
temp=temp->link;
}
}
At first glance I see this mistake:
old->link=temp->link;
temp->link->pre=temp->pre; // or old.
free(temp);
You should set the pointers for next and previous nodes too. You can make this change in your add method which is also wrong.

Linked list example using threads

I have read the following code of using CRITICAL_SECTION when working with multiple threads to grow a linked list. what would be the main() part which uses two threads to add to linked list?
#include <windows.h>
typedef struct _Node
{
struct _Node *next;
int data;
} Node;
typedef struct _List
{
Node *head;
CRITICAL_SECTION critical_sec;
} List;
List *CreateList()
{
List *pList = (List*)malloc(sizeof(pList));
pList->head = NULL;
InitializeCriticalSection(&pList->critical_sec);
return pList;
}
void AddHead(List *pList, Node *node)
{
EnterCriticalSection(&pList->critical_sec);
node->next = pList->head;
pList->head = node;
LeaveCriticalSection(&pList->critical_sec);
}
void Insert(List *pList, Node *afterNode, Node *newNode)
{
EnterCriticalSection(&pList->critical_sec);
if (afterNode == NULL)
{
AddHead(pList, newNode);
}
else
{
newNode->next = afterNode->next;
afterNode->next = newNode;
}
LeaveCriticalSection(&pList->critical_sec);
}
Node *Next(List *pList, Node *node)
{
Node* next;
EnterCriticalSection(&pList->critical_sec);
next = node->next;
LeaveCriticalSection(&pList->critical_sec);
return next;
}
It will likely involve one or more calls to CreateThread

Resources