Hello :-)
I have build a task of Linked List but for some reason , after i break the Input sequence by entering the same INT value twice in a row , the menu appears twice . howcome?
Thank , Code Attached .
Yotam
Hello :-)
I have build a task of Linked List but for some reason , after i break the Input sequence by entering the same INT value twice in a row , the menu appears twice . howcome?
Thank , Code Attached .
Yotam
#include <stdio.h>
#include <stdlib.h>
typedef struct list_node
{
int data;
struct list_node *next;
}LIST_NODE,*LIST_NODE_PTR;
typedef struct mid_list
{
LIST_NODE* head;
LIST_NODE* end;
LIST_NODE* biggest;
int size;
}MID_LIST,*MID_LIST_PTR;
int add_value(MID_LIST_PTR list, int new_info);
int get_first_value(MID_LIST parm);
int get_last_value(MID_LIST parm);
int get_items_number(MID_LIST parm);
int the_biggest(MID_LIST parm);
void print_nodes(MID_LIST parm);
void menu();
int main()
{
char choise='1';
int input;
MID_LIST data_list;
MID_LIST_PTR data_list_ptr;
data_list.head=data_list.end=data_list.biggest=NULL;
data_list.size=0;
data_list_ptr=&data_list;
while (choise!='7')
{
menu();
scanf("%c",&choise);
switch(choise)
{
case '1': { printf("Please enter Data into Node \n");
scanf("%d",&input);
while (add_value(data_list_ptr,input)==1)
{
printf("Please enter Data into Node \n");
scanf("%d",&input);
}
break;
}
case '2': { printf("Printing Nodes \n"); print_nodes(data_list);break; }
case '3': { printf("Printing Last Node : %d \n",get_last_value(data_list));break; }
case '4': { printf("printing First Node : %d \n",get_first_value(data_list));break; }
case '5': { printf("Printing Biggest Node Value : %d \n",the_biggest(data_list));break; }
case '6': { printf("Printing Number of Nodes : %d \n",get_items_number(data_list));break; }
case '7': { printf("Now Exiting , thank you for using HW_4! \n"); exit(1);break; }
}
}
return 0;
}
void menu(void)
{
printf("Press 1 to add the node to the linked list\n");
printf("Press 2 to print the nodes\n");
printf("Press 3 for the last node\n");
printf("Press 4 for the first node\n");
printf("Press 5 for the biggest number\n");
printf("Press 6 for the number of nodes in list\n");
printf("Press 7 to exit\n");
}
int get_first_value(MID_LIST parm)
{
return parm.head->data;
}
int get_last_value(MID_LIST parm)
{
return parm.end->data;
}
int get_items_number(MID_LIST parm)
{
return parm.size;
}
int the_biggest(MID_LIST parm)
{
LIST_NODE* ptr=NULL;
int max;
ptr=parm.head;
max=ptr->data;
while (ptr!=NULL)
{
if (ptr->data>max) max=ptr->data;
ptr=ptr->next;
}
return max;
}
int add_value(MID_LIST_PTR list, int new_info)
{
LIST_NODE *temp;
temp=(LIST_NODE*)malloc(sizeof(LIST_NODE));
if (temp==NULL)
{
printf("\n Not enough memory for the allocation");
exit(1);
}
if (new_info!=list->end->data)
{
temp->data=new_info;
temp->next=NULL;
if (list->head==NULL) list->head=temp;
else list->end->next=temp;
list->end=temp;
list->size+=1;
return 1;
}
return 0;
}
void print_nodes(MID_LIST parm)
{
int i=1;
LIST_NODE* ptr=NULL;
ptr=parm.head;
while (ptr!=NULL)
{
printf("Node numder %d - data is %d : \n",i,ptr->data);
ptr=ptr->next;
i++;
}
}
because the keyboard buffer still contains the <Enter> key -- you need to flush that out. One way is like below, which will remove up to 3 keys.
char inbuf[4];
menu();
fgets(inbuf,sizeof(inbuf),stdin);
//scanf("%c",&choise);
choise = inbuf[0];
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.