Program to print from a queue prints a number instead - c

So here's the code I have at the moment.
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
struct cake {
char name;
int waitTime;
int prepTime;
int bakeTime;
int turnTime;
};
struct cake redVelvet(){
struct cake c;
c.name = "R";
c.waitTime = 0;
c.prepTime = 60;
c.bakeTime = 30;
c.turnTime = 0;
return c;
};
struct Node {
struct cake cake;
struct Node* next;
};
// Two glboal variables to store address of front and rear nodes.
struct Node* front = NULL;
struct Node* rear = NULL;
// To Enqueue an integer
void Enqueue(struct cake x) {
struct Node* temp =
(struct Node*)malloc(sizeof(struct Node));
temp->cake = x;
temp->next = NULL;
if (front == NULL && rear == NULL){
front = rear = temp;
return;
}
rear->next = temp;
rear = temp;
}
// To Dequeue an integer.
void Dequeue() {
struct Node* temp = front;
if (front == NULL) {
printf("Queue is Empty\n");
return;
}
if (front == rear) {
front = rear = NULL;
}
else {
front = front->next;
}
free(temp);
}
struct cake Front() {
if (front == NULL) {
printf("Queue is empty\n");
return;
}
return front->cake;
}
void Print() {
struct Node* temp = front;
while (temp != NULL) {
printf("%d ", temp->cake.name);
temp = temp->next;
}
printf("\n");
}
int main(void){
Enqueue(redVelvet());
Enqueue(redVelvet());
Print();
getchar();
}
So In reality I'm going to have many different cakes, and if certain conditions are meant they are going to be inputted into the queue of LinkedList. However as a sample I created one type of cake (Red Velvet) And added it into the queue via my enqueue function and then tried to print it, However my out put is "88 88"
I want it to print the name of the Cake instead.

Following 2 line of code change needed
c.name = 'R';
Inside print function formatter type should match the type of variable
printf("%c ", temp->cake.name);

Related

How to structure linked list in C with pointers, keep getting error

I'm quite new to C and am still coming to grips with a lot of the syntax and idiosyncrasies. I'm not exactly sure what needs changing in order to make my code work but am open to any ideas. I understand that I need to utilize pointers in order to make this work but I am still lost on the specific implementation. I keep getting an error that my myList function is undeclared but I feel like I have declared it already. Is there something I am missing about how C works? Any help would be appreciated
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node* head;
struct node* next;
}node;
node*linkedList ();
int main ()
{
struct linkedList* myList = (struct createList*)malloc(sizeof(struct node));
myList.addNode(5);
myList.addNode(10);
myList.addNode(13);
printf("%d\n", myList.search(10));
printf("The linked list is this big: %d\n", myList.getSize);
}
node* linkedList ()
{
node* head;
node* current;
node*next;
addNode (int x)
{
node keephead = head;
current = head;
while (current.next = NULL)
{
if (current.next = NULL)
{
current.next = node* newnode
newnode.data = x;
newnode.next = NULL;
newnode.head = keephead
}
if (head = NULL)
{
head = current;
}
}
}
int getSize ()
{
int counter = 0;
node countNodes = head;
while (countNodes.next != NULL)
{
countNodes = countNodes.next;
counter++;
}
return counter;
}
int search(int value)
{
int index = 0;
node searchNode = head;
while(searchNode.next!= NULL)
{
searchNode = searchNode.next;
index++;
if (node.value = data)
{
break;
}
else {
index = -1;
}
}
return index;
}
}
I will be simplifying the explanations, so the terms that I will use might not be the correct one.
Proper and consistent indentation would always make your code easier to read and to fix. There were missing semi-colons too, you should watch out for that.
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node* head;
struct node* next;
} node;
node* linkedList();
int main()
{
struct linkedList* myList = (struct createList*) malloc(sizeof(struct node));
myList.addNode(5);
myList.addNode(10);
myList.addNode(13);
printf("%d\n", myList.search(10));
printf("The linked list is this big: %d\n", myList.getSize);
}
node* linkedList()
{
node* head;
node* current;
node* next;
addNode (int x)
{
node keephead = head;
current = head;
while (current.next = NULL)
{
if (current.next = NULL)
{
current.next = node* newnode;
newnode.data = x;
newnode.next = NULL;
newnode.head = keephead;
}
if (head = NULL)
{
head = current;
}
}
}
int getSize ()
{
int counter = 0;
node countNodes = head;
while (countNodes.next != NULL)
{
countNodes = countNodes.next;
counter++;
}
return counter;
}
int search (int value)
{
int index = 0;
node searchNode = head;
while (searchNode.next != NULL)
{
searchNode = searchNode.next;
index++;
if(node.value = data)
{
break;
}
else
{
index = -1;
}
}
return index;
}
}
In main(), you should add the return 0; at the end of the function as it is an undefined behavior (AKA not a good thing to do). You could also change it to void main(), but it doesn't compile in clang.
printf("%d\n", myList.search(10));
printf("The linked list is this big: %d\n", myList.getSize);
return 0;
}
You can't put a function within a function in C (nested functions). Put addNode(), getSize(), and search() outside the linkedList() function.
node* linkedList()
{
node* head;
node* current;
node* next;
}
addNode (int x)
{
node keephead = head;
...
}
int getSize ()
{
int counter = 0;
...
return counter;
}
int search (int value)
{
int index = 0;
...
return index;
}
linkedList() does literally nothing now and should be removed.
struct node* next;
} node;
int main()
{
struct linkedList* myList = (struct createList*) malloc(sizeof(struct node));
...
printf("The linked list is this big: %d\n", myList.getSize);
return 0;
}
void addNode (int x)
{
node keephead = head;
In main(), myList is the head of the currently empty linked-list, so it should be initialized to NULL. There's no linkedList data type, only node. Change it to:
node* myList = NULL;
You seem to be applying addNode(), getSize(), and search() to a variable, but C doesn't have that feature (C++ have it though). Add the linked-list head as an argument instead. addNode() needs the & address operator since it will be changing where the head starts.
addNode(&myList, 5);
addNode(&myList, 10);
addNode(&myList, 13);
printf("%d\n", search(myList, 10));
printf("The linked list is this big: %d\n", getSize(myList));
Update the function parameters of addNode(). In the while condition current.next = NULL and if statements, you were using an assignment operator instead of a comparison operator != or ==. You used the variables current and newnode, but never declared it anywhere in the function. There were lots of logic errors here. Change it to:
void addNode (node** head, int x)
{
node* current = *head;
node* newnode = malloc(sizeof(node));
newnode->data = x;
newnode->head = *head;
newnode->next = NULL;
if (*head == NULL)
*head = newnode;
else
{
while (current->next != NULL)
current = current->next;
//current is now the last node of linked list
current->next = newnode;
}
}
Do the same for the function parameters of getSize() and search(). Use node * instead for countNodes and searchNode. -> is the operator for accessing the members of a struct pointer. if statement should be put before it goes to the next node, as it would always skip the first node if left as it is. index should be put before the if statement so the index would update before it breaks out of the loop.
int getSize (node* head)
{
int counter = 0;
node* countNodes = head;
while (countNodes != NULL)
{
countNodes = countNodes->next;
counter++;
}
return counter;
}
int search (node* head, int value)
{
int index = 0;
node* searchNode = head;
while (searchNode != NULL)
{
index++;
if(searchNode->data == value)
break;
searchNode = searchNode->next;
}
if(searchNode->data != value)
index = -1;
return index;
}
Add the function prototypes before main().
void addNode (node** head, int x);
int getSize (node* head);
int search (node* head, int value);
int main()
{
node* myList = NULL;
Everything should work properly now.

Polynominal with doubly linked list - pointer problem

I made some polynomial code with a doubly-linked list. for example, if
you write 1 and 2 then 1 is a degree and 2 is coefficient. 1x^2 insert
to doubly linked list. the problem is that when I check my code, the Node
head->degree is changing. if I write 1x^2 then head->degree is 1 next,
I write 2x^1 then head-> degree should maintain 1 but head-> degree
change to 2 I think there is some problem in the head pointer.
#include <stdio.h>
#include <stdlib.h>
// struct
struct Node {
int degree;
int coefficient;
struct Node* next;
struct Node* prev;
};
// global variables
int de; // degree
int co; // coefficient
int flag;
Node** head = (Node**)malloc(sizeof(Node)); //
Node** head1 = (Node**)malloc(sizeof(Node)); //
Node** head2 = (Node**)malloc(sizeof(Node)); //
Node** head3 = (Node**)malloc(sizeof(Node)); //
Node* newNode = (Node*)malloc(sizeof(Node)); //
// function
Node* inputpoly(void);
void printNode(Node* inp);
Node* multiply(Node* a, Node* b);
// main
int main() {
// head null
(*head1) = NULL;
(*head2) = NULL;
(*head3) = NULL;
while (1) {
printf("Input (degree) (coefficient) : ");
scanf_s("%d %d", &de, &co);
if (de * co < 0) { continue; }
if (de < 0 && co < 0) {
printf("Done!\n");
break;
}
*head = inputpoly();
}
printNode(*head);
//multiply(*head1, *head2);
free(head1);
free(head2);
free(head3);
free(newNode);
free(head);
}
Node* inputpoly(void) {
// create Node
newNode->degree = de;
newNode->coefficient = co;
newNode->next = NULL;
newNode->prev = NULL;
// case1
if (flag == 0) {
// list
if ((*head1) == NULL) {
*head1 = newNode;
}
// list x
else {
Node* horse = (*head1);
// front of head
// ------------------There is some problem
printf("%d\n", 1);
printf("--%d\n", newNode->degree);
printf("--%d\n", horse->degree);
if (horse->degree > newNode->degree) {
newNode->next = horse;
horse->prev = newNode;
*head1 = newNode;
}
// barward of head
else {
int num = 0;
while (horse->next != NULL) {
horse = horse->next;
if (horse->degree > newNode->degree) {
horse->prev->next = newNode;
newNode->next = horse;
newNode->prev = horse->prev;
horse->prev = newNode;
num = 1;
break;
}
}
// behind tail
if (num == 0) {
horse->next = newNode;
newNode->prev = horse;
}
}
}
return *head1;
}
}
void printNode(Node* inp) {
Node* horse = inp;
if (horse == NULL)
{
return;
}
while (horse != NULL) {
if (horse->prev == NULL) {
if (horse->degree == 1) {
printf("%d", horse->coefficient);
}
else {
printf("%d x^%d", horse->coefficient, horse->degree);
}
}
else {
if (horse->degree == 1) {
printf(" + %d", horse->coefficient);
}
else {
printf(" + %d x^%d", horse->coefficient, horse->degree);
}
}
}
printf("\n");
}
"i think there is some head pointer problem, and if I fixed it I can this problem. so I want to maintain this code as possible. I want some
advice or solution to my head pointer"
The code posted in your example does not compile:
Before you can fix a head pointer problem the code must compile. This list of errors is detailing 2 things:
first, functions cannot be called outside of a function, eg:
Node** head = (Node**)malloc(sizeof(Node)); //
Node** head1 = (Node**)malloc(sizeof(Node)); //
Node** head2 = (Node**)malloc(sizeof(Node)); //
Node** head3 = (Node**)malloc(sizeof(Node)); //
Node* newNode = (Node*)malloc(sizeof(Node)); //
should be called from within main(void){...} or some other function.
second, every occurrence of Node should be prepended with struct. eg:
struct Node** head = malloc(sizeof(struct Node *));
(have also removed the cast, and modified the size of what you are creating memory for, i.e. a pointer)
Rather then fix these and other problems, here is an example of a doubly linked list that can demonstrate the structure of a simple working program. You can adapt the following to match your needs:
struct Node {
int deg;
int coef;
struct Node* next; // Pointer to next node in DLL
struct Node* prev; // Pointer to previous node in DLL
};
void inputpoly(struct Node** head_ref, int deg, int coef)
{
//allocate node
struct Node *new_node = malloc(sizeof(*new_node));
//assign data
new_node->deg = deg;
new_node->coef = coef;
//set next as new head and prev to null
new_node->next = (*head_ref);
new_node->prev = NULL;
//change prev of head to new */
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
//point head to the new node */
(*head_ref) = new_node;
}
void printList(struct Node* node)
{
struct Node* last;
printf("\nread forward\n");
while (node != NULL) {
printf(" %d,%d ", node->deg,node->coef);
last = node;
node = node->next;
}
printf("\nread reverse\n");
while (last != NULL) {
printf(" %d,%d ", last->deg,last->coef);
last = last->prev;
}
}
int main(void)
{
//start with empty list
struct Node* head = NULL;
//create and populate new nodes
inputpoly(&head, 7, 2);
inputpoly(&head, 1, 4);
inputpoly(&head, 4, 6);
//ouput list
printList(head);
getchar();
return 0;
}
Note that this code is offered as a basic demonstration of creating doubly linked list, and illustrate how to traverse both directions. Because it does not free allocated memory, it is not recommended that it be used for any production purpose without addressing that omission.

Queue resets after every operation

I am working on a queue implementation and something weird is happening. The Enqueue seems to work but the changes are not being registered (the size stays the same at 0). From what I can tell, the code will enqueue an element but immediately forget about it. The output is:
Data passed: a34
Back befor:(null)
Back a:a34
Data passed: bg
Back befor:(null)
Back a:bg
Print (null)
Print 0
Based on this I'm guessing it's something to do with memory and scope, but I'm not sure how to resolve it.Below is the code.
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
typedef struct node_s{
void* data;
struct node_s* next;
} node;
// queue structure
typedef struct queue_s{
node* back;
node* front;
int size;
} queue_t;
//helpers
node *newNode(void *data)
{
//create a new node* with data->data. #return: a node*
node *temp = (node *)malloc(sizeof(node));
temp->data = data;
temp->next = NULL;
return temp;
}
//deep copy a n2
node* copy(node* n2){
node *temp = (node *)malloc(sizeof(node));
temp->data=n2->data;
temp->next=n2->next;
return temp;
}
queue_t que_create(void)
{
queue_t new;
new.size = 0;
node *front = (node *)malloc(sizeof(node));
node *back = (node *)malloc(sizeof(node));
new.front=front;
new.back=back;
return new;
}
void que_destroy(queue_t queue)
{
while (queue.size > 0)
{
que_dequeue(queue);
}
}
void que_clear(queue_t queue)
{
while (que_size(queue) != 0)
{
que_dequeue(queue);
}
}
void que_enqueue(queue_t queue, void *data)
{
// Create a new LL node
printf("Data passed: %s\n",data);
node *temp = newNode(data);
// If queue is empty, then new node is front and back
if (queue.size == 0){
printf("Back befor:%s\n",queue.back->data);
queue.front = copy(temp);
queue.back = copy(temp);
queue.size=queue.size+1;
free(temp);
printf("Back a:%s\n",queue.back->data);
return;
}
// Add the new node at the end of queue
queue.back->next = copy(temp);
queue.back = temp;
printf("Back2:%s\n",queue.back->data);
if (data != NULL){
queue.size=queue.size+1;
}
//free(temp);
}
void que_dequeue(queue_t queue)
{
if (queue.size == 0)
{
printf("Deq on an empty q");
return; //break here
}
else if (queue.size == 1)
{
//only 1 element
queue.front = NULL;
queue.back = NULL;
}
else
{
node* temp = newNode(NULL);
temp=queue.front->next;
queue.front = copy(temp);
free(temp);
}
queue.size=queue.size-1;
}
const void *que_front(const queue_t queue)
{
//return queue.front->data;
return queue.front;
}
size_t que_size(queue_t queue)
{
return (size_t)queue.size;
}
int main(void){
queue_t q=que_create();
que_enqueue(q,"a34");
que_enqueue(q,"bg");
node* f=(node *)que_front(q);
printf("Print %s\n",f->data);
printf("Print %d\n",q.size);
return 0;
}
The question was answered in the comments by #Paul Ogilvie. C is not "pass by value", so the code will edit a copy of the queue not the actual thing. This is fixed by passing a pointer that will change the value at the address

Create two linked lists using one function

I have a function called ll() for creating a linked list as follows. My program requires two linked lists. Is it possible to reuse this function so I can have two linked lists, say head1 and head2?
#include <stdio.h>
#include <malloc.h>
typedef struct node
{
int data;
struct node* link;
} Node;
Node* head = NULL;
Node* previous = NULL;
int main(void)
{
ll();
print();
return 0;
}
int ll()
{
int data = 0;
while(1)
{
printf("Enter data, -1 to stop: ");
scanf("%d", &data);
if(data == -1)
break;
addtoll(data);
}
}
int addtoll(int data)
{
Node* ptr = NULL;
ptr = (Node*)malloc(sizeof(Node));
ptr->data = data;
ptr->link = NULL;
if(head == NULL)
head = ptr;
else
previous->link = ptr;
previous = ptr;
}
int print()
{
printf("Printing linked list contents: ");
Node* ptr = head;
while(ptr)
{
printf("%d ", ptr->data);
ptr = ptr->link;
}
printf("\n");
}
Is there a better way than doing something like
main()
{
ll(1);
ll(2);
}
int ll(int serial)
{
if (serial == 1)
Use head1 everywhere in this function
else if(serial == 2)
Use head2 everywhere in this function
}
Instead of passing an int you could also just pass the linked list.
Node head1;
Node head2;
Node previous1;
Node previous2;
int main(){
ll(&head1, &previous1);
ll(&head2, &previous2);
}
int ll(Node* head, Node* previous)
{
int data = 0;
scanf("%d",&data);
*head = {data, null};
previous = head;
while(1)
{
printf("Enter data, -1 to stop : ");
scanf("%d",&data);
if(data == -1)
break;
addtoll(data, previous);
}
}
int addtoll(int data, Node* previous)
{
struct student newNode = {data, null}
previous->link = &newNode;
previous = &newNode;
}
Exceutable Code to implement more than 1 Linked List by appending new node one by one.

Simplest Linked List Creation and Printing Data

The following is a simple code segment in C to create a linked list and print all elements contained in the list.
User is asked to input integer data till a zero is entered which marks the termination of user input; Once data is saved in the linked list, the program prints all elements stored in the list and then completes its execution.
I can't make it run, every time it gives a "Segmentation fault" error, please check and tell me where I'm wrong (using gcc 4.8.2)
Code :
struct node
{
int data;
struct node * next;
};
struct node * createLinkedList()
{
int x;
struct node * start;
start = NULL;
printf("Input 0 to end, Insert elements :\n");
for(scanf("%d", &x); x ;scanf("%d", &x))
{
struct node * temp = (struct node *) malloc(sizeof(struct node));
if (temp)
{
temp->data = x;
temp->next = NULL;
if(start == NULL) {
start = temp;
} else {
start->next = temp;
start = temp;
}
}
}
return start;
}
void printLinkedList(struct node * start)
{
if (start == NULL) {
printf("Linked List is empty!\n");
} else {
printf("\nPrinting Linked List : \n");
struct node * s;
s = start;
while(s != NULL)
{
printf("%d\n", s->data);
s = s->next;
}
}
}
int main(int argc, char const *argv[])
{
struct node * start;
start = NULL;
start = createLinkedList();
printLinkedList(start);
return 0;
}
This part of code
if(start == NULL) {
start = temp;
} else {
start->next = temp;
start = temp;
}
is invalid. There has to be
if(start == NULL) {
start = temp;
} else {
temp->next = start;
start = temp;
}
Also you need to have a function that deletes all nodes of the list.

Resources