Error, linked lists using C programming - c

I'm trying to code simple push function and everything is fine until run time, executed code crashes. Anyone can clarify the reason, please ?
#include<stdio.h>
#include<stdlib.h>
typedef struct list{
int order;
struct list *next;
}list;
void push(struct list **arg,int i);
int main()
{
struct list **ptr=NULL;
for(int i=0;i<10;++i){
push(ptr,i);
}
return 0;
}
void push(struct list **arg,int i){
struct list *temp;
temp= malloc(sizeof(list));
temp->order=i;
temp->next=*arg;
*arg=temp;
}

Write
list *ptr=NULL;
^^^
for(int i=0;i<10;++i){
push( &ptr,i);
^^^^
}
Otherwise if to declare like this
struct list **ptr=NULL;
then this dereferencing
*ptr
results in undefined behavior.

Related

How can I fix this linked list output?

#include <stdio.h>
#include <stdlib.h>
struct node{
int sayi;
struct node* sonraki;
};
struct node* baslangic=NULL;
struct node* olustur(int sayi){
struct node* yenidugum=(struct node*)malloc(sizeof(struct node));
yenidugum->sayi=sayi;
yenidugum->sonraki=NULL;
return yenidugum;
}
void sonaekle(int sayi){
struct node* sonaeklenecek=olustur(sayi);
if(baslangic==NULL){
baslangic=sonaekle;
}
else
{
struct node* temp=baslangic;
while(temp->sonraki!=NULL){
temp=temp->sonraki;
temp->sonraki=sonaekle;
}
}
}
void yazdir(){
struct node* temp=baslangic;
while(temp!=NULL){
printf("%d =>",temp->sayi);
temp=temp->sonraki;
}
}
int main()
{
int secim, sayi;
while(1){
printf("1-Sona eleman ekle.....\n");
printf("Yapmak istediginiz secimi yapin...\n");
scanf("%d",&secim);
switch(secim){
case 1:
printf("Hangi elemani ekleyeceksiniz..\n");
scanf("%d",&sayi);
sonaekle(sayi);
yazdir();
break;
}
}
return 0;
}
I am trying to do make linked list but when I run this code it gives output like this:-443987883 =>. What is my mistake. I can't find it. Thank you for your answers and giving your time.
There are typos.
Instead of
baslangic=sonaekle;
where you are assigning the function pointer of the type void ( * )( int ) to the pointer of the type struct node * you need to write
baslangic=sonaeklenecek;
Instead of this while loop
while(temp->sonraki!=NULL){
temp=temp->sonraki;
temp->sonraki=sonaekle;
}
you need to write
while(temp->sonraki!=NULL){
temp=temp->sonraki;
}
temp->sonraki = sonaeklenecek;

Deletion algorithm of doubly linked list

#include<stdio.h>
#include<malloc.h>
struct list {
char name[30];
char subject1[30];
char subject2[30];
struct list* next;
struct list* previous;
};
typedef struct list node;
node input(node* n1)
{
node n2;
n2.previous=n1;
n1->next=&n2;
n2.next=NULL;
scanf("%s",n2.name);
scanf("%s",n2.subject1);
scanf("%s",n2.subject2);
return n2;
}
void delete(node *n1)
{
if(n1->next!=NULL)
n1->previous->next=n1->next;
else
n1->previous->next=NULL;
if(n1->previous!=NULL)
n1->next->previous=n1->previous;
printf("\nDeleting....");
free(n1);
}
int main()
{
node* start;
start=(node*)malloc(sizeof(node));
node n1=input(start);
node n2=input(&n1);
start->previous=NULL;
printf("%s %s %s %s %s %s",n1.name,n1.subject1,n1.subject2,n2.name,n2.subject1,n2.subject2);
delete(&n1);
delete(&n2);
delete(start);
return 0;
}
The deletion is not happening properly. It's giving an unhandled exception returning 255 error. Also, in books only the n1 node is being deleted and the start(header) and last node is being left as it is. But, I want to delete all the nodes and header. Please explain and correct. Also, I am writing this code in C and C++. Please correct accordingly.

Error of expression syntax

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *create_cll(node *head,int n);
void main()
{
clrscr();
node *head,*p;
int n;
printf("Enter the no.of elements:");
scanf("%d",&n);
head=create_cll(*head,int n);
getch();
}
node *create_cll(node *head,int n)
{
node *rear,*p;
int i;
head=p;
for(i=1;i<n;i++)
{
p=(node*)malloc(sizeof(node));
scanf("%d",&p->data);
p=rear;
rear->next=p;
p=p->next;
}
printf("Circular linked list created..!");
return (head);
}
Code is about creating the circular linked list.
But here I have an error of expression syntax which I am unable to solve.
The error is in the line in the main() section where head is equalled to the function.
So I need help...
You need to pass a ptr-to-node, not a node; and remove the int keyword:
head = create_cll(head, n);
In other news:
It's int main in C, not void main
Don't cast the return from malloc in C
Not testing the return value from scanf is a sure recipe for surprises.
printf format strings usually have a newline "\n" at the end.
<conio.h> and getch are not C (even though Mr. Gates wants you to believe that.) Use getchar() instead.
head is already a pointer
*head in your code could be changed to head as the method definition follows.

Value of struct member changes after printing C

I got this code and a strange behaviour while printing the id member variable of node.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int id;
int visited;
// struct node *neighbors_[];
};
struct graph
{
struct node nodes[26];
int adjMat[26][26];
};
struct stack_item
{
struct node node;
struct stack_item *next_;
};
struct myStack
{
struct stack_item *anfang_;
};
void initGraph(struct graph *graph_);
void push(struct myStack *stack_, struct node node);
int main()
{
struct graph graph;
struct myStack stack;
char ausgabe[26]="";
initGraph(&graph);
//READ DATA
char line[200];
int firstTime=1,first;
first=0;
push(&stack,graph.nodes[first]);
printf("ID %i\n",stack.anfang_->node.id);
printf("ID %i\n",stack.anfang_->node.id);
//FINISHED DATA READING
//CALL DFS
//dfs(graph,stack,ausgabe);
}
void push(struct myStack *stack_, struct node node)
{
struct stack_item item;
item.node=node;
item.next_=stack_->anfang_;
stack_->anfang_=&item;
}
void initGraph(struct graph *graph_)
{
int i,j;
for(i=0; i<26; i++)
{
struct node node= {i,0};
graph_->nodes[i]=node;
for(j=0; j<26; j++)
{
graph_->adjMat[i][j]=0;
}
}
}
If i execute this, the first print command leads to 'ID 0',the second to 'ID 1980796117'. How can this value change by printing it? Could please anyone help me, i've got really no idea!
void push(struct myStack *stack_, struct node node)
{
struct stack_item item;
item.node=node;
item.next_=stack_->anfang_;
/* BAD! */
stack_->anfang_=&item;
}
item is a local variable which, when the push function returns, goes out of scope. Any existing pointers which refer to this object are now invalid, and dereferencing it results in undefined behavior.
You will need to dynamically allocate item (i.e., malloc) if you need it to persist once the function has returned.

changing struct/variable content in C

I would like to get some help with the following question.
I have a struct Node and I want to change it's insides using some method.
and I want to print the content of the changed struct inside my main method.
how do I get the struct changed and see the changed value in my main without returning the node as a return value.
I guess it might be solved with passing the struct Node as a pointer and then editing it.
what is the right way to do so?
for example:
typedef struct Node{
struct Node * right;
struct Node * left;
void * data;
}Node;
void insert(void * element, Node* root){
if(root==NULL){
root=(Node*)malloc(sizeof(Node));
root->data=element;
}
}
int main(){
Node a;
int b=8;
insert(&b,&a);
printf("%d",*(int*)a.data);
return 0;
}
printf doesn't print 8 it prints 1 (i guess some garbage)
It sounds like you are trying to do the following
Create a struct in one method, say main
Pass it to a second method, say example
Have example modify the struct and have the results visible in main
If so then the way to do this in C is by passing the struct as a pointer to example.
struct Node {
int data;
struct Node* pNext;
};
void example(struct Node* pNode) {
pNode->data = 42;
pNode->pNext = NULL;
}
int main() {
struct Node n;
example(&n);
printf("%d\n", n.data);
}
EDIT
Responding to the updated question.
To see the result of a modification of a Node you must pass a Node*. And accordingly to see the result of a Node* modification you need to pass a Node**. Essentially you need to pass one more level of indirection than the value you want to mutate / return.
void insert(void* element, Node** ppRoot){
if (NULL == *ppRoot) {
Node* pTemp = malloc(sizeof(Node));
pTemp->data = element;
*ppRoot = pTemp;
}
}

Resources