I'm reading in from a text file likewise:
George Washington, 2345678
John Adams, 3456789
Thomas Jefferson, 4567890
James Madison, 0987654
James Monroe, 9876543
John Quincy Adams, 8765432
Andrew Jackson, 7654321
Martin Van Buren, 6543210
William Henry Harrison, 5432109
John Tyler, 4321098
The function to delete the name works, however when it is successful the printf statements just continue to loop in the command window. I tried using a break statement at the end of the loop, however that only led to saying that the name wasn't found. Can someone offer any insight?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Creates node for holding student's information
struct node
{
char name [50];
int id;
struct node *next;
}*head;
//Create Function Prototypes
void readDataFile ();
void insert(char *inName, char *inID);
void display(struct node *d);
int deleteID(int num);
void deleteName(char *delete_name);
//Main function
int main()
{
//Declare variables
int i, num, delete_id, id;
char *name;
char nameDelete [50];
char nameInsert [50];
struct node *n;
//initialize link list
head = NULL;
//Read in file
readDataFile();
//Create list of operations utilized in program
while (1)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Delete by ID\n");
printf("4.Delete by Name\n");
printf("5.Exit\n");
printf("Enter your choice : ");
if(scanf("%d", &i) <= 0)
{
printf("Enter only an Integer\n");
exit(0);
}
else
{
switch(i)
{
case 1:
getchar();
printf("Enter the name to insert:");
scanf("%[^\n]s", nameInsert);
printf("\nEnter the ID associated with the name: ");
scanf("%d", &id);
break;
case 2:
if (head == NULL)
printf("List is Empty\n");
else
{
printf("Elements in the list are:\n");
}
display(n);
break;
case 3:
if(head == NULL)
printf("List is Empty\n");
else
{
printf("Enter the ID number to delete: ");
scanf("%d", &delete_id);
}
if(deleteID(delete_id))
printf("%d deleted successfully \n", delete_id);
else
printf("%d not found in the list\n", delete_id);
break;
case 4:
getchar();
if(head == NULL)
printf("List is Empty\n");
else
{
printf("Enter name to delete: ");
scanf("%[^\n]s", nameDelete);
printf("Checking for name %s...\n", nameDelete);
printf("%s not found in the list\n", nameDelete);
deleteName(nameDelete);
}
break;
case 5:
return 0;
default:
printf("Invalid option\n");
}
}
}
return 0;
}
//Define the functions
//Function to delete by name
void deleteName(char *delete_name)
{
//Create temporary and helper node
struct node *temp, *helper;
//Set temp equal to head
temp = head;
//Loop until the end of the list
while(temp != NULL)
{
if(strcmp(temp->name, delete_name) == 0)
{
if(temp == head)
{
head = temp->next;
free(temp);
printf("Found %s!\n", delete_name);
printf("%s deleted successfully\n", delete_name);
}
else
{
helper->next = temp->next;
free(temp);
printf("Found %s!\n", delete_name);
printf("%s deleted successfully\n", delete_name);
}
}
else
{
helper = temp;
temp = temp->next;
}
}
break;
}
Please learn how to make an MCVE (How to create a Minimal, Complete, and Verifiable Example?) or SSCCE (Short, Self-Contained, Correct Example) — two names and links for the same basic idea.
Here's an MCVE derived from your code. I added the missing break; or return; from the loop in deleteName(). I rewrote main() essentially completely, but it works cleanly:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
char name[50];
int id;
struct node *next;
} *head;
void deleteName(char *delete_name);
int main(void)
{
struct node *n;
head = NULL;
head = malloc(sizeof(*head));
assert(head != 0);
strcpy(head->name, "Abraham Lincoln");
head->id = 1;
head->next = 0;
n = malloc(sizeof(*n));
strcpy(n->name, "George Washington");
n->id = 2;
n->next = head;
head = n;
n = malloc(sizeof(*n));
strcpy(n->name, "John Adams");
n->id = 3;
n->next = head;
head = n;
deleteName("George Washington");
deleteName("John Adams");
deleteName("Abraham Lincoln");
return 0;
}
void deleteName(char *delete_name)
{
struct node *temp, *helper = 0;
temp = head;
while (temp != NULL)
{
if (strcmp(temp->name, delete_name) == 0)
{
if (temp == head)
{
head = temp->next;
free(temp);
printf("Found %s!\n", delete_name);
printf("%s deleted successfully\n", delete_name);
}
else
{
helper->next = temp->next;
free(temp);
printf("Found %s!\n", delete_name);
printf("%s deleted successfully\n", delete_name);
}
return; // The key change!
}
else
{
helper = temp;
temp = temp->next;
}
}
}
This ran cleanly under valgrind and Mac OS X 10.10.2 with GCC 4.9.1.
Found George Washington!
George Washington deleted successfully
Found John Adams!
John Adams deleted successfully
Found Abraham Lincoln!
Abraham Lincoln deleted successfully
It is important to learn how to be brutal about stripping out irrelevant code when creating an MCVE.
The break keyword will break out of the nearest switch or loop. Therefore, you can try this:
while(temp != NULL)
{
if(strcmp(temp->name, delete_name) == 0)
{
if(temp == head)
{
head = temp->next;
free(temp);
printf("Found %s!\n", delete_name);
printf("%s deleted successfully\n", delete_name);
break;
}
else
{
helper->next = temp->next;
free(temp);
printf("Found %s!\n", delete_name);
printf("%s deleted successfully\n", delete_name);
break;
}
}
else
{
helper = temp;
temp = temp->next;
}
}
An alternate solution is to set temp to NULL after you free it (this is good practice anyway, so that might be another idea)
Add the return statement at the end of the if block once you have found the name to be deleted
void deleteName(char *delete_name)
{
//Create temporary and helper node
struct node *temp, *helper;
//Set temp equal to head
temp = head;
//Loop until the end of the list
while(temp != NULL)
{
if(strcmp(temp->name, delete_name) == 0)
{
if(temp == head)
{
head = temp->next;
free(temp);
printf("Found %s!\n", delete_name);
printf("%s deleted successfully\n", delete_name);
}
else
{
helper->next = temp->next;
free(temp);
printf("Found %s!\n", delete_name);
printf("%s deleted successfully\n", delete_name);
}
return;
}
else
{
helper = temp;
temp = temp->next;
}
}
}
Also you how are planning to indicate that the name isn't part of the list, as opposed to being found and deleted. You can change modify the function such that it returns an error code when it hasn't found the name in the list, which is then used to indicate the name was never in the list to begin with.
I did not really look at the delete() function,
however, the following code illustrates how the
code should be formatted, etc.
notice the checking for input errors, which should always be performed
notice the separation of the struct definition from the struct declaration
notice the easy readability of the switch cases
by incorporating some vertical white space
notice the simple comments after the closing braces
notice that no call to an action is performed if the linked list is empty
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//define node for holding student's information
struct node
{
char name [50];
int id;
struct node *next;
};
// create head pointer for linked list of student info
// and initialize
struct node *head = NULL;
// Function Prototypes
// note: if all these functions are only accessed within this file
// then they should be declared with the 'static' modifier
void readDataFile (void);
void insert (char *inName, char *inID);
void display (struct node *d);
void deleteID (int delete_ID);
void deleteName (char *delete_name);
//Main function
int main()
{
//Declare local variables
// notice use of meaningful names
// notice, for readability and documentation,
// only one variable declared per line
int i; // receives user menu selection input
int idDelete;
int idInsert;
char nameDelete [50];
char nameInsert [50];
// struct node *n; // unused variable
int done = 0; // used to exit when user enters '5'
//Read in file
readDataFile();
//Create list of operations utilized in program
while (!done)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Delete by ID\n");
printf("4.Delete by Name\n");
printf("5.Exit\n");
printf("Enter your choice : ");
if( 1 != scanf("%d", &i) )
{ // then, scanf failed
perror( "scanf for choice failed");
exit( EXIT_FAILURE );
}
// implied else, scanf successful
switch(i)
{
case 1:
int ch;
while(EOF != (ch = getchar()) && (ch != '\n'));
printf("Enter the name to insert:");
if( 1 != (scanf("%[^\n]s", nameInsert) ) )
{ // then scanf failed
perror( "scanf for new student name failed");
exit( EXIT_FAILURE );
}
// implied else, scanf successful
printf("\nEnter the ID associated with the name: ");
if( 1 != (scanf("%d", &idInsert) ) )
{ // then scanf failed
perror( "scanf for new student ID failed");
exit( EXIT_FAILURE );
}
// implied else, scanf successful
insert( nameInsert, idInsert );
break;
case 2:
if (head == NULL)
printf("List is Empty\n");
else
{
printf("Elements in the list are:\n");
display(n);
} // end if
break;
case 3:
if(head == NULL)
printf("List is Empty\n");
else
{
printf("Enter the ID number to delete: ");
if( 1 != (scanf("%d", &idDelete) ) )
{ // then, scanf failed
perror( "scanf for ID to delete failed");
exit( EXIT_FAILURE );
}
// implied else, scanf successful
deleteID(idDelete);
} // end if
break;
case 4:
int ch;
while(EOF != (ch = getchar()) && (ch != '\n'));
if(head == NULL)
printf("List is Empty\n");
else
{
printf("Enter name to delete: ");
if( 1 != (scanf("%[^\n]s", nameDelete) ) )
{ // then, scanf failed
perror( "scanf for name to delete failed");
exit( EXIT_FAILURE );
}
// implied else, scanf successful
printf("Checking for name %s...\n", nameDelete);
deleteName(nameDelete);
} // end if
break;
case 5:
done = 1; // this will cause while() loop to exit
break;
default:
printf("Invalid option\n");
break;
} // end switch
} // end while
return 0;
} // end of function: main
Related
I need to make a function that removes elements of a linked list (elements of the list are words) that contain a letter the user inputs. Basically, I have a .txt file that has random words in it, after loading the words into the linked list, I need to make several functions (this post is only dedicated for the 4th function from the menu), one of which is the function I'm having trouble with.
My idea was to make a separate function that will send 1 if the word contains the letter and 0 if it does not, then I made a function that goes through my linked list and using the first function checks if that element of the list contains the word, if it does then I remove it and move onto the next element.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Element Element;
struct Element
{
char word[50];
Element *next;
};
Element *load_file(Element *head)
{
char file[500];
scanf("%s", file);
Element *temp = head;
FILE* fp = fopen(file, "r");
if(fp == NULL)
{
printf("Error while loading file.");
return 0;
}
while(!feof(fp))
{
Element *newE = (Element*)malloc(sizeof(Element));
fscanf(fp, "%s", newE->word);
newE->next = NULL;
if(!temp)
{
head = temp = newE;
}
else
{
temp->next = newE;
temp = newE;
}
}
fclose(fp);
printf("\nFile loaded successfully\n");
return head;
}
int frequency(Element *head, char *word)
{
Element *temp = head;
int counter = 0;
while(temp != NULL)
{
if(strcmp(temp->word, word) == 0)
{
counter++;
}
temp = temp->next;
}
return counter;
}
int contains(char word[], char letter)
{
int end = strlen(word);
int flag = 0;
for(int i = 0; i < end-1; i++)
{
if(letter == word[i])
{
flag = 1;
break;
}
}
return flag;
}
Element *delete_word(Element *head, char letter)
{
Element *temp = head;
Element *before = NULL;
Element *newHead = head;
while(temp != NULL)
{
if(contains(temp->word, letter))
{
if(before == NULL)
{
newHead = temp->next;
free(temp);
temp = newHead;
}
else
{
before->next = temp->next;
free(temp);
temp = before->next;
}
}
else
{
before = temp;
temp = temp->next;
}
}
return newHead;
}
void printElement(Element *element)
{
printf("%s \n", element->word);
}
void printList(Element *head)
{
Element *temp = head;
while(temp != NULL)
{
printElement(temp);
temp = temp->next;
}
}
void meni()
{
printf("************** MENI **************\n");
printf("1. Loading text from file \n");
printf("2. Print all elements of the list \n");
printf("3. Frequency of a certain word \n");
printf("4. Delete words that contain a letter \n");
printf("5. Load the list into a file \n");
printf("6. Exit \n\n");
}
int main()
{
Element *head = NULL;
int option;
while(1)
{
meni();
scanf("%d", &option);
switch(option)
{
case 1:
{
printf("Input a name of the file: \n");
head = load_file(head);
break;
}
case 2:
{
printList(head);
break;
}
case 3:
{
char word[100];
printf("Input a word: ");
scanf("%s", word);
int num = frequency(head, word);
printf("%d\n", num);
break;
}
case 4:
{
char word[100];
printf("Input a word: ");
scanf("%s", word);
head = delete_word(head, word);
printList(head);
break;
}
case 5:
{
}
case 6:
{
return 0;
}
}
}
return 0;
}
For some reason, it makes no changes on the list whatsoever.
Your delete_word() function is declared as:
Element *delete_word(Element *head, char letter);
But you call it with a char*:
char word[100];
head = delete_word(head, word);
The menu option says Delete words that contain a letter but when the user selects that option, he/she is instructed to Input a word: (confusing).
You then send the address of the first char in word to the function. From that address one char is picked. It'll be random at best and it will most probably corrupt the stack (since you, by picking one char only, probably pick 1 byte out of 4 or 8).
You could make it work like this:
case 4:
{
char word[100];
printf("Input characters: ");
if(scanf("%s", word)==1) {
int len = strlen(word);
for(int i=0; i<len; ++i) {
head = delete_word(head, word[i]);
}
}
printList(head);
break;
}
This would allow the user to enter some characters and all words containing any of those would be removed.
I have been getting Thread 1 : EXC_BAD_ACCESS error when trying to push a string element into the stack. I've changed the char* name to char name[21] but the assignment char[21] is not assignable to curr->name. Also I've tried fgets but the error still there. Anyone know where the error?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct parts
{
char* name;
char* type;
int quantity;
int price;
int num;
struct parts * next;
}*head,*tail,*curr,komponen;
void menu();
void show_Parts();
void push(char* nm, char* tip, int jml, int harga, int nom);
void del();
int main() {
menu();
del();
getchar();
return 0;
}
void push(char* nm, char* tip, int jml, int harga, int nom)
{
// 1.
curr = (struct parts *)malloc(sizeof(struct parts));
// 2.
if (curr == NULL) {
exit(0);
}
head = NULL;
head = curr->next;
curr->name = nm;
curr->type = tip;
curr->quantity = jml;
curr->price = harga;
curr->num = nom;
curr = head;
//
printf("Input name of the new item [3..20]: ");
scanf("%s",nm);
if (strlen(nm) < 3 || strlen(nm) > 20) {
printf("Length of name must between 3 and 20");
exit(0);
}
printf("Input type of the new item [processor/graphic card/memory]: ");
scanf("%s",tip);
if (tip != "processor" || tip != "graphic card" || tip != "memory") {
printf("Input type of existing item. Error");
exit(0);
}
printf("Input quantity of the new item [1..20]: ");
scanf("%d",&jml);
if (jml < 1 || jml > 20) {
printf("Quantity between 1 and 20\n");
exit(0);
}
printf("Input price of new item [$1..$1000]: ");
scanf("%d",&harga);
if (harga < 1 || harga > 1000) {
printf("Price between 1 and 1000\n");
exit(0);
}
nom++;
printf("--- Add New Item Success ---\n");
tail->next = NULL;
}
void del()
{
if (curr == NULL) {
exit(0);
}
curr = head;
head = head->next;
free(curr);
}
void menu()
{
int choic;
do {
printf("BLUE COMPUTER ADMINISTRATOR\n");
printf("++++++++++++++++++++++++++++\n");
printf("1. Item list\n");
printf("2. Add <PUSH> New item\n");
printf("3. Exit\n");
printf(">> Input your choice : ");
switch (choic) {
case 1:
show_Parts();
break;
case 2:
push(komponen.name,komponen.type,komponen.price, komponen.quantity, komponen.num);
break;
case 3:
del();
exit(0);
break;
}
scanf("%d",&choic);
} while (choic != 3);
}
void show_Parts()
{
if (curr == NULL) {
printf("No item in list\n");
exit(0);
}
printf(" ---- ITEM LIST ---- \n\n");
printf("-----+------------------+--------+-----------+--------\n");
printf("|No. | Name | Type | Quantity | Price|\n");
do {
printf("|%d |%s |%s |%d |%d \n",curr->num, curr->name, curr->type, curr->quantity, curr->price);
curr = curr->next;
}while (curr != NULL);
}
Thanks.
I added a "search" function for my linked list menu and I don't know what is wrong in my code. When I enter a search key which is a name that is not in the list, instead of printing the "Search Key: %s Not Found!", the program will just stop. How to fix it? Any suggestion on how can I improve my program?
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
struct list
{
char name[20];
int age;
char gender[10];
struct list *next;
};
void main(void)
{
struct list *HEAD = NULL;
struct list *temp,*current, *trav;
struct list *prev,*temp1,*temp2;
char choice;
while(1)
{
clrscr();
printf("MENU\n");
printf("A) ADD\n");
printf("B) DISPLAY\n");
printf("C) DELETE\n");
printf("D) SEARCH\n");
printf("X) EXIT\n");
scanf("%c", &choice);
switch(toupper(choice))
{
case 'A':
temp= (struct list*)malloc(sizeof(struct list));
temp->next=NULL;
printf("Fill-Up the following:\n");
printf("Name:");
fflush(stdin);
gets(temp->name);
printf("Age:");
fflush(stdin);
scanf("%d",&temp->age);
printf("Gender:");
fflush(stdin);
gets(temp->gender);
if(HEAD == NULL)
{
HEAD = temp;
}
else if(HEAD!=NULL)
{
for(trav=HEAD; trav->next != NULL; trav= trav->next);
trav->next=temp;
}
else
{
printf("Not Enough Memory!\n");
}
break;
case 'B':
if(HEAD==NULL)
{
printf("Linked List is Empty!\n");
getch();
}
else{
for(trav=HEAD; trav != NULL; trav=trav->next )
{
printf("\nName: %s\n", trav->name);
printf("Age: %d\n", trav->age);
printf("Gender: %s\n\n", trav->gender);
}
getch();
}
break;
case 'C' :
temp1=( struct list*)malloc(sizeof(struct list));
temp1->next=NULL;
if(HEAD==NULL)
{
printf("No item to be delete. List is Empty!\n");
getch();
}
else {
printf("Enter The Name of the item you want to Delete: ");
fflush(stdin);
gets(temp1->name);
current=HEAD;
if(strcmp(temp1->name,current->name)== 0)
{
HEAD=HEAD->next;
free(current);
printf("Item has been successfully deleted from the list.\n");
getch();
}
else
{
for(prev=HEAD,trav=HEAD->next; strcmp(trav->name,temp1->name) == 1 ; trav=trav->next,prev=prev->next);
if(trav==NULL)
{
printf("Name: %s not found!", temp1->name);
getch();
}
else{
prev->next=trav->next;
free(trav);
printf("Item has been successfully deleted from the list.\n");
getch();
}
}
}
break;
case 'D':
temp2=( struct list*)malloc(sizeof(struct list));
temp2->next=NULL;
if(HEAD==NULL)
{
printf("No item to search. List is Empty.\n");
getch();
}
else{
printf("Enter Name (Search Key): ");
fflush(stdin);
gets(temp2->name);
int count=0;
struct list *trav2=HEAD;
while( trav2 !=NULL)
{
for(struct list *trav1=trav2; strcmp(trav1->name,temp2->name)!=0;trav1=trav1->next);
if(trav1!=NULL)
{
printf("\nName: %s\n", trav1->name);
printf("Age: %d\n", trav1->age);
printf("Gender: %s\n",trav1->gender);
trav2=trav1->next;
count++;
}
else {
trav2=NULL;
}
}
getch();
if(count==0)
{
printf("Search Key: %s Not Found!\n", temp2->name);
getch();
}
}
break;
case 'X':
if(HEAD!=NULL){free(HEAD); }
if(trav!=NULL){ free(trav); }
if(trav1!=NULL){ free(trav1); }
if(trav2!=NULL){ free(trav2); }
if(temp!=NULL){ free(temp); }
if(temp1!=NULL){ free(temp1); }
exit(1);
break;
}
}
}
In your search routine..
for(struct list *trav1=trav2; strcmp(trav1->name,temp2->name)!=0;trav1=trav1->next);
here trav1 will be null at the end of list but you are still going ahead and derefrencing it.
Add a check temp1 in your for loop like this:
for(struct list *trav1=trav2; trav1 && (strcmp(trav1->name,temp2->name)!=0);trav1=trav1->next);
Or, for better readability:
for (struct list *trav1 = trav2; trav1 != NULL; trav1 = trav1->next)
{
if (strcmp(trav1->name, temp2->name) !=0 )
break;
}
A few other comments on your code:
Do not use fflush(stdin) use something like this since that is not guaranteed by the standard to work.
int c;
while ((c = getchar()) != EOF && c != '\n')
;
Stop using gets(). It is impossible to use gets() safely. Use fgets() instead (but be aware it keeps the newline where gets() discards it).
Your loop continues while while( trav2 !=NULL). But where do you set trav2 = trav2->next? Without that, you loop will just continue forever.
But your code seems to have more problems than just that. Why are you allocating a new list item? Just declare a pointer to a list item (struct list*) and then point it to the head of your list.
for (temp = HEAD; temp != NULL; temp = temp->next)
{
// Examine temp for the value you are looking for
}
I suspect you are still new to understanding pointers. That is key to getting this code working right.
The problem was solved. A guy gave it in comments. The problem was that I was using %d to read in a short int. I should have used %hd or I should have used an `int'.
I tried to create a program of singly-linked list using only local variables. I was able to make a working program by using global variables.
The program with local variables compiles but it crashes when I try to traverse the linked list.
I have absolutely no idea what is wrong with the implementation with local variables. What is the problem present in the Implementation with local variables?
ABOUT THE STRUCTURE OF THE PROGRAMS:
I understand that the programs are big so I'll put in something about structure of the program.
The program is structured as a menu driven program. So the initial calls to functions are in main() function
There are 3 options in main() menu - exit, traverse and insertion
Exit returns 0 to exit program while other 2 do function calls
Insertion function itself is arranged as menu-driven program.
It has 3 options - return , insert_begin and insert_end. The last 2 are function calls.
I know there are memory leaks as I haven't freed any memory but I will take care of that after I can understand the problem in the current program.
//WORKING IMPLEMENTATION USING GLOBAL VARIABLE
#include<stdio.h>
#include<stdlib.h>
#define MIN 0
#define MAX 2
#define INS_MIN 0
#define INS_MAX 2
typedef struct node
{
int data;
struct node *next;
}sll_node;
sll_node *start = NULL;
void intro()
{
system("cls");
printf("\n\tThese are the various options:\n");
printf("\n\t00 Exit");
printf("\n\t01 Traverse the list");
printf("\n\t02 Insertion into the list");
}
void insert_begin()
{
sll_node *node = malloc(sizeof(sll_node));
if(node == NULL)
{
printf("\n\tNot enough menory");
exit(-1);
}
int data;
printf("\n\tData to be entered: ");
scanf("%d", &data);
node->data = data;
node-> next = start;
start = node;
}
void insert_end()
{
sll_node *node = malloc(sizeof(sll_node));
if(node == NULL)
{
printf("\n\tNot enough menory");
exit(-2);
}
if(start == NULL)
insert_begin();
else
{
printf("\n\tData to be entered: ");
scanf("%d", &(node->data));
node-> next = NULL;
sll_node *node2;
for(node2 = start; node2->next != NULL; node2 = node2->next)
;
node2->next = node;
}
}
void insert_intro()
{
system("cls");
printf("\n\tThese are the various options:\n");
printf("\n\t00 Insertion Done");
printf("\n\t01 Insert at beginning");
printf("\n\t02 Insert at end");
}
void insertion()
{
short choice;
while(1)
{
choice = -1;
while(choice < INS_MIN || choice > INS_MAX)
{
insert_intro();
printf("\n\n\tEnter your chocie: ");
scanf("%d", &choice);
}
switch(choice)
{
case 0:
return;
case 1:
insert_begin();
break;
case 2:
insert_end();
break;
}
}
}
void traverse()
{
if(start == NULL)
printf("\n\n\tLinked list is empty");
else
{
printf("\n\n\t");
for(sll_node *node = start; node != NULL; node = node->next)
printf("%d ", node->data);
}
getch();
}
int main()
{
short choice;
while(1)
{
choice = -1;
while(choice < MIN || choice > MAX)
{
intro();
printf("\n\n\tEnter your choice: ");
scanf("%d", &choice);
}
switch(choice)
{
case 0:
return 0;
case 1:
traverse();
break;
case 2:
insertion();
break;
}
}
return 0;
}
//COMPILES BUT CRASHES - Same program but with local variable start and variable passing between functions
#include<stdio.h>
#include<stdlib.h>
#define MIN 0
#define MAX 2
#define INS_MIN 0
#define INS_MAX 2
typedef struct node
{
int data;
struct node *next;
}sll_node;
void intro()
{
system("cls");
printf("\n\tThese are the various options:\n");
printf("\n\t00 Exit");
printf("\n\t01 Traverse the list");
printf("\n\t02 Insertion into the list");
}
sll_node* insert_begin(sll_node *start)
{
sll_node *node = malloc(sizeof(sll_node));
if(node == NULL)
{
printf("\n\tNot enough menory");
exit(-1);
}
int data;
printf("\n\tData to be entered: ");
scanf("%d", &data);
node->data = data;
node-> next = start;
return node;
}
sll_node* insert_end(sll_node *start)
{
sll_node *node = malloc(sizeof(sll_node));
if(node == NULL)
{
printf("\n\tNot enough menory");
exit(-2);
}
if(start == NULL)
start = insert_begin(start);
else
{
printf("\n\tData to be entered: ");
scanf("%d", &(node->data));
node-> next = NULL;
sll_node *node2;
for(node2 = start; node2->next != NULL; node2 = node2->next)
;
node2->next = node;
}
return start;
}
void insert_intro()
{
system("cls");
printf("\n\tThese are the various options:\n");
printf("\n\t00 Insertion Done");
printf("\n\t01 Insert at beginning");
printf("\n\t02 Insert at end");
}
sll_node* insertion(sll_node *start)
{
short choice;
while(1)
{
choice = -1;
while(choice < INS_MIN || choice > INS_MAX)
{
insert_intro();
printf("\n\n\tEnter your chocie: ");
scanf("%d", &choice);
}
switch(choice)
{
case 0:
return start;
case 1:
start = insert_begin(start);
break;
case 2:
start = insert_end(start);
break;
}
}
}
void traverse(sll_node *start)
{
if(start == NULL)
printf("\n\n\tLinked list is empty");
else
{
printf("\n\n\t");
for(sll_node *node = start; node != NULL; node = node->next)
printf("%d ", node->data);
}
getch();
}
int main()
{
sll_node *start = NULL;
short choice;
while(1)
{
choice = -1;
while(choice < MIN || choice > MAX)
{
intro();
printf("\n\n\tEnter your choice: ");
scanf("%d", &choice);
}
switch(choice)
{
case 0:
return 0;
case 1:
traverse(start);
break;
case 2:
start = insertion(start);
break;
}
}
return 0;
}
You are not returning anything from insertion() function when item is added to a list. So linked list may not get constructed properly.
Probably, you should return start only when its added at the beginning, otherwise start in main() will not point to head of the list.
sll_node* insertion(sll_node *start)
{
...
switch(choice)
{
case 0:
return start;
case 1:
start = insert_begin(start);
return start; //<----- return node
break;
case 2:
start = insert_end(start);
break;
}
...
}
Change short choice to int choice.
Why does this make a difference?
Short answer is that printf("%d") expects an integer.
The long answer is "%d" describes the data type you are passing to printf as an integer (which is commonly 4 to 8 bytes), and you're giving it a datatype of short - which is commonly 2 bytes long. When your program reads the input and stores it at the pointer, &choice, it writes 4 bytes starting at that address (but only 2 were reserved). This causes a segmentation fault and will crash your program.
Here's a list to some printf documentation. You'll notice that to pass a short to printf you would write %hd instead of %d
When i compile your code on my computer, it works, but i changed "short choice" to "int choice", because scanf("%d", &choice) takes 4 bytes to write on, and when choice is short it crashes, because short has only 2 bytes, therefore stack corruption will occur, my be on your computer this corruption damage the "start" pointer.
About the crash. Change the argument start in both functions insert_begin and insert_end to sll_node ** start, and when assigning new value, use the expression *start = your-new-value. It is because you have to pass a pointer to the local variable start which is also pointer. You do not need to change function traverse.
About memory leaks, let me to point-out that when you call insert_begin from inside insert_end, the node created from insert_end is left unused. before exit() and the return in main() you should free the list.
Yes, sorry. There was another bug hard to see. It was at 2 lines where you read (choice).
short choice;
...
// It is ERROR to use "%d" with (short choice), because the stack will
// be overwritten with unsuspected results. The format specifier "%hd"
// say to compiler that (&choice) point to a short 16-bit integer,
// not 32-bit
scanf("%hd", &choice);
This is slightly different version, tested, without memory leaks.
//
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define MIN 0
#define MAX 2
#define INS_MIN 0
#define INS_MAX 2
typedef struct node
{
int data;
struct node *next;
} sll_node;
void clear_list(sll_node** start)
{
assert(start != NULL);
sll_node* node = *start;
while (node != NULL)
{
sll_node* element = node;
node = element->next;
free(element);
}
*start = NULL;
}
void intro()
{
system("cls");
printf("\n\tThese are the various options:\n");
printf("\n\t00 Exit");
printf("\n\t01 Traverse the list");
printf("\n\t02 Insertion into the list");
}
void insert_begin(sll_node** pstart)
{
sll_node* node = (sll_node*)malloc(sizeof(sll_node));
if (node == NULL)
{
printf("\n\tNot enough menory");
clear_list(pstart);
exit(-1);
}
int data;
printf("\n\tData to be entered: ");
scanf_s("%d", &data);//scanf
node->data = data;
node->next = *pstart;
// update the local variable start passed from main to point just inserted node
*pstart = node;
}
void insert_end(sll_node** start)
{
assert(start != NULL);
if (*start == NULL)
{
insert_begin(start);
}
else
{
sll_node* node = (sll_node*)malloc(sizeof(sll_node));
if (node == NULL)
{
printf("\n\tNot enough menory");
clear_list(start);
exit(-2);
}
printf("\n\tData to be entered: ");
scanf("%d", &(node->data));
node->next = NULL;
sll_node* node2;
for(node2 = *start; node2->next != NULL; node2 = node2->next)
;
node2->next = node;
}
}
void insert_intro()
{
system("cls");
printf("\n\tThese are the various options:\n");
printf("\n\t00 Insertion Done");
printf("\n\t01 Insert at beginning");
printf("\n\t02 Insert at end");
}
void insertion(sll_node** start)
{
short choice;
while(1)
{
choice = -1;
while(choice < INS_MIN || choice > INS_MAX)
{
insert_intro();
printf("\n\n\tEnter your chocie: ");
scanf("%hd", &choice);
}
switch(choice)
{
case 0:
return;
case 1:
insert_begin(start);
break;
case 2:
insert_end(start);
break;
}
}
}
void traverse(sll_node *start)
{
if (start == NULL)
printf("\n\n\tLinked list is empty");
else
{
printf("\n\n\t");
for(sll_node *node = start; node != NULL; node = node->next)
printf("%d ", node->data);
}
getch();
}
int main()
{
sll_node *start = NULL;
short choice;
while(1)
{
choice = -1;
while(choice < MIN || choice > MAX)
{
intro();
printf("\n\n\tEnter your choice: ");
scanf("%hd", &choice);
}
switch(choice)
{
case 0:
clear_list(&start);
return 0;
case 1:
traverse(start);
break;
case 2:
insertion(&start);
break;
}
}
return 0;
}
P.S. Very hard to edit! I'm new here and do not have enough experience. Wasted a lot of time to edit!
I am coding for a simple linked list , but facing a little problem. The program is like it is accepting name, age and DOB through user, and memory for it is dynamically allocated. After taking data from the user, it is searching a name, asked by user, if the name exists, it should print all the details related to it.
Here is my code-
//function declarations
struct node *initnode(char *, char *, char *);
void add(struct node *);
struct node *printnode(struct node *);
struct node *searchname(struct node *, char *);
struct node {
char name[25];
char age[10];
char dob[10];
struct node *next;
};
struct node *head = (struct node *) NULL;
struct node *initnode(char *name, char *age, char *dob1)
{
struct node *ptr;
ptr = (struct node *) malloc(sizeof(struct node));
if (ptr == NULL)
return (struct node *) NULL;
else {
strcpy(ptr->name, name);
strcpy(ptr->age, age);
strcpy(ptr->dob, dob1);
ptr->next = NULL;
return ptr;
}
}
struct node *printnode(struct node *ptr)
{
printf("Name -> %s\n", ptr->name);
printf("age -> %s \n", ptr->age);
printf("dob ->%s\n", ptr->dob);
return ptr;
}
void add(struct node *newp)
{
struct node *temp = (struct node *) malloc(sizeof(struct node));
if (head == NULL)
head = newp;
else {
for (temp = head; temp->next != NULL; temp = temp->next);
temp->next = newp;
temp = newp;
}
free(temp);
}
struct node *searchname(struct node *ptr, char *name1)
{
if (strcmp(name1, ptr->name) == 0) {
printf("\n name found \n");
printf("\n details are -\n");
printnode(ptr);
return ptr;
} else {
printf("\n name not found in the database \n");
}
}
int main()
{
char name[25];
char name1[25];
char rep;
char age[10];
char dob[10];
int i;
int flag = 1;
struct node *ptr;
do {
fflush(stdin);
while (flag != 0) {
printf("Enter name -- ");
gets(name);
for (i = 0; name[i] != '\0'; i++)
if (isdigit(name[i])) {
printf("Error in user input, name should be in alphabets\n");
flag = 1;
break;
}
else
flag = 0;
}
flag = 1;
while (flag != 0) {
printf("Enter age -- ");
scanf("%s", &age);
fflush(stdin);
for (i = 0; age[i] != '\0'; i++)
if (isalpha(age[i])) {
printf("Error in user input, age should be in numbers\n");
flag = 1;
break;
} else {
flag = 0;
}
}
flag = 1;
while (flag != 0) {
printf("Enter dob in DD/MM/YY format-- ");
scanf("%s", &dob);
fflush(stdin);
for (i = 0; dob[i] != '\0'; i++) {
if (isalpha(dob[i])) {
printf("Error in user input, dob should be in numbers\n");
flag = 1;
break;
} else
flag = 0;
}
}
flag = 1;
ptr = initnode(name, age, dob);
add(ptr);
printf("\n Do you want to continue?<Y/N>:\n ");
scanf("%s", &rep);
//rep = getchar();
}
while (rep == 'Y' || rep == 'y');
printf("\n do u want to search for a name in the database? <Y/N>:\n");
scanf("%s", &rep);
if (rep == 'Y' || rep == 'y') {
printf("Enter name you want to search-- ");
scanf("%s", &name1);
ptr = searchname(head, name1);
} else {
printf("\n goodbye \n");
}
do {
printf("\n do u want to search again? <Y/N>:\n");
scanf("%s", &rep);
if (rep == 'Y' || rep == 'y') {
printf("Enter name you want to search-- ");
scanf("%s", &name1);
ptr = searchname(head, name1);
} else {
printf("\n goodbye \n");
}
}
while (rep == 'Y' || rep == 'y');
return 0;
}
The issue is that it is searching for the first entry only and not others, can anyone help me to sort out this? I am compiling through "gcc".
At a first glance, your search function is only comparing one element, the head of the list.
After comparing one element you must go to the next element. This can either be done recursively or with a while:
Recursive use:
struct node *searchname(struct node *ptr, char *name1)
{
if (ptr==NULL) //Reached end of the list
{
printf("\n name not found in the database \n");
return NULL;
}
if (strcmp(name1, ptr->name) == 0) { //found the element
printf("\n name found \n");
printf("\n details are -\n");
printnode(ptr);
return ptr;
} else { //search next element
return searchname(ptr->next,name1); //this will call your function again for the next element on the list
}
}
While use:
struct node *searchname(struct node *ptr, char *name1)
{
struct node *aux; // usually i use an auxiliary pointer in order to avoid unwanted overwrite at my pointer.
aux = ptr;
while (aux!=NULL)
{
if (strcmp(name1, aux->name) == 0) { //found the element
printf("\n name found \n");
printf("\n details are -\n");
printnode(aux);
return aux;
}
else { //move pointer to next element
aux=aux->next;
}
}
//if it reaches this point, the element was not found
printf("\n name not found in the database \n");
return NULL;
}
Check your add function, it is wrong. Step mentally through it, and see what happens
to your pointer, and to the memory it points to.
What are the malloc and free used for in the add function?