#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.
Related
#include<stdio.h>
#include<stdlib.h>
struct node{
int id;
char name[50];
float point;
char grade;
struct node *next;
};
struct node *head;
void DataEntry(struct node *);
void GetData(struct node *);
void DataDisp();
void Insert()
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
DataEntry(p);
if(head==NULL)
{
head=p;
}
else
{
struct node *q;
q=head;
while(q!=NULL)
{
q=q->next;
}
q->next=p;
p->next=NULL;
}
}
void DataEntry(struct node *p)
{
printf("Enter value of Id employee\n");
scanf("%d",&p->id);
printf("Enter name of the person\n");
scanf("%s",p->name);
printf("Enter points got by Employee\n");
scanf("%f",&p->point);
printf("Enter grade of the person\n");
fflush(stdin);
scanf("%c",&p->grade);
}
void GetData(struct node *q)
{
printf("The data entered by you is as follows\n");
printf("Id is %d\n",q->id);
printf("Name is %s\n",q->name);
printf("Point is %f\n",q->point);
printf("Grade is %c\n",q->grade);
}
void DataDisp()
{
struct node *z;
z=head;
if(z==NULL)
{
printf("List is Empty\n");
return;
}
while(z!=NULL)
{
GetData(z);
z=z->next;
}
}
int main()
{
Insert();
DataDisp();
}
I have implemented a singly linked list, I insert data in a list using Insert() function and displaying the data using DataDisp() function in DataDisp() function I have used GetData() function which is accessing data from linked list nodes. The problem here is I can do the first insertion and it displays data also using DataDisp() but after that, there is error file.exe stop running. I think there is a segmentation fault; I tried my best to solve the problem but could not run it successfully. Please help.
ONE MORE THING IS TRY TO PREVENT USE OF "FFLUSH" KEYWORD.THIS IS NOT A PORTABLE WAY OF CLEANING THE INPUT BUFFER, AND MAY NOT WORK WITH ALL COMPILER.
I wanna write a program about linked list that gets a number and if the number was equal to the one of the nodes' data , gives the number of that node .
like the datas in 3 nodes are
123
56
78
and it gets a number like 56 and its equal to the second node's data so the output should be 2.
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
node *next;
};
node* cnode();
void find(node *first,int i,int d);
int main()
{
node *first,*last;
int n,i,x,d;
printf(" How many nodes ?\t");
scanf("%d",&x);
for(i=1;i<=x;i++){
last=cnode();
last->next=first;
first=last;
}
printf("\n enter a particular data:\t");
scanf("%d",&d);
printf("\n number of the particular node:\t");
find(first,i,d);
}
void find(node *first,int i,int d){
int count=0;
while (first != NULL)
{
if (first->data == d)
count++;
first = first->next;
}
if(count != 0){
printf("%d",count);
}
if(count == 0){
printf("\n NOT FOUND ! ");
}
}
According to the C Standard the function main without parameters shall be declared like
int main( void )
Secondly this declaration of the structure
struct node{
int data;
node *next;
};
is not a valid C declaration.
You should write
struct node{
int data;
struct node *next;
};
typedef struct node node;
or
typedef struct node{
int data;
struct node *next;
} node;
You have to initialize at least node first with NULL. Otherwise the program will have undefined behavior.
node *first = NULL,*last;
The parameter i is not used within the function find. So it may be removed.
void find(node *first, int d);
The function definition can look at least like
void find( node *first, int d )
{
int count = 0;
while ( first != NULL && first->data != d )
{
first = first->next;
++count;
}
if ( first != NULL )
{
printf("%d",count);
}
else
{
printf("\n NOT FOUND ! ");
}
}
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.
#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.
here's my code in C for making of linked list. Its giving runtime error after the while loop gets executed for one time. Plz help me in correcting my code. (totally confused that where's the error.) I am making a head node first and then adding child nodes to it.
#include <stdio.h>
#include <stdlib.h>
typedef struct node nd;
typedef nd *link;
struct node{
int data;
link next;
};
typedef struct {
int size;
link head;
}list;
void create(link temp)
{
link new;
new=(link)malloc(sizeof(nd));
printf("enter data: ");
scanf("%d",new->data);
temp->next=new;
temp=temp->next;
}
list createlist()
{
list sl;
sl.size=0;
sl.head=0;
return sl;
}
int main()
{
list sl;
sl=createlist();
link temp;
temp=sl.head;
char c;
while (1)
{
printf("Add node?: ");
scanf(" %c",&c);
if (c=='y')
{
create(temp);
sl.size++;
}
else
break;
}
return 0;
}
your createlist() function is returning a reference to a local variable that goes out of scope after it returns. You should instead return a heap based value:
list* createlist() {
list* sl = (list*)malloc(sizeof(list));
sl->size=0;
sl->head=0;
return sl;
}
Initially temp points to NULL. temp = sl.head;
In create(temp) temp->next = new;
You are dereferencing a NULL, address 0x0. I get a segmentation fault when I do that.
Need to change the algorithm.
A debugger shows this problem immediately.
You could use a pointer to pointer for temp. It would be easier to read if you didn't use a typedef for a pointer to node. I haven't tested this, but it should be close:
nd ** create(nd **temp)
{
nd *new;
new=(nd *)malloc(sizeof(nd)); /* this cast shouldn't be needed */
printf("enter data: ");
scanf("%d",&(new->data));
new->next = NULL;
*temp = new;
return &(new->next);
}
/* ... */
int main()
{
nd **temp;
temp = &(sl.head);
/* ... */
temp = create(temp);
/* ... */
}