How to declare struct variable with in the main function in c? - c

I am new for c programming, my code is working but my question is, if I declare struct node *a,*b; in main function ,how to passing a and b to void create() .And why it's not working, can someone please help me understand it?
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int d;
struct node *next;
}*start=NULL;struct node *a,*b; //move this part to main function -> struct node *a,*b; but its not working
void create()
{
a=(struct node *)malloc(sizeof(struct node));
printf("Enter the data : ");
scanf("%d",&a->d);
a->next=NULL;
if(start==NULL)
{
start=a;
b=a;
}
else
{
b->next=a;
b=a;
}
}
void display()
{
struct node *a;
printf("\nThe Linked List : ");
a=start;
while(a!=NULL)
{
printf("%d--->",a->d);
a=a->next;
}
printf("NULL\n");
}
void main()
{
char ch;
do
{
create();
printf("Do you want to create another : ");
ch=getche();
}
while(ch!='n');
display();
}
void freenodes()
{
struct node *a;
a = start;
while(a != NULL)
{
struct node *freenode = a ;
a = a->next;
free(freenode) ;
}
}

void main()
{
struct node name_of_varialbe;//this is struct variable declaration
...
}

Just change the prototype of void create() to void create(struct node *,struct node *) and call this way:
char ch;
do
{
struct node *a_main=(struct node *)malloc(sizeof(struct node));
struct node *b_main=(struct node *)malloc(sizeof(struct node));
create(a_main,b_main);
printf("Do you want to create another : ");
ch=getche();
}while(ch!='n');
void create(struct node *a,struct node *b)
{
//do your stuff
}

Related

Problem in linked list while using struct variable

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.

Creating Linked-list of characters and printing it

I'm trying to implement a Linked list using this code.This code complies successfully but results in Segmentation fault (core dumped) error.How can I resolve this?
#include<stdio.h>
#include<stdlib.h>
struct node{
char ch;
struct node *next;
};
struct node *head=(struct node *)malloc(sizeof(struct node));
struct node *p1=NULL;
void addnode(char ch) {
if(head==NULL) {
head->ch=ch;
head->next=NULL;
}
else {
struct node *New=(struct node *) malloc (sizeof(struct node));
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=New;
}
}
void main() {
char ch,frm,to;
printf("\nEnter the string");
while((ch=getchar())!='\0')
addnode(ch);
for(p1=head;p1!=NULL;p1=p1->next)
printf("\n%c",p1->ch);
}
Simple Mistakes first: When you allocate memory in global you initiate a function call(malloc is also a function). Function calls can only be made inside main or other funcitons. So just declare head don't use malloc in global.
#include<stdio.h>
#include<stdlib.h>
struct node{
char ch;
struct node *next;
};
struct node *head=NULL;
struct node *p1=NULL;
void addnode(char ch) {
if(head==NULL) {
struct node *New=(struct node *) malloc (sizeof(struct node));
head=New;
New->ch=ch;
New->next=NULL;
}
else {
struct node *New=(struct node *) malloc (sizeof(struct node));
New->ch=ch;
New->next=NULL;
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=New;
}
}
void main() {
char ch,frm,to;
printf("\nEnter the string");
while((ch=getchar())!='\n')
addnode(ch);
for(p1=head;p1!=NULL;p1=p1->next)
printf("\n%c",p1->ch);
}
Second mistake: Inside your addnode function when you chekk if head is null or not allocate some memory and assign it to head.
Third mistake: in your getchar() check until you find a new line not null character.
Fourth mistake: Assign ch to New and set New->next=null. You almost completely forgot that.
This worked better and I over came the errors :). And i missed pointers clarity there, which were rectified here..
#include<stdio.h>
#include<stdlib.h>
struct Node{
char ch;
struct Node *next;
};
struct Node head={'\0',NULL};
struct Node *p1=NULL;
void add(char ch){
if(head.ch=='\0')
head.ch=ch;
else{
struct Node *new=(struct node *)malloc(sizeof(struct Node));
new->ch=ch;
for(p1=&head;p1->next!=NULL;p1=p1->next);
p1->next=new;
}
}
void main(){
char c;
while((c=getchar())!='\n')
add(c);
for(p1=&head;p1!=NULL;p1=p1->next)
printf("%c\n",p1->ch);
}
But i still receive a warning saying,
initialization from incompatible pointer type [enabled by default]
struct Node *new=(struct node *)malloc(sizeof(struct Node));
^
I am not sure this is c way..but you have to think your code over how to release your allocated pointer...like free list function maybe..
Here is my way.
#include<stdio.h>
#include<stdlib.h>
struct node{
char ch;
struct node *next;
};
struct node * addnode(struct node *head, struct node *p1, char ch) {
if(head==NULL) {
printf("......return 2... \r\n");
head=(struct node *)malloc(sizeof(struct node));
head->ch=ch;
head->next=NULL;
return head;
}
else {
struct node *New=NULL;
printf("......return ... \r\n");
New=(struct node *) malloc (sizeof(struct node));
New->ch = ch;
New->next=NULL;
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=New;
return head;
}
}
void main() {
char ch,frm,to;
struct node *head=NULL, *p1=NULL;
printf("\nEnter the string \n");
while((ch=getchar())!='q')
head = addnode(head, p1, ch);
for(p1=head;p1!=NULL;p1=p1->next)
{
printf("\n%c",p1->ch);
}
}
Another one.
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
char ch;
struct node *next;
} *pNODE, NODE;
pNODE addnode2(pNODE head, pNODE p1, char ch) {
if(head==NULL) {
printf("......return 2... \r\n");
head=(pNODE)malloc(sizeof(NODE));
head->ch=ch;
head->next=NULL;
return head;
}
else {
struct node *new=NULL;
printf("......return ... \r\n");
new=(pNODE) malloc (sizeof(NODE));
new->ch = ch;
new->next=NULL;
for(p1=head;p1->next!=NULL;p1=p1->next);
p1->next=new;
return head;
}
}
void main() {
char ch,frm,to;
pNODE head=NULL;
pNODE p1=NULL;
printf("\nEnter the string \n");
while((ch=getchar())!='q')
head = addnode2(head, p1, ch);
for(p1=head;p1!=NULL;p1=p1->next)
{
printf("\n%c",p1->ch);
}
}

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*

program to find the largest element in stack while implementing linked list but the data is not being accesed

#include<stdio.h>
#include<stdlib.h>
//structure//
struct node
{
int data;
struct node *link;
};
//function to push elements into stack//
void push(struct node *top_ref,int x)
{
struct node *new=(struct node *)malloc(sizeof(struct node));
new->data=x;
new->link=top_ref;
top_ref=new;
}
//function to get largest element//
int largest(struct node *top_ref)
{
int temp=0;
while(top_ref!=NULL)
{
if(temp<top_ref->data)
{
temp=top_ref->data;
}
top_ref=top_ref->link;
}
return temp;
}
//function to print the stack//
void print(struct node *top_ref)
{
while(top_ref!=NULL)
{
printf("%d->",top_ref->data);
top_ref=top_ref->link;
}
printf("\n");
}
//main function//
int main()
{
struct node *stack=(struct node *)malloc(sizeof(struct node));
stack=NULL;
push(stack,1);
push(stack,5);
push(stack,6);
push(stack,3);
print(stack);
printf("the largest no. is %d\n",largest(stack));
}
trying to find the largest no. in a stack.
there is a problem in line 20, 32 top_ref->data is not being accesed
top_ref is the last node of the linked list
new to linked list and data structures
the output i am getting is
empty line
the largest no. is 0
The point here is stack is a pointer to another pointer so should be taken as a double pointer in push function.
#include<stdio.h>
#include<stdlib.h>
//structure//
struct node
{
int data;
struct node *link;
};
//function to push elements into stack//
void push(struct node **top_ref,int x)
{
struct node * tmp=(struct node *)malloc(sizeof(struct node));
tmp->data=x;
tmp->link=NULL;
if(top_ref==NULL){
*top_ref=tmp;
}
else{
tmp->link= *top_ref;
*top_ref=tmp;
}
}
//function to get largest element//
int largest(struct node *top_ref)
{
int temp=0;
while(top_ref!=NULL)
{
if(temp<top_ref->data)
{
temp=top_ref->data;
}
top_ref=top_ref->link;
}
return temp;
}
//function to print the stack//
void print(struct node *top_ref)
{
while(top_ref!=NULL)
{
printf("%d->",top_ref->data);
top_ref=top_ref->link;
}
printf("\n");
}
//main function//
int main()
{
struct node *stack;
stack=NULL;
//stack=NULL;
push(&stack,1);
push(&stack,5);
push(&stack,6);
push(&stack,3);
print(stack);
printf("the largest no. is %d\n",largest(stack));
}

I want to swap the linkedlist terms pairwise, my code is giving me Segmentation Fault

I want to swap the linkedlist terms pairwise.
Here is my code. It is giving me Segmentation Fault-core dumped
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void insert(struct node *n)
{
int num;
printf("Enter a number : ");
scanf("%d",&num);
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
if(n==NULL)
{
head=temp;
head->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}
int main()
{
struct node *n;
head=NULL;
n=head;
int i;
for(i=0;i<6;i++)
{
insert(n);
n=head;
}
display(n);
pairswap(n);
display(n);
}
void display(struct node *n)
{
struct node *temp;
temp=n;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
}
void pairswap(struct node *n)
{
struct node *temp,*temp1,*temp2;
temp=n;
temp1=temp->next;
while(temp!=NULL)
{
int tempnum;
tempnum=temp->data;
temp->data=temp1->data;
temp1->data=tempnum;
if(temp==n)
{
head=temp;
head->next=temp1;
}
else
{
temp2->next=temp;
temp2->next->next=temp1;
}
temp2=temp1;
temp=(temp->next)->next;
temp1=temp->next;
}
n=head;
}
Please learn about the debuggers.
Somewhere at the end of the list the value of
(temp->next)->next
is NULL, which you are putting it in variable temp.
Before making this assignment temp1=temp->next, you need to check if temp is NULL and take proper action.
The problem is pairswap function. In the while loop you are using the condition:
while(temp!=NULL)
and inside the loop you are assigning:
temp=(temp->next)->next
which will get crashed on the second last node since you are trying to dereference NULL pointer.
Use the following condition:
while((temp->next)->next!=NULL)
or
while (temp1->next != NULL)

Resources