hey guys so far my code works good but when i enter a code and press '\n' the code statements are executed twice....
Code:
.//Filename: rllst.c
//This program prints a reverse linked list.
//Author: Nyah check, chairman, INK Corp..
//Licence: Freeware
#include <stdio.h>
#include <stdlib.h>
int print_list( struct node);
int insert_list(void);
struct node
{
int num;
struct node *next;
}values;
struct node *first = NULL;
struct node *p, *q;
int main(void)
{
char code;
int n;
printf("\nThis program stores variables in a linked list and prints all of them in reverse order.");
printf("\nPress '0': QUIT, 'c': CREATE LIST, 'p': PRINT REVERSED LIST.");
for ( ; ; )
{
printf("\nEnter code: ");
code = getchar();
getc(stdin);
switch(code)
{
case '0': printf("\nThanks for using our software.");
printf("\n We would like to receive your comments, suggestions to better our products.");
printf("\nINK Corp.. 2012.");
exit(EXIT_SUCCESS);
case 'C':
case 'c': insert_list();
break;
case 'P':
case 'p': print_list(values);
break;
default: printf("\nInvalid Command.");
break;
}
}
return 0;
}
//insert_list(): This function adds variables to a linked list and returns if it was unsuccessful.
int insert_list(void)
{
int n;
struct node *new_node;
new_node =(struct node *)malloc(sizeof(struct node));
if ( new_node == NULL)
{
printf("\nNo memory to add data.");
exit(EXIT_FAILURE);
}
else
printf("\nEnter number: ");
scanf("%d", &n);
new_node->num = n;
p == first;
new_node->next = first;
q = new_node;
return 0;
}
//print_list(): This function prints all the values in the linked list in reverse order.
// starting from the last element.
int print_list( struct node)
{
printf("\nReverse list");
printf("\n-------------");
for ( int i = 1; q != p; q = q->next, i++)
printf("\n%d. %d ",i, q->num);
return 0;
}