I am a newbie to c. The question is to implement a Reastuarent Management Software using Linkedlist. A user should be able to enter their order and view their order. A user who pays additional amount for fast delivery should be appeared on top of the order list.
My code, while executing shows no erros but while running it shows segmentation fault when I try to enter the second entry or try to display the list.
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct items{
char *item_name;
float cost;
};
struct items array[] = {
{"Pizza",49.9},{"Apples",22.0},{"Oranges",10.5},{"Grapes",3.5},{"Parotta",4.5}
};
struct bill_item{
int bill_no;
char* customer_name;
int order_item_no;
int quantity;
float total;
struct bill_item *next;
};
struct bill_item* head;
int count=0;
void insert_at_start(struct bill_item* node);
void insert_at_end(struct bill_item* node);
void book();
void display_list();
void main(){
int choice;
head = (struct bill_item*)malloc(sizeof(struct bill_item));
head->next = NULL;
while(choice!=3){
printf("\n----------------------------------------------");
printf("\n1.Book\t2.Check Orders\t3.Exit\nEnter option: ");
scanf("%d",&choice);
printf("\n------choice %d",choice);
switch(choice){
case 1: book();
printf("\nItem Purchased!!");
break;
case 2: display_list();
break;
case 3: break;
default: printf("\nEnter the correct option");
break;
}
}
}
void insert_at_start(struct bill_item* node){
if(head->next == NULL){
head->next = node;
}
else{
node->next = head;
head->next = node;
}
}
void insert_at_end(struct bill_item* node){
struct bill_item* ptr = head;
if(head->next==NULL)
head->next = node;
else{
while(ptr->next!=NULL){
ptr = ptr->next;
}
ptr->next = node;
}
}
void book(){
char c;
struct bill_item* node = (struct bill_item*)malloc(sizeof(struct bill_item));
int i=0,choice;
printf("\nMenu");
printf("\n-----------------");
for (i=0;i<5;i++){
printf("\n%d %s : %.2f",i+1,array[i].item_name,array[i].cost);
}
printf("\nEnter choice: ");
scanf("%d",&choice);
printf("\nEnter quantity: ");
scanf("%d",&node->quantity);
node->next = NULL;
count++;
node->bill_no = count;
node->total = array[choice-1].cost*node->quantity;
fflush(stdin);
printf("\nEnter customer name: ");
fgets(node->customer_name,30,stdin);
fflush(stdin);
printf("\nPurchase amount: %.2f \nNeed fast delivery(Extra 100rs will be charges)(y/n)?",node->total);
c = getc(stdin);
fflush(stdin);
if(c=='Y' || c=='y'){
node->total +=100;
printf("\nFast delivery applied\nTotal %.2f",node->total);
insert_at_start(node);
}
else{
printf("\nTotal: %.2f",node->total);
insert_at_end(node);
}
}
void display_list(){
printf("\nBill No\t\tOrder Item\t\tCname\t\tQ\t\tTotal");
struct bill_item* ptr = head;
ptr = ptr->next;
while(ptr->next!=NULL){
printf("\n%d\t\t%d\t\t%s\t\t%d\t\t%.2f",ptr->bill_no,ptr->order_item_no,ptr->customer_name,ptr->quantity,ptr->total);
ptr = ptr->next;
}
}
I have tried to find the errors. But can't point out any. The errror is somewhere at the functions insert_at_end() and insert_at_start()
In display_list(), if head->next is NULL, you try to read the ptr->next without first checking if it's null:
ptr = ptr->next;
while(ptr->next != NULL) {
// ...
}
This will segfault because ptr is null.
Related
So I had a problem with the "String name" I fixed it, now I would like to
add a function that's removes the node by its ID, so I tried to make a function that locates the node with the requested ID, It did not go well because it does not track the ID I entered.
Here is the full code:
#include <stdlib.h>
#include <stdio.h>
typedef struct stringData {
int id;
char *name;
int payment;
struct stringData *next;
} Node;
Node *addEmployee(int id, char *name, int payment) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->id = id;
newNode->name = strdup(name);
newNode->payment = payment;
newNode->next = NULL;
return newNode;
}
void insert(Node **link, Node *newNode) {
newNode->next = *link;
*link = newNode;
}
void removeEmployee(Node *head, int id){
while (head != NULL){
head = head->next;
if(head ->id == id){
printf("The employee exists.");
}
printf("The employee does not exists.");
}
}
void printList(Node *head) {
while (head != NULL) {
printf("----------------------------------\n");
printf("Name: %s\n", head->name);
printf("ID: %d\n", head->id);
printf("Payment: %d\n", head->payment);
printf("----------------------------------\n");
head = head->next;
}
}
int main(void)
{
int id;
char name[42];
int payment;
int input;
Node *head = NULL;
Node *tail = NULL;
Node *n;
n = addEmployee(1, "Dor", 5000);
insert(&head, n);
tail = n;
n = addEmployee(2, "David", 10000);
insert(&head, n);
tail = n;
printList(head);
removeEmployee(head, 2);
while((input != 4)){
printf("\nPlease select 1 option:\n1) Add an employee - type 1\n2) Remove an employee - type 2\n3) Show a list of employees - type 3\n4) Exit - type 4\n");
scanf("%d", &input);
switch(input){
case 1:
printf("\nPlease enter the employee's id: ");
scanf("%d", &id);
printf("\nPlease enter the employee's name: ");
scanf("%41s",name);
printf("\nPlease enter the employee's payment: ");
scanf("%d", &payment);
n = addEmployee(id, name, payment);
insert(&head, n);
tail = n;
break;
case 2:
printf("Please enter the ID of the employee you would like to remove: ");
scanf("%d", &id);
break;
case 3:
printList(head);
break;
case 4:
printf("Good-bye");
break;
default:
printf("This is not an option!");
break;
}
}
return 0;
}
I do not know how to get into it, I'm not that familiar with this subject so It would be very helpful to also see some examples.
im doing a project, where i need to manage a queue of sorts, specifically cars in a car wash. I've found some code online, that allows you to add to queue and manage it, with integer inputs. Is there a way for me to re-write this code, so that it accepts inputs as c strings instead of integers?
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front = NULL;
struct node *rear = NULL;
void display();
void enqueue(int);
void dequeue();
int main()
{
int n, ch;
do
{
printf("\n\nQueue Menu\n1. Add \n2. Remove\n3. Display\n0. Exit");
printf("\nEnter Choice 0-3? : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter number ");
scanf("%d", &n);
enqueue(n);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
}
}while (ch != 0);
}
void enqueue(int item)
{
struct node *nptr = malloc(sizeof(struct node));
nptr->data = item;
nptr->next = NULL;
if (rear == NULL)
{
front = nptr;
rear = nptr;
}
else
{
rear->next = nptr;
rear = rear->next;
}
}
void display()
{
struct node *temp;
temp = front;
printf("\n");
while (temp != NULL)
{
printf("%d\t", temp->data);
temp = temp->next;
}
}
void dequeue()
{
if (front == NULL)
{
printf("\n\nqueue is empty \n");
}
else
{
struct node *temp;
temp = front;
front = front->next;
printf("\n\n%d deleted", temp->data);
free(temp);
}
}
Where the desired input would be one of these strings;
AV96888 VW alm
KD65656 Audi luksus
AX21878 Ford alm
CN32323 VW alm
NB21214 Ford luksus
UM21878 Ford alm
AV54361 Tesla luksus
Yes, the data is stored in the node:
struct node
{
int data;
struct node *next;
};
So you just need to change the int data; to a string.
Are there a reason you are doing this in C and not c++ ? There, string management is easier.
Here is the answer, you just convert the data to char * and use strdup to store the pointer
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
struct node
{
char *data;
struct node *next;
};
struct node *front = NULL;
struct node *rear = NULL;
void enqueue(char *item)
{
struct node *nptr = malloc(sizeof(struct node));
nptr->data = strdup(item);
nptr->next = NULL;
if (rear == NULL)
{
front = nptr;
rear = nptr;
}
else
{
rear->next = nptr;
rear = rear->next;
}
}
void display()
{
struct node *temp;
temp = front;
printf("\n");
while (temp != NULL)
{
printf("%s\t", temp->data);
temp = temp->next;
}
}
void dequeue()
{
if (front == NULL)
{
printf("\n\nqueue is empty \n");
}
else
{
struct node *temp;
temp = front;
front = front->next;
printf("\n\n%d deleted", temp->data);
free(temp->data);
free(temp);
}
}
int main()
{
int ch;
char n[256];
do {
printf("\n\nQueue Menu\n1. Add \n2. Remove\n3. Display\n0. Exit");
printf("\nEnter Choice 0-3? : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter a name: ");
scanf("%s", n);
enqueue(n);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
}
}while (ch != 0);
}
In this programm, when I create a list then it creates list successfully. But when I try to print it the program is just displaying last two values of node. I debug many times and found (*temp)->next changes the start1 and start2 pointer. I'm unable to solve how temp pointer changes value in start1 and start2.
Compiler didn't produce any error or warning.
#include <stdio.h>
struct node {
int info;
struct node *next;
} *start1 = NULL, *start2 = NULL;
void create_node(struct node **s1);
void display(struct node **s);
void create_node(struct node **s1)
{
struct node *ptr = NULL, **temp = s1;
ptr = (struct node *)malloc(sizeof(struct node));
if (*s1 == NULL)
{
*s1 = ptr;
} else
{
while ((*temp)->next != NULL)
(*temp) = (*temp)->next;
(*temp)->next = ptr;
}
ptr->next = NULL;
printf("enter the value\n");
scanf("%d", &(ptr->info));
}
void display(struct node **s)
{
struct node **temp = s;
while ((*temp) != NULL)
{
printf("%d\t", (*temp)->info);
(*temp) = (*temp)->next;
}
}
void main()
{
int choice = 0;
while (1){
clrscr();
printf("enter your choice\n");
printf("enter 1 to create_node1\nenter 2 to create node 2 \nenter 3 to display node 1\nenter 4 to display node2\nenter 5 to exit\n");
printf("\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
create_node(&start1);//correct//
break;
case 2:
create_node(&start2);
break;
case 3:
display(&start1);
getch();
break;
case 4:
display(&start2);
getch();
break;
case 5:
exit(1);
break;
default:
printf("invalid");
}
}
}
Use this
while ((*temp)->next != NULL) (*temp) = (*temp)->next;.
This will solve the problem.
Otherwise, the index out of memory is accessed and program crash.
This question already has an answer here:
Delete element of linked list by a certain criterion
(1 answer)
Closed 7 years ago.
So the problem I'm running into is deleting a user-inputted string from a linked list full of strings.
I'm still having a few issues in understanding just precisely how linked lists work, so any explanation as to what I am doing wrong would be greatly appreciated!
Also, every other function seems to be working just fine, just having issues with the deleteItem function!
Edit - The problem I'm getting when I run the deleteItem function is just my terminal window crashing after getting hung up for a bit.
Here's my code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
char name[50];
struct node *next;
}*head;
void display();
void insert();
void count();
void deleteItem();
int main(){
int choice = 1;
char name[50];
struct node *first;
head = NULL;
while(1){
printf("Menu Options\n");
printf("----------------\n");
printf("Please enter the number of the operation you'd like to do: \n");
printf("----------------\n");
printf("1. Insert\n");
printf("2. Display\n");
printf("3. Count\n");
printf("4. Delete\n");
printf("5. Exit\n");
printf("----------------\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
count();
break;
case 4:
if(head=NULL)
printf("The list is blank");
else
deleteItem();
break;
case 5:
return 0;
default:
printf("invalid option");
}
}
system("pause");
return 0;
}
void insert(){
char nameToInsert[50];
struct node *temp;
temp = head;
if(head == NULL){
head = (struct node *)malloc(sizeof(struct node));
printf("What's the name you wish to insert?\n");
scanf("%s", &nameToInsert);
strcpy(head->name, nameToInsert);
head->next = NULL;
}
else{
while(temp->next !=NULL){
temp = temp->next;
}
temp->next = malloc(sizeof(struct node));
temp = temp->next;
printf("What's the name you wish to insert?\n");
scanf("%s", &nameToInsert);
strcpy(temp->name, nameToInsert);
temp->next = NULL;
}
}
void display(){
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
temp = head;
if(temp == NULL)
printf("The list is empty\n");
else{
printf("%s\n", temp->name);
while(temp->next != NULL){
temp = temp->next;
printf("%s\n", temp->name);
}
}
}
void count(){
struct node *temp;
int c =0;
temp = head;
while(temp!=NULL){
temp=temp->next;
c++;
}
printf("\n%d", c);
}
void deleteItem(){
char nameToDelete[50];
struct node *temp, *previous;
temp = head;
printf("What is the name you wish to delete?\n");
scanf("%s", nameToDelete);
while(temp->next != NULL){
temp = temp->next;
if(strcmp(nameToDelete, temp->name)==0){
previous = temp->next;
free(temp);
printf("%s was deleted successfully\n", nameToDelete);
}
}
}
I see the following issues:
You are not dealing with the case where the item to delete is at the head of the list.
The linked list is not restored to a good state after you free the node that contains the item.
You continue to iterate over the list even after you free the node that contains the item.
Here's a version that should work.
void deleteItem(){
char nameToDelete[50];
struct node *temp, *previous;
temp = head;
printf("What is the name you wish to delete?\n");
scanf("%s", nameToDelete);
// First, locate the node that contains the item.
for ( ; temp->next != NULL; temp = temp->next )
{
if(strcmp(nameToDelete, temp->name)==0)
{
break;
}
prev = temp;
}
// Now take care of deleting it.
if ( temp != NULL )
{
if ( temp == head )
{
head = temp->next;
}
else
{
prev->next = temp->next;
}
free(temp);
printf("%s was deleted successfully\n", nameToDelete);
}
}
I can create the linked list. But I could not managed to create the stack with it. (Stack cannot be more than 5, and it can be empty as shown in the link). How can I do it? (C language but C++ functions like new int are allowed)
The structure could be something like:
struct linkedStack {
int elements[5];
int top;
struct linkedStack *next;
};
Then manage the stack with top (equals to zero at the beginning)...
Post your code please so we can understand what you are trying to make.
This example can help you
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
#include<alloc.h>
void Push(int, node **);
void Display(node **);
int Pop(node **);
int Sempty(node *);
typedef struct stack {
int data;
struct stack *next;
} node;
void main() {
node *top;
int data, item, choice;
char ans, ch;
clrscr();
top = NULL;
printf("\nStack Using Linked List : nn");
do {
printf("\n\n The main menu");
printf("\n1.Push \n2.Pop \n3.Display \n4.Exit");
printf("\n Enter Your Choice");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the data");
scanf("%d", &data);
Push(data, &top);
break;
case 2:
if (Sempty(top))
printf("\nStack underflow!");
else {
item = Pop(&top);
printf("\nThe popped node is%d", item);
}
break;
case 3:
Display(&top);
break;
case 4:
printf("\nDo You want To Quit?(y/n)");
ch = getche();
if (ch == 'y')
exit(0);
else
break;
}
printf("\nDo you want to continue?");
ans = getche();
getch();
clrscr();
} while (ans == 'Y' || ans == 'y');
getch();
}
void Push(int Item, node **top) {
node *New;
node * get_node(int);
New = get_node(Item);
New->next = *top;
*top = New;
}
node * get_node(int item) {
node * temp;
temp = (node *) malloc(sizeof(node));
if (temp == NULL)
printf("\nMemory Cannot be allocated");
temp->data = item;
temp->next = NULL;
return (temp);
}
int Sempty(node *temp) {
if (temp == NULL)
return 1;
else
return 0;
}
int Pop(node **top) {
int item;
node *temp;
item = (*top)->data;
temp = *top;
*top = (*top)->next;
free(temp);
return (item);
}
void Display(node **head) {
node *temp;
temp = *head;
if (Sempty(temp))
printf("\nThe stack is empty!");
else {
while (temp != NULL) {
printf("%d\n", temp->data);
temp = temp->next;
}
}
getch();
}