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.
Related
I want to create a function that search for a certain value in a linked list and then put the node containing it and the node before the one containing it in two separate nodes.
This is my code and I can't figure out why it does not work:
#include <stdio.h>
#include <stdlib.h>
struct node{
int val;
struct node* next;
};
typedef struct node node_t;
void printlist(node_t *head){
node_t *temp=head;
while (temp!=NULL){
printf("%d -",temp->val);
temp=temp->next;
}
printf("\n");
}
node_t *create_new_node(int value){
node_t *result=malloc(sizeof(node_t));
result->val=value;
result->next=NULL;
return result;
}
void *insert_after_node(node_t *node_to_insert,node_t *newnode){
newnode->next=node_to_insert->next;
node_to_insert->next=newnode;
}
void *find_node(node_t *head,int value,node_t *R,node_t *RA){
node_t *tmp=head;
while (R!=NULL && tmp!=NULL){
if (tmp->val==value)
R=tmp;
else{
RA=tmp;
tmp=tmp->next;
}
}
}
int main(){
node_t *head=NULL;
node_t *tmp;
node_t *R=NULL;
node_t *RA=NULL;
for (int i=0;i<25;i++){
tmp=create_new_node(i);
insert_at_head(&head,tmp);
}
find_node(head,13,R,RA);
printlist(head);
printlist(RA);
printlist(R);
}
Thank you !
You want to pass in the addresses of R and RA to find_node. And declare the params as **
void *find_node(node_t *head,int value,node_t **R,node_t **RA){
and then use (*R/ *RA) in the function
Call with
find_node(head,13,&R,&RA);
In your version, find_node cannot reassign the pointers passed in.
I have written this code in c to implement linked list. while the semantic and syntax are fine, it does not work as it should. For example, when I insert 1 into the list and then print the linked list, it shows that the list is empty.
#include<stdio.h>
#include<stdlib.h>
struct node {
int info;
struct node *next;
}; typedef struct node Node;
void addNode(Node *head, int x)
{
Node *temp;
temp=malloc(sizeof(temp));
temp->info=x;
if (head==NULL)//if this is the first node in the list
{
head=temp;
temp->next=NULL;
}
else //if not, add it as the head
{
temp->next=head;
head=temp;
}
}
void appendNode(Node *head, int x)
{
Node *rear =head;
Node *temp= malloc(sizeof(temp));
temp->info=x;
temp->next=NULL;
while (rear->next!=NULL)
rear=rear->next;
rear->next=temp;
rear=temp;
}
void insertNodeafter(Node *head, int location, int x)
{
int i;
Node *before, *after = head;
Node *temp= malloc(sizeof(temp));
temp->info=x;
for (i=0;i<location;i++)
before=before->next;
after=before->next;
temp->next=after;
before->next=temp;
}
void insert(Node *head, int x)
{
int c=0;
Node *temp;
temp=head;
if(temp==NULL)
{
addNode(temp,x);
}
else
{
while(temp!=NULL)
{
if(temp->info<x)
c++;
temp=temp->next;
}
if(c==0)
addNode(temp,x);
else if(c<listSize())
insertNodeafter(temp,x,++c);
else
appendNode(temp,x);
}
}
int listSize()
{
Node *head, *n;
int c=0;
n=head;
while(n!=NULL)
{
n=n->next;
c++;
}
return c;
}
void DisplayLinkedList(Node* head)
{
Node *rear=NULL;
if (head==NULL)
printf("list is empty!\n");
else
{
rear=head;
while (rear!=NULL)
printf("%d |---> ", rear->info);
rear=rear->next;
}
}
int getNextNode(Node *head)
{
if (head == NULL)
return -1;
else
return head->next->info;
}
Node* deleteNode(Node *head, int x)
{
Node *temp;
if (head == NULL)
return NULL;
else
{
if (head->info==x)
{
temp = head->next;
free(head);
head=temp;
return head;
}
else
{
deleteNode(head->next,x);
return head;
}
}
}
void main()
{
int i=0;
Node *myNode;
insert(myNode,1);
DisplayLinkedList(myNode);
}
Because you are using Node* variable instead of using Node** variable in function parameter. Since you are using Node* so changes done in variable head is local to that function. If you want to reflect these changes even after function call(which you obviously wants to) then use Node** and use it accordingly in your code.
As the previous poster has mentioned, it is best to use Node** variable to reflect changes after a function call. If you wanted to use Node*, you'd have to return Node* back to main to print from it.
I pared your code down to add the 1, using AddNode, insert and DisplayLinkedList and it is now displaying correctly with the code below.
You should also set Node* to NULL in main to initialize the empty linked list. Check your DisplayLinkedList function - you're missing curly brackets in the while loop. It was only reading the printf line and not traversing the list, leading to an infinite loop.
Best practice is to debug and test as you are creating this program.
#include<stdio.h>
#include<stdlib.h>
#include "stack.h"
void main()
{
int i=0;
Node *myNode;
myNode = NULL;
insert(&myNode,1);
DisplayLinkedList(myNode);
}
void addNode(Node **head, int x)
{
Node *temp;
temp=malloc(sizeof(temp));
temp->info=x;
if (*head==NULL)//if this is the first node in the list
{
*head=temp;
temp->next = NULL;
}
else //if not, add it as the head
{
temp->next=*head;
*head=temp;
}
}
void insert(Node **head, int x)
{
int c=0;
Node *temp;
temp=*head;
if(temp==NULL)
{
addNode(head ,x);
}
}
void DisplayLinkedList(Node* head)
{
Node *rear=NULL;
if (head==NULL)
printf("list is empty!\n");
else
{
rear=head;
while (rear!=NULL)
{
printf("%d |---> ", rear->info);
rear=rear->next;
}
}
}
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.
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*
I've implemented the following code for queues as a linked list. Now I'm trying to get an hashtable as an array of queues as a linked list. It works fine until I do insertion after deletion.
What is being done is a queue is implemented as linked list. So when I want to remove, I delete the head element and for insert I use tail.
To do an hashtable I maintain another linked list of keys in the order in which they were inserted. Deletion starts with first removing this key and going to that individual linked list and removing the head and updating the head.
#include<stdio.h>
#include<stdlib.h>
struct Node{
int value;
struct Node *next;
struct Node* head;
struct Node* tail;
};
struct Node* lruhashtable[10];
struct Node* trackHead;
struct Node* trackTail;
void insert(int page)
{
if(trackHead==NULL)
{
trackHead=malloc(sizeof(struct Node));
trackHead->value=(page-1)%10;
trackHead->next=NULL;
trackTail=trackHead;
}
else
{
struct Node* temp=malloc(sizeof(struct Node));
temp->value=(page-1)%10;
temp->next=NULL;
trackTail->next=temp;
trackTail=temp;
}
}
void hashEntry(int page)
{
struct Node** iter;
iter=&lruhashtable[(page-1)%10];
for(;*iter;iter=&(*iter)->next);
*iter = malloc(sizeof **iter );
(*iter)->value = page;
(*iter)->next = NULL;
if((*iter)->head==NULL)
{
(*iter)->head=*iter;
(*iter)->tail= (*iter)->head;
}
else
{
(*iter)->tail->next=*iter;
(*iter)->tail=*iter;
}
insert(page);
}
void deleteInHashEntry()
{
int pageToDelete=delete();
struct Node** iter;
iter=&lruhashtable[pageToDelete];
if((*iter)->head!=NULL)
{
struct Node* curr=(*iter)->head;
(*iter)=curr->next;
if((*iter)!=NULL)
(*iter)->head=*iter;
free(curr);
}
else
{
(*iter)->tail=NULL;
}
}
void print()
{
int i;
struct Node **iter;
for(i=0;i<10;i++)
{
iter=&lruhashtable[i];
for(;*iter;iter=&(*iter)->next)
{
printf("%d%s%d\n",(*iter)->value,"--",i);
}
}
}
int delete()
{
int page=-1;
if(trackHead!=NULL)
{
struct Node*current=trackHead;
page=current->value;
trackHead=current->next;
free(current);
}
else
{
trackTail=NULL;
}
return page;
}
void printTrack()
{
struct Node* temp=trackHead;
while(temp!=NULL)
{
printf("%d",temp->value);
printf("\n");
temp=temp->next;
}
}
int main()
{
hashEntry(1);
hashEntry(11);
hashEntry(2);
hashEntry(3);
hashEntry(22);
hashEntry(4);
hashEntry(33);
print();
printTrack();
deleteInHashEntry();
print();
printTrack();
deleteInHashEntry();
print();
printTrack();
deleteInHashEntry();
print();
printTrack();
hashEntry(1);
hashEntry(11);
hashEntry(22);
deleteInHashEntry();
deleteInHashEntry();
deleteInHashEntry();
deleteInHashEntry();
deleteInHashEntry();
deleteInHashEntry();
deleteInHashEntry();
return 0;
}
GDB to the rescue, thought I think I should have caught it much before,in the function hashEntry if((*iter)->head==NULL) this statement is wrong as everytime this will execute after malloc. Added a STATUS variable to do this check before malloc statement and change if condition to if status. Hopefully this is the only bug.