Basic Linked List operations in C - c

I am creating a program to perform basic linked list operations. Right now i have wrote the code only for inserting the node at the front. I ran my program to see its working or not but the program is terminating after accepting the input for the node, then it prints the message after switch. It doesn't even pause for accepting my input for continuing the operations (just before end of main())
here is the code :
#include <stdio.h>
#include <stdlib.h>
struct linkedlist
{
int num;
struct linkedlist *next;
};
struct linkedlist *head = NULL;
void display();
void insertBeginning()
{
struct linkedlist *obj;
int no;
obj = (struct linkedlist *)malloc(sizeof(struct linkedlist));
if(obj == NULL)
{
printf("\n Overflow ");
}
else
{
printf("\n Enter the number = ");
scanf("%d", &no);
obj->num = no;
if(head == NULL)
{
head = obj;
obj->next = NULL;
}
else
{
obj->next = head;
head = obj;
}
}
}
void display ()
{
struct linkedlist *head2 = head;
while(head2 != NULL)
{
printf("%d ->",head2->num);
head2=head->next;
}
printf("NULL \n");
}
int main()
{
int choice;
char wish;
printf("\n 1. Insert at beginning");
printf("\n 2. Insert at end");
printf("\n 3. Insert in between");
printf("\n 4. Delete from front");
printf("\n 5. Delete from end");
printf("\n 6. Delete from in between");
printf("\n 7. Reverse");
printf("\n 8. Sort ascending");
printf("\n 9. Sort descending");
printf("\n 10.Swap alternate elements");
printf("\n 11.Display\n\n");
do
{
printf("\n Enter the option = ");
scanf("%d", &choice);
switch(choice)
{
case 1:
insertBeginning();
break;
case 2:
// insertEnd();
break;
case 3:
// insertInbetween();
break;
case 4:
// deleteFront();
break;
case 5:
// deleteEnd();
break;
case 6:
// deleteInbetween();
break;
case 7:
// Reverse();
break;
case 8:
// sortAsc();
break;
case 9:
// sortDesc();
break;
case 10:
// swap();
break;
case 11:
display();
break;
default:
printf("\n Wrong choice ");
}
printf("\n Do you wish to continue (y/n) = ");
scanf ("%c",&wish);
}while(wish == 'y' || wish =='Y');
return 0;
}

In your case, you have to change
scanf ("%c",&wish);
to
scanf (" %c",&wish);
because, if you don't include the leading white-space before the format specifier, it will consider the remaining \n (newline) which got generated and stored into the input buffer by pressing ENTER key after the first input. So, the second scanf() won't wait for the user input.

when calling scanf()
1) with a '%d' format specifier, the trailing newline, from where the user entered the number, will not be consume.
2) with a '%c' format specifier, leading white space, like a newline, will cause the scanf() to fail, leaving the parameter (wish) unchanged.
3) in the posted code, when the 'wish' does not contain a valid 'Y' or 'y' then the program exits.
I agree with the other poster, that adding a choice '0' for exiting would be a much better way than the separate call to scanf()

there is a new line after giving the input 'choice' which scan by the variable 'wish'. So we need to remove that newline ('\n').
So if you want the user to continue just use a getchar() before take the input wish. Its easy and simple.
printf("\n Do you wish to continue (y/n) = ");
getchar();
scanf ("%c",&wish);

Related

switch case not working with character value if i input char value the switch doesn't encounter default and starts linfinite loop

---------- > ## Heading ## > when i input a character as an input default case of switch doesn't encounter and loop started > infinite times > > > enter code here
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void create();
void display();
void search();
struct node {
int data;
struct node* link;
};
struct node* head;
int main()
{
int value;
while (1) {
printf("Enter Correct Choice :- \n");
printf("Enter 1 to Create Linklist :- \n");
printf("Enter 2 to Display Linklist :- \n");
printf("Enter 3 to Search Linklist :- \n");
printf("Enter Your Choice Here _________ ");
scanf(" %d", &value);
switch (value) {
case 1:
create();
break;
case 2:
display();
break;
case 3:
search();
break;
default:
printf("Error !! Wrong Choice :- \n");
break;
}
}
return 0;
}
TL;DR- Always check the return value of scanf() for success.
In case of a non-numeric character input, the format specifier %d does not find a match, and no input is consumed (i.e., the invalid input remains in the input buffer). Thus, the switch body executes, most likely it does not find a match with any existing case statement, so the default case statement(s) get executed, and control goes back to while loop.
Then, due to the presence of the invalid input in the buffer (and not getting consumed), the above phenomena keeps on repeating.
The major problem is, in case of scanf() failure, the value of variable value remains uninitialized and indeterminate. It does not construct a well-defined program.
Couple of things:
Always initialize local variables.
Always check for success of scanf() before using the returned value, if you have to use scanf(). For better use fgets() to take user input.
In case of failure of input using scanf(), clean up the input buffer before trying to read next input.
The break causes the program to exit the switch case but not the while. To wxit the while as well you should do something like this:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int value;
int done = 0;
while (!done) {
printf("Enter Correct Choice :- \n");
printf("Enter 1 to Create Linklist :- \n");
printf("Enter 2 to Display Linklist :- \n");
printf("Enter 3 to Search Linklist :- \n");
printf("Enter Your Choice Here _________ ");
scanf(" %d", &value);
switch (value) {
case 1:
printf("do stuff\n");
break;
case 2:
printf("do stuff\n");
break;
case 3:
printf("do stuff\n");
break;
default:
printf("Error !! Wrong Choice :- \n");
done = 1;
break;
}
}
return 0;
}
Here I used a variable initialized to 0 which indicates that the operation is not completed yet. When it's time to exit, the variable is set to 1, which causes the program to exit the while loop
Also, always remember to check the return value of printf(), to avoid possible errors
There is no break condition for while().
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void create();
void display();
void search();
struct node {
int data;
struct node* link;
};
struct node* head;
int main()
{
int value = 1;
while (value) {
printf("Enter Correct Choice :- \n");
printf("Enter 1 to Create Linklist :- \n");
printf("Enter 2 to Display Linklist :- \n");
printf("Enter 3 to Search Linklist :- \n");
printf("Enter 0 to exit :- \n");
printf("Enter Your Choice Here _________ ");
scanf(" %d", &value);
switch (value) {
case 1:
create();
break;
case 2:
display();
break;
case 3:
search();
break;
default:
printf("Error !! Wrong Choice :- \n");
break;
}
}
return 0;
}
if a non valid integer is enter for scanf(" %d", &value); value is not set and the invalid input is not flush => scanf will never block nor update value
you need to check scanf returns 1 (you read 1 value) and if not to flush the invalid input for instance reading all the line
so you can replace
scanf(" %d", &value);
switch (value) {
...
}
by
if (scanf("%d", &value) != 1) {
puts("invalid input");
while ((value = getchar()) != '\n') {
if (value == EOF) {
puts("EOF, exit");
exit(-1);
}
}
}
else {
switch(value) {
...
}
}
of course you can also manage the invalid input in your default case forcing an invalid value :
if (scanf("%d", &value) != 1) {
while ((value = getchar()) != '\n') {
if (value == EOF) {
puts("EOF, exit");
exit(-1);
}
}
value = -1; /* any value except 1,2 or 3 */
}
switch(value) {
...
}
Out of that you have no option to stop the execution, you can do :
...
puts("Enter 4 to exit :-);
...
switch (value) {
...
case 4:
exit(0);
...
}
I belive your intention is to run the program as long as "0" is not entered, but ter is no case for "0". also when we have scanf for %d and entering a "char" instead scanf will not read the char from buff. data on value will not get changed and it will keep printing existing data. (garbage if we enter invalid data first time itself, or any entered data.)
used fgets to read the input data, and do a scanf from buf, even when data is incorrect we are clearing the std input. so program will not get in to a infinite loop with scanf failure.
initilised "value = 0"
added case for "0".
scanf is replaced with fget +sscanf
#include <stdio.h>
#include <stdlib.h>
int main()
{
int value = 0;
char buff[256] = {0};
while (1) {
value = -1;
printf("Enter Correct Choice :- \n");
printf("Enter 1 to Create Linklist :- \n");
printf("Enter 2 to Display Linklist :- \n");
printf("Enter 3 to Search Linklist :- \n");
printf("Enter Your Choice Here _________ ");
fgets(buff, 255, stdin);
sscanf(buff, "%d", &value);
switch (value) {
case 1:
//create();
printf("case : 1\n");
break;
case 2:
//display();
printf("case : 2\n");
break;
case 3:
//search();
printf("case : 3\n");
break;
case 0 :
printf("case : 0 : Exiting program\n");
return 0;
default:
printf("Error !! Wrong Choice :- %d\n", value);
break;
}
}
return 0;
}

Missed scanf and function goes on without it. If I add a space still doesn't work

#include <stdio.h>
struct mychar {
char value;
struct mychar *nextPtr;
};
typedef struct mychar Mychar;
void instructions();
void append(Mychar **, char );
void printlist(Mychar *);
int main(){
instructions();
Mychar *startPtr = NULL;
unsigned int choice;
char newchar;
do {
scanf("%d",&choice);
switch (choice) {
case 1:
printf("\nWrite the character you want to add.");
printf("\n> ");
scanf(" %c", &newchar);
append(&startPtr, newchar);
printlist(startPtr);
break;
case 2:
break;
default:
printf("\nError, try again.\n");
//main();
instructions();
break;
}
} while (choice!=3);
printf("\n\nEnd of run.\n");
}
void instructions(){
printf("\nSelect operation. 1 to add, 2 to remove, 3 to exit.");
printf("\n> ");
}
void append(Mychar **sPtr, char newvalue){
Mychar *newlinkPtr = calloc (1, sizeof(Mychar));
newlinkPtr->value = newvalue;
newlinkPtr->nextPtr = NULL;
Mychar *previousPtr = NULL;
Mychar *currentPtr = *sPtr;
while(currentPtr!=NULL && newvalue > currentPtr->value){
previousPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
}
if (previousPtr){
previousPtr->nextPtr = newlinkPtr;
newlinkPtr->nextPtr = currentPtr;
} else {
*sPtr = newlinkPtr;
}
}
void printlist(Mychar *currentPtr){
printf("\n\nCurrent list:\n");
while (currentPtr!=NULL){
printf("%c", currentPtr->value);
currentPtr = currentPtr->nextPtr;
}
}
Why do I have this behaviour? If I run the program, after I enter 1, it prints the "current list" and leave the scanf input opened, so I can enter the value only after "current list" printed. Also, "current list" should be called only after I enter the character with scanf, since the function printlist is AFTER the scanf... but actually this is what happens:
Select operation. 1 to add, 2 to remove, 3 to exit.
> 1
Write the character you want to add.
> a
Current list:
ab
Write the character you want to add.
>
Current list:
abc
Write the character you want to add.
>
Current list:
abcd
Write the character you want to add.
>
Current list:
abcd
The lesson to take form this is to always check scanf for 0 return, at the very least, EOF check is also advised, and act accordingly, as for the order of events of your code, it's not quite there, with some tweaks you can have a nice, bad input proof, I/O sequence:
void clear_stdin() { //stdin buffer clearing function
int c;
while ((c = getchar()) != '\n' && c != EOF){}
}
do {
instructions(); //move inside the loop, user will be prompted in each cycle
while (scanf("%d", &choice) == 0) {
printf("\nError, try again.\n");
instructions();
clear_stdin(); // if input fails clear the buffer
}
clear_stdin(); // clear the buffer for 1hjh type input
switch (choice) {
case 1:
printf("\nWrite the character you want to add.");
printf("\n> ");
while (scanf(" %c", &newchar) == 0) { //this can be a pattern
clear_stdin(); //see #ismick comment
} //
clear_stdin(); //
append(&startPtr, newchar);
printlist(startPtr);
break;
case 2:
break;
case 3:
printf("\n\nEnd of run.\n"); //if you dont have a case default will catch 3
break;
default:
printf("\nError, try again.\n");
break;
}
} while (choice != 3);

While / Switch Loop Implementation

When I run this code and I hit one of the switch's breaks (excluding case 'x') the first printf statement outputs twice. I'm not sure why this happens. Wondering if anybody can see it right from the code without needing the functions to actually run the program.
int main(void)
{
int key;
char command, word[STRINGMAX];
struct data_node *first=NULL, *ptr, *new_node;
while (command)
{
printf("Enter a list command (+-flx): ");
scanf("%c", &command);
switch(command)
{
case '+' :
printf("'+' detected\n");
printf("Enter key data: ");
scanf("%d", &key);
printf("What string to store?: ");
scanf("%s", &word);
first = ptr = insert(&first, key, word);
break;
case '-' :
printf("'-' detected\n");
printf("Enter key data: ");
scanf("%d", &key);
delete(&first, key);
break;
case 'f' :
printf("'f' detected\n");
printf("Enter a key data: ");
scanf("%d", &key);
find_node(first, key);
break;
case 'l' :
printf("'l' detected\n");
dump_list(first);
break;
case 'x' :
printf("Goodbye.\n");
exit(0);
default :
break;
}
}
return (0);
}
Any help appreciated. Thanks.
Add a getchar() after reading a number and a string as scanf leaves the new line character in the input stream. It is preferable to use fgets().
Vivek

Why is my static stack not working?

static stack implementation
this is also not deleting according to the lifo principle
static stack implementation:
it is not taking name for the second time
this is the new code now tell me why is it not working
please help
typedef struct student {
char name[20];
int roll;
int age;
} mystruct;
#define size 40
int top;
static mystruct s[size];
void push()
{
if (top == size - 1) {
printf("\noverflow"); //
} else {
printf("\nenter the name of the student");
gets(s[top].name);//not taking name for d 2 time
printf("\nenter the roll number");
scanf("%d", &s[top].roll);
printf("\nenter the age of the student");
scanf("%d", &s[top].age);
++top;
}
}
void pop()
{
if (top == -1)
{
printf("\nunderflow");
} else {
printf("%s", s[top].name);
printf("%d", s[top].roll);
printf("%d", s[top].age);
printf("\npopped");
--top;
}
}
void display()
{
int i;
if (top == -1) {
printf("\nstack is empty");
} else {
for (i = top; i > 0; i--) {
printf("\nthe name of the student is%s", s[top].name);
}
printf("\nthe roll no of the student is%d", s[top].roll);
printf("\nthe age of the student is%d", s[top].age);
}
}
main()
{
top = -1;
char ch;
while (1) {
printf("\nwelcome to static stack menu");
printf("\n1.PUSH\n2.POP\n3.DISPLAY\n0.EXIT");
printf("\nplease enter your choice\n");
ch = getche();
if (ch == '0') {
break;
}
switch (ch) {
case '1':
push();
break;
case '2':
pop();
break;
case '3':
display();
break;
default:
printf("choice not valid");
break;
}
}
}
The first problem I noticed was that top is initialized to -1. Trying to access the member data of s[top] when top is initialized to -1 will result in unpredictable behavior.
I would suggest changing the line
top = -1;
to
top = 0;
That changes the basic assumption you have made in push, pop, and display about when the stack is empty and when it is full. Instead of checking if ( top == -1 ), you have to now check if (top == 0 ). Instead of checking if ( top == size - 1 ), you have to now check if ( top == size ).
In pop, you have to use top-1 instead of top.
The for loop in display is not scoped correctly. You need to use:
for (i = top-1; i >= 0; i--) {
printf("\nthe name of the student is %s", s[i].name);
printf("\nthe roll no of the student is %d", s[i].roll);
printf("\nthe age of the student is %d", s[i].age);
}
Also, reading the options for the menu and reading the subsequent input is little bit tricky.
After you read the menu option, you have to make sure that you eat up all the input until the next newline. Otherwise, gets() will read everything after your menu option until the end of the line. If you typed 1 for the menu and then typed Return/Enter, the name will be automatically accepted as "\n". Hence, I suggest the lines:
printf("\nwelcome to static stack menu");
printf("\n1.PUSH\n2.POP\n3.DISPLAY\n0.EXIT");
printf("\nplease enter your choice\n");
ch = fgetc(stdin);
/* Skip till the end of line is read. */
while ( fgetc(stdin) != '\n' );
Also, after you read the age of the object, you have to eat everything up to the newline. Otherwise, the newline character is read in as the choice for the next menu option.
scanf("%d", &s[top].age);
/* Skip till the end of line is read. */
while ( fgetc(stdin) != '\n' );
Here's the fully working file. I have replaced gets by fgets and getche by fgetc.
#include <stdio.h>
#include <string.h>
typedef struct student {
char name[20];
int roll;
int age;
} mystruct;
#define size 40
int top;
static mystruct s[size];
void push()
{
if (top == size) {
printf("\noverflow"); //
} else {
printf("\nenter the name of the student: ");
fgets(s[top].name, 20, stdin);//not taking name for d 2 time
// The newline character is part of s[top].name when fgets is
// finished. Remove that.
s[top].name[strlen(s[top].name)-1] = '\0';
printf("\nenter the roll number: ");
scanf("%d", &s[top].roll);
printf("\nenter the age of the student: ");
scanf("%d", &s[top].age);
/* Skip till the end of line is read. */
while ( fgetc(stdin) != '\n' );
++top;
}
}
void pop()
{
if (top == 0)
{
printf("\nunderflow");
} else {
printf("%s, ", s[top-1].name);
printf("%d, ", s[top-1].roll);
printf("%d", s[top-1].age);
printf("\npopped");
--top;
}
}
void display()
{
int i;
if (top == 0) {
printf("\nstack is empty");
} else {
for (i = top-1; i >= 0; i--) {
printf("\nthe name of the student is %s", s[i].name);
printf("\nthe roll no of the student is %d", s[i].roll);
printf("\nthe age of the student is %d", s[i].age);
}
}
}
main()
{
top = 0;
char ch;
while (1) {
printf("\nwelcome to static stack menu");
printf("\n1.PUSH\n2.POP\n3.DISPLAY\n0.EXIT");
printf("\nplease enter your choice\n");
ch = fgetc(stdin);
/* Skip till the end of line is read. */
while ( fgetc(stdin) != '\n' );
if (ch == '0') {
break;
}
switch (ch) {
case '1':
push();
break;
case '2':
pop();
break;
case '3':
display();
break;
default:
printf("choice, %c, not valid", ch);
break;
}
}
}
You need to change getche() to getchar()
Note: getche() is a non-standard function.
Maybe this will be useful http://www.delorie.com/djgpp/doc/libc/libc_385.html
Pay attention to implementation note:
"If you can detect the situation when one of the conio functions is called for the very first time since program start, you could work around this problem by calling the gppconio_init function manually"
or just replace it with getchar(). And there meaned conio included.

switch case not working properly in C

In my program there is a minor problem.
When I press 2 or 3 or 4 it will display the properly but after that when I
press the a or b or c etc., it will display previous one result instead of print the Invalid option.
How can I fix this?
#include <stdio.h>
#include <string.h>
typedef struct vehicle
{
char name[100];
char lice_no[25];
int vehicle_type;
char cmpny_name[100];
int menu_year;
}record;
int main(void)
{
int i,choice;
FILE *fp1,*fp2;
char oname[100];
record det,det1;
int recsize;
char c;
fp1 = fopen("record.dat" , "r+");
if(fp1 == NULL)
{
fp1 = fopen("record.dat" , "w+");
if(fp1 == NULL)
{
printf("error in opening file : \n");
return -1;
}
}
recsize = sizeof(det);
do
{
printf("\t\"enter the choice\"\n");
printf("1 : adding the record\n");
printf("2 : delete the record\n");
printf("3 : editing the record\n");
printf("4 : display the record\n");
printf("5 : exit the program\n");
fflush(stdin);
scanf("%d" , &choice);
scanf("%c" , &c);
switch(choice)
{
case 1 :
{
printf("In this add logic\n")
break;
}
case 2 :
{
printf("In this case delete logic\n");
break;
}
case 3 :
{
printf("In this case edit logic\n");
break;
}
case 4 :
{
printf("display logic\n");
break;
}
case 5 :
{
printf("exit logic\n");
break;
}
default :
{
printf("\"Invalid option\"\n");
break;
}
}
}
while(1);
return 0;
}
It looks like you are getting the numbers into Choice, and the chars into c.
but you are only using the Choice var in the switch, you are never checking the C var.
So essentially, if you hit a letter it is storing it in the C var, and then using the old value in Choice again.
Hmm, one of the things wrong in your codes is the:
scanf("%c" , &c);
because, the scanf function requires the user to press the enter key before it could store the character to its respective variable.
So if the compiler reads the line:
scanf("%c" , &c);
it reads your input, PLUS, the ENTER.
Thus forcing the scanf function to store your input, PLUS, the ENTER, into your character variable.
It would be better if you would use getche() or getch() instead of using scanf() function, and please do not ever use:
scanf("%c" , &c);
because it would generate an error.
Sample of usage of getche() or getch() function:
c=getche(); //waits for a keypress and stores it on a variable
c=getch(); //waits for a keypress and stores it on a variable
the difference between the two is that the getche() displays your keypress while the getch() does not.
Note: Do not forget to put
#include<conio.h>
Added info:
If you still want to go on using the scanf() function just make sure that you declare your favorite variable as:
char c[20];
then you may use:
scanf("%s", &c);
but your variable, can only hold up to 19 characters, as how we declared on your character array.
And the summary is do not use:
scanf("%c", &c);
because, it can affect your other scanf() functions. :)
SOLUTION(Spoiler Warning):
#include <stdio.h>
#include <string.h>
#include <conio.h>
typedef struct vehicle
{
char name[100];
char lice_no[25];
int vehicle_type;
char cmpny_name[100];
int menu_year;
}record;
int main(void)
{
int i; //removed choice from int
FILE *fp1,*fp2;
char oname[100];
record det,det1;
char choice; //made the variable choice a character
int recsize;
char c;
fp1 = fopen("record.dat" , "r+");
if(fp1 == NULL)
{
fp1 = fopen("record.dat" , "w+");
if(fp1 == NULL)
{
printf("error in opening file : \n");
return -1;
}
}
recsize = sizeof(det);
do
{
printf("\t\"enter the choice\"\n");
printf("1 : adding the record\n");
printf("2 : delete the record\n");
printf("3 : editing the record\n");
printf("4 : display the record\n");
printf("5 : exit the program\n");
fflush(stdin);
choice = getche(); // or getch()
switch(choice) //changed the target character
{
case '1' : //changed the case from 1 to '1'
{
printf("In this add logic\n");
break;
}
case '2' : //changed the case from 2 to '2'
{
printf("In this case delete logic\n");
break;
}
case '3' : //changed the case from 3 to '3'
{
printf("In this case edit logic\n");
break;
}
case '4' : //changed the case from 4 to '4'
{
printf("display logic\n");
break;
}
case '5' : //changed the case from 5 to '5'
{
printf("exit logic\n");
break;
}
default :
{
printf("\"Invalid option\"\n");
break;
}
}
}
while(1);
return 0;
}
You can also use switch to compare characters. Just change the values
case 1:
to
case '1':
scanf returns you a value, which you don't check.
When used with %d specifier - it should parse an integer. Since you enter a non-integer value - scanf returns you an error code, and choice is unchanged
It's because in c when you are reading characters as integers (scanf("%d" , &choice);) it takes the ascii code of the characters for example a = 97, b = 98 c = 99 d = 100 if you want to read a as 1 b as 2 etc you will have to add some extra code that tells the programme if the number is equal to the ascii code of a b c d or e subtract it with 96 so you get 1,2,3..

Resources