why float value in structute can be used for linkedlist ???
if you are taking a float variable in a structure, why you can't use it in linked-list?
why you need to take a floating value first in a temporary variable and then assigning it to float variable of structure? some people write a function to avoid this, what is that and what is reason for that?
working if "marks" is an integer
here is the code which works if "marks" is declared as an "int" and using format specifier "%d" instead of "%f"
tested on turbo c++ editor only
Code:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct LinkedList
{
int roll;
float marks;
char name[20];
struct LinkedList *next;
}Node;
Node* CreateNode()
{
Node *temp;
temp=(Node*)malloc( sizeof(Node) );
printf("\n\t\tEnter roll no : ");
scanf("%d",&temp->roll);
printf("\n\t\tEnter name : ");
flushall();
gets(temp->name);
printf("\n\t\tEnter Marks : ");
scanf("%f",&temp->marks);
temp->next=NULL;
return temp;
}
Node* AddNode(Node *head)
{
Node *newnode,*temp;
temp=head;
newnode=CreateNode();
if(head==NULL)
{
head=newnode;
}
else
{
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
}
return head;
}
void Display(Node *head)
{
Node *temp;
temp=head;
printf("\n\nRoll no Name \t\tMarks\n");
while(temp!=NULL)
{
printf("\n %d\t ",temp->roll);
cputs(temp->name);
printf("\t %f ",temp->marks);
temp=temp->next;
}
printf("\n\n");
}
void Displayrev(Node *temp)
{
if(temp->next!=NULL)
Displayrev(temp->next);
printf("\n %d\t ",temp->roll);
cputs(temp->name);
printf("\t %f ",temp->marks);
}
Node* Delete(Node *head)
{
int option,r;
Node *temp,*previous;
temp=head;
printf("\n\t\tEnter option. \n\t\t1. Delete first record\n\t\t");
printf("Delete a particular record.\n\t\t3.Delete last record\n");
scanf("%d",&option);
switch(option)
{
case 1:
head=head->next;
free(temp);
break;
case 2:
printf("\nEnter roll no. to delete the record: ");
scanf("%d",&r);
while(r!=temp->roll)
{
previous=temp;
temp=temp->next;
}
previous->next=temp->next;
free(temp);
break;
case 3:
while(temp->next!=NULL)
{
previous=temp;
temp=temp->next;
}
previous->next=NULL;
free(temp);
break;
}
return head;
}
Node* Insert(Node *head)
{
int option,pos,loc=1;
Node *temp,*newnode;
temp=head;
newnode=CreateNode();
printf("\n\t\tEnter option. \n\t\t1. Insert at begining.\n\t\t2. Insert ");
printf("at particular position.\n\t\t3.Insert at end\n");
scanf("%d",&option);
switch(option)
{
case 1:
newnode->next=temp;
head=newnode;
break;
case 2:
printf("\nEnter Position at which you want to insert record \n");
scanf("%d",&pos);
while(loc!=pos-1)
{
temp=temp->next;
loc++;
}
newnode->next=temp->next;
temp->next=newnode;
break;
case 3:
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
break;
}
return head;
}
void Search(Node *head)
{
Node *move;
int r,flag=0;
move=head;
if(head==NULL)
printf("\nHmmmmm .... Linked List is not yet created!!!!");
else
{
printf("\nEnter Id\n");
scanf("%d",&r);
while(move!=NULL)
{
if(move->roll==r)
{
printf("\nRecord found\n");
flag=1;
printf("\n\nRoll no Name \t\tMarks\n");
printf("\n %d\t ",move->roll);
cputs(move->name);
printf("\t %f ",move->marks);
}
move = move->next;
}
if(move==NULL && flag==0)
printf("\nRecord not found !!!\n");
}
}
Node* Modify(Node *head)
{
Node *move;
int r,flag=0;
move=head;
if(head==NULL)
printf("\n\nRecord is not yet created !!!");
else
{
printf("\nEnter Roll no : ");
scanf("%d",&r);
while(move!=NULL)
{
if(move->roll==r)
{
printf("\n\t\tEnter roll no : ");
scanf("%d",&move->roll);
printf("\n\t\tEnter name : ");
flushall();
gets(move->name);
printf("\n\t\tEnter Marks : ");
scanf("%f",&move->marks);
flag=1;
}
move = move->next;
}
if(move==NULL && flag==0)
printf("\nNode not found !!!\n");
}
return head;
}
void Freeall(Node *head)
{
Node *temp=head;
while(temp!=NULL)
{
head=head->next;
temp->next=NULL;
free(temp);
temp=head;
}
}
void main()
{
int option,roll,ch;
Node *head=NULL;
clrscr();
do
{
printf("\n\n**************************** STUDENT DATABASE ****************************");
printf(" \n\n\t\t\t 1.Add Record\n\t\t\t 2.Delete Record \n\t\t\t 3.Insert Record \
\n\t\t\t 4.Display Record \n\t\t\t 5.Search Record \n\t\t\t 6.Modify \
\n\t\t\t 7.Freeall \n\t\t\t 8.Exit \n\n\t\t\t ");
scanf("%d",&option);
switch(option)
{
case 1:
head=AddNode(head);
break;
case 2:
head=Delete(head);
break;
case 3:
head=Insert(head);
break;
case 4:
if(head==NULL)
{
printf("\n\nRecord is Empty !!!");
break;
}
printf("\n\n1:Display serially \n\n2:Display Reverse \n\n");
scanf("%d",&ch);
printf("\n\nRoll no Name \t\tMarks\n");
switch(ch)
{
case 1:
Display(head);
break;
case 2:
Displayrev(head);
break;
default:
printf("\n\nInvalid Choice !!!");
}
break;
case 5:
Search(head);
break;
case 6:
head=Modify(head);
break;
case 7:
Freeall(head);
head=NULL;
break;
}
}while(option!=8);
getch();
}