I am trying to create a simple program in C language using queues and structs .... to be a ticketing system.
ticket includes:
ticket id (to search with)
name
age
favorite movie
options to choose from:
add new ticket (enqueue)
search and display one ticket
Delete all (empty queue)
Display all tickets
my code:
#include <stdio.h>
#define SIZE 100
void enQueue(int);
void deQueue();
void display();
void search();
struct ticket_details
{
int ticket_id;
char name[20];
int age;
char movie[50];
};
struct ticket_details ticket_details;
int items[SIZE];
int front = -1;
int rear = -1;
int ticket_number=0;
int search_number=0;
void main() {
int choice = 0;
printf("1-buy ticket\n2-search for a ticket\n3-display all tickets\n4-delete a ticket\n");
scanf("%d",&choice);
switch(choice) {
case '1' :
ticket_number++;
enQueue(ticket_number);
break;
case '2' :
printf("ticket number:");
scanf("%d",&search_number);
search(search_number);
case '3' :
printf("display all tickets\n");
display();
break;
case '4' :
printf("ticket number:\n");
scanf("%d",&ticket_number);
deQueue(ticket_number);
break;
default :
printf("Invalid choice\n" );
}
}
void enQueue(int value) {
if (rear == SIZE - 1)
printf("\nNo more tickets!!");
else {
if (front == -1)
front = 0;
rear++;
items[rear] = value;
printf("Name:");
scanf("%s",ticket_details[value].name);
printf("age:");
scanf("%d",&ticket_details[items[rear]].age);
printf("Movie:");
scanf("%s",ticket_details[items[rear]].movie);
}
}
void deQueue(int value) {
if (front == -1)
printf("\nNo tickets to delete");
else {
int i;
for (i = front; i <= rear; i++)
if(i==value)
free(value);
printf("\nDeleted : %d", ticket_details[front]);
}
}
// Function to print the queue
void display() {
if (rear == -1)
printf("\nNo tickets found");
else {
int i;
printf("\nAll tickets:\n");
for (i = front; i <= rear; i++)
printf("%d/n", &ticket_details[i].ticket_id+
"%s/n",ticket_details[i].name+
"%d/n",ticket_details[i].age+
"%s/n",ticket_details[i].movie);
}
printf("\n");
}
void search(int search_number) {
int i;
for (i = front; i <= rear; i++)
if(i==search_number)
printf("%d/n", &ticket_details[search_number].ticket_id+
"%s/n",ticket_details[search_number].name+
"%d"/n,ticket_details[search_number].age+
"%s"/n,ticket_details[search_number].movie);
else
{
printf("Ticket number not found");
}
}
Related
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max 100
char name[max][80], data[80];
int front = 0;
int rear = 0;
int enqueue();
int dequeue();
int peek();
void display();
int main() {
int value;
int ch;
printf("------------------------------\n");
printf("\tMenu");
printf("\n------------------------------");
printf("\n [1] ENQUEUE");
printf("\n [2] DEQUEUE");
printf("\n [3] PEEK");
printf("\n [4] DISPLAY");
printf("\n------------------------------\n");
while(1)
{
printf("Choice : ");
scanf("%d", &ch);
switch(ch) {
case 1 : // insert
printf("\nEnter the Name : ");
scanf("%s",data);
value = enqueue(name, &rear, data);
if(value == -1 )
printf("\n QUEUE is Full \n");
else
printf("\n'%s' is inserted in QUEUE.\n\n",data);
break;
case 2 : // delete
value = dequeue(name, &front, &rear, data);
if( value == -1 )
printf("\n QUEUE is Empty \n");
else
printf("\n Deleted Name from QUEUE is : %s\n", data);
printf("\n");
break;
case 3:
value = peek(name, &front, &rear, data);
if(value != -1)
{
printf("\n The front is: %s\n", data);
}
break;
case 4:
display();
break;
case 5 : exit(0);
default: printf("Invalid Choice \n");
}
}
return 0;
}
int enqueue(char name[max][80], int *rear, const char data[80]) {
if(*rear + 1 == max)
return -1;
strcpy(name[*rear], data);
(*rear)++;
return 1;
}
int dequeue(char name[max][80], int *front, int *rear, char data[80])
{
if(*front == *rear)
return -1;
strcpy(data, name[(*front)++]);
return 1;
}
int peek(char name[max][80], int *front, int *rear, char data[80]) {
if(*front == *rear) {
printf(" QUEUE IS EMPTY\n");
return -1;
}
strcpy(data, name[*front]);
return 1;
}
void display(char name[max][80], int *front, int *rear, char data[80])
{
if(*front == -1 || *front > *rear)
{
printf("\n QUEUE IS EMPTY");
}
else
{
for(int i = *front; i<= *rear; i++)
{
printf("\t %s",data[i]);
}
}
}
Student here.
I need to display all elements inside the queue, but my program ends whenever I click option number 4, For example, the user inputs four names, "Jennie, Lisa, Jisoo, Rose", when the user selects option number 4, the program should print all 4 names. Even though I only input one name and need to print it, the program just ends. How to fix this?
I provided similar advise on earlier questions:
Your prototypes are incorrect (either fix them or delete them and move the definitions before use). I removed the prototype of display() and moved the implementation before use.
As your prototypes are wrong your compiler doesn't complain when you call dislay() without arguments (yours requires 4 arguments).
As display() doesn't change name, front, or rear I suggest you make first one const, and the other two integers. data is not used so eliminate it.
display(): queue is empty if front and rear have the same value.
Eliminate the global variables in favor of local variables in main().
Here is your working program:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max 100
char name[max][80], data[80];
int front = 0;
int rear = 0;
int enqueue();
int dequeue();
int peek();
void display(const char name[max][80], int front, int rear) {
if(front == rear) {
printf("\n QUEUE IS EMPTY");
return;
}
for(int i = front; i < rear; i++)
{
printf("\t %s", name[i]);
}
printf("\n");
}
int main() {
int value;
int ch;
printf("------------------------------\n");
printf("\tMenu");
printf("\n------------------------------");
printf("\n [1] ENQUEUE");
printf("\n [2] DEQUEUE");
printf("\n [3] PEEK");
printf("\n [4] DISPLAY");
printf("\n------------------------------\n");
while(1)
{
printf("Choice : ");
scanf("%d", &ch);
switch(ch) {
case 1 : // insert
printf("\nEnter the Name : ");
scanf("%s",data);
value = enqueue(name, &rear, data);
if(value == -1 )
printf("\n QUEUE is Full \n");
else
printf("\n'%s' is inserted in QUEUE.\n\n",data);
break;
case 2 : // delete
value = dequeue(name, &front, &rear, data);
if( value == -1 )
printf("\n QUEUE is Empty \n");
else
printf("\n Deleted Name from QUEUE is : %s\n", data);
printf("\n");
break;
case 3:
value = peek(name, &front, &rear, data);
if(value != -1)
{
printf("\n The front is: %s\n", data);
}
break;
case 4:
display(name, front, rear);
break;
case 5 : exit(0);
default: printf("Invalid Choice \n");
}
}
return 0;
}
int enqueue(char name[max][80], int *rear, const char data[80]) {
if(*rear + 1 == max)
return -1;
strcpy(name[*rear], data);
(*rear)++;
return 1;
}
int dequeue(char name[max][80], int *front, int *rear, char data[80])
{
if(*front == *rear)
return -1;
strcpy(data, name[(*front)++]);
return 1;
}
int peek(char name[max][80], int *front, int *rear, char data[80]) {
if(*front == *rear) {
printf(" QUEUE IS EMPTY\n");
return -1;
}
strcpy(data, name[*front]);
return 1;
}
and sample session:
------------------------------
Menu
------------------------------
[1] ENQUEUE
[2] DEQUEUE
[3] PEEK
[4] DISPLAY
------------------------------
Choice : 1
Enter the Name : test
'test' is inserted in QUEUE.
Choice : 1
Enter the Name : test2
'test2' is inserted in QUEUE.
Choice : 4
test test2
so i have created a program which uses double linked list and
performs some operations on it . The problem is that it displays garbage
values
at the end every time i try to create a linked list and then display it.
whats wrong in my code?(sorry! for the bad indentations)
if the linked list i created has elements say 15 and 16, it displays it as
15 16 25710 0 0
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct dll
{
int data;
struct dll *llink;
struct dll *rlink;
};
typedef struct dll *node;
node head=NULL;
void create();
int search(int u);
void insert(int num1);
void Delete(int num2);
void display();
node getnode();
int main()
{
int i,num,o;
while(1)
{
printf("\n1.create a list\n 2. insert before a search node\n 3. delete a node\n 4.display\n 5.exit\n");
scanf("%d",&num);
switch(num)
{
case 1 :
create();
break;
case 2 :
printf("enter the value before which you want to enter the node\n");
scanf("%d",&i);
insert(i);
break;
case 3 :
printf("enter the value to be deleted\n");
scanf("%d",&o);
Delete(o);
break;
case 4 :
printf("the linked list has :\n");
display();
break;
case 5 :
getch();
exit(1);
default :
printf("enter the correct option\n");
break;
}
}
}
node getnode()
{
node temp1;
temp1=(node)malloc(sizeof(node));
temp1->llink=NULL;
temp1->rlink=NULL;
return temp1;
}
void create()
{
node nn;
int num,y;
if(head==NULL)
head=getnode();
while(1)
{
printf("enter the data for the node");
scanf("%d",&num);
head->data=num;
printf("do you want to create another node(1/0):\n");
scanf("%d",&y);
if(y==1)
{
nn=getnode();
nn->rlink=head;
head->llink=nn;
head=nn;
nn=NULL;
}
else
break;
}
}
void insert (int num1)
{
int i,n,k;
node temp=head,nn;
n=search(num1);
if(n==0)
{
printf("element not present in the linked list");
}
if(n==1)
{
nn=getnode();
printf("enter the data for the node");
scanf("%d",&k);
nn->data=k;
nn->rlink=head;
head->llink=nn;
head=nn;
nn=NULL;
}
else
{
for(i=2; i<=n; i++)
temp=temp->rlink;
nn=getnode();
temp->llink->rlink=nn;
nn->llink=temp->llink;
nn->rlink=temp;
temp->llink=nn;
}
}
void Delete(int num2)
{
node temp=head;
int p,i;
p=search(num2);
if(p==0)
{
printf("no element is found");
}
if(p==1)
{
printf("the deleted element is %d",head->data);
head=head->rlink;
head->llink=NULL;
free(temp);
}
else
{
for(i=2; i<=p; i++)
{
temp=temp->rlink;
}
temp->llink->rlink=temp->rlink;
temp->rlink->llink=temp->llink;
free(temp);
temp=temp->rlink;
}
}
int search(int u)
{
node temp=head;
int pos=0;
if(u==head->data)
return 1;
else
{
while(temp!=NULL)
{
pos++;
if(temp->data==u)
{
printf("element found\n");
return(pos);
}
temp=temp->rlink;
}
}
if(temp==NULL)
{
return 0;
}
return -1;
}
void display()
{
node temp=head;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->rlink;
}
}
This:
temp1=(node)malloc(sizeof(node));
is a major error. Since you're "hiding a star", and node is a typedef for a pointer type, you're not allocating enough memory. It should be:
node temp1 = malloc(sizeof *temp1);
But I really recommend against typedefing a pointer away, it just makes things confusing.
I have created an array of linked lists which is organized in four levels (the parameters defined in the case 1 switch statement for party size) that correlate to array[0]-array[3], respectively.
I am trying to find out how to take the users input (from the scanf statement) and push it into its respective index in the array.
Can I get some help figuring out how to implement this strategy after the else logic?
Here is the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NODE struct node
struct node
{
char partyname[20];
int partysize;
NODE *next;
};
struct node* array[4]; // array to be inserted into
//
// proto
//
void add_party(char name[], int age);
void delete_party(char name[]);
void list_parties(void);
void change_p_size(void);
//
// globals
//
NODE *head=NULL;
NODE *tail=NULL;
//
// main function
//
int main()
{
int x;
while (1)
{
printf("Enter 1 to add a party\nEnter 2 to remove a party\nEnter 3 for the list of the party\nEnter 4 to quit\n");
// user interface
scanf("%d",&x);
switch(x)
{
char name[20]; //local variables
int size;
case 1:
printf("Party Name: ");
scanf("%s", name);
printf("\nParty Size: ");
scanf("%d", &size);
if(size >= 1 && size <= 2)
{
//put into array[0]
}
else if(size >= 3 && size <= 4)
{
//put into array[1]
}
else if(size >= 5 && size <= 6)
{
//put into array[2]
}
else(size >= 7)
{
//put into array[3]
}
add_party(name, &size)
break;
case 2:
printf("\nSize of party to delete: ");
scanf("%i", &size);
if(size >= 1 && size <= 2)
{
//traverse array[0] and delete
}
else if(size >= 3 && size <= 4)
{
//traverse array[0] and delete
}
else if(size >= 5 && size <= 6)
{
//traverse array[0] and delete
}
else(size >= 7)
{
//traverse array[0] and delete
}
delete_party(size)
break;
case 3:
list_parties();
break;
case 4:
change_partysize();
break;
case 5:
return 0;
default:
continue;
}
}
}
//
//add function
//
void add_party(char *name, int size)
{
//create a new node
NODE *p;
NODE *q;
int i=0;
q = (NODE *)malloc(sizeof(NODE)); // allocate memory the size of the struct
strcpy(q->name,partyname); // (source,destination)
q->size = partysize;
if(head == NULL) // if an empty list, create a head and tail
{
head = q;
tail = head;
q->next = NULL;
return;
}
//traversal
p = head;
while(p != NULL)
{
//check that no repeating names. delete nodes that do have repeating names
if(strcmp(p->name,name) == 0)
{
printf("\nSorry, that name is already taken\n");
free(q);
return;
}
p = p->next; //go to the next node in the list
}
tail->next = q;
q->next = NULL;
tail = q;
}
//
//delete function
//
void delete_party(int size)
{
NODE *p = head;
if(p == NULL)
return;
if(head == tail) // case 1
{
if(head->size <= size)
{
head=NULL;
tail=NULL;
free(p);
}
return;
}
while(p->next->next != NULL)
{
if(p->next->size <= size) // check that its not going too far?
{
p->next=p->next->next;
return;
}
}
if(p->size <= size) // case 2, multiple elements
{
head=p->next;
free(p);
return;
}
if(p->next->size <= size) // case 3, one element
{
node *q=p->next;
p->next=NULL;
free(q);
tail=p;
}
}
//
// list function
//
void list_parties(void)
{
node *p=head;
while(p!=NULL)
{
printf("%s, %d\n", p->name, p->size);
p=p->next;
}
}
Just add a destination parameter to your add_party function which specifies the array location.
Something like
add_party(node *dest, char *name, int age) then pass the location from your switch statementsand then call if like so
if(size >= 1 && size <= 2)
{
add_party(&array[0], name, age);
}
The same with delete function pass the array location as an argument.
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'm trying to write a recursive function to print out the values in preorder. However, for some reason, it keeps printing out the same as my inOrder function. The postOrder function works fine, but I had to do the recursive function slightly differently for that one. Can you guys take a look at my code below and let me know what's wrong? I really appreciate it cuz this has been giving me trouble ALL day. Thanks in advance
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#define TRUE 1
#define FALSE 0
typedef struct bnodestruct
{
int value;
struct bnodestruct *left;
struct bnodestruct *right;
}bnode;
typedef bnode *bnodePtr;
void displayMenu();
void insert (bnodePtr *hd, int val);
void inOrder(bnodePtr hd);
void preOrder(bnodePtr hd);
void postOrder(bnodePtr hd);
void empty (bnodePtr *hd);
int main (int argc, char **argv)
{
int val;
bnodePtr head;
head = NULL;
char option;
int result = 0;
while(1)
{
displayMenu();
printf("Choose an option : ");
scanf("\n%c", &option);
/*printf("The option chosen is %c\n", option);*/
switch (option)
{
case 'q':
printf("The program is exiting...\n");
exit(-1);
case 'i':
printf("Enter an integer value to be inserted into the linked list : ");
scanf("%d", &val);
insert(&head, val);
break;
case 'o':
inOrder(head);
break;
case 'n':
preOrder(head);
break;
case 'p':
postOrder(head);
break;
case 'e':
empty(&head);
/*inOrder (head);*/
break;
}
}
}
void displayMenu()
{
printf("\n Menu \n");
printf("(q): quit the program\n");
printf("(i): insert integer value into the binary tree\n");
printf("(e): empty all values from the binary tree\n");
printf("(o): list the items contained in the binary tree in order\n");
printf("(n): list the items contained in the binary tree in pre order\n");
printf("(p): list the items contained in the binary tree in post order\n");
}
void insert (bnodePtr *hd, int val)
{
if (*hd == NULL)
{
*hd = (bnodePtr)malloc(sizeof(bnode));
(*hd)->left = NULL;
(*hd)->value = val;
(*hd)->right = NULL;
}
else
{
if (val < ((*hd)->value))
insert (&((*hd)->left), val);
else if (val > ((*hd)->value))
insert (&((*hd)->right), val);
}
}
void empty (bnodePtr *hd)
{
bnodePtr temp1 = *hd;
bnodePtr temp2;
while (temp1 != NULL)
{
temp2 = temp1;
temp1 = temp1->left;
free (temp2);
}
*hd = NULL;
}
void inOrder (bnodePtr hd)
{
if (hd != NULL)
{
inOrder(hd->left);
printf("%d\n", hd->value);
inOrder(hd->right);
}
}
void preOrder (bnodePtr hd)
{
if (hd != NULL)
{
printf("%d\n", hd->value);
preOrder(hd->left);
preOrder(hd->right);
}
}
void postOrder (bnodePtr hd)
{
if (hd != NULL)
{
inOrder(hd->left);
inOrder(hd->right);
printf("%d\n", hd->value);
}
}
void postOrder (bnodePtr hd)
{
if (hd != NULL)
{
inOrder(hd->left); // XXX
inOrder(hd->right); // XXX
printf("%d\n", hd->value);
}
}
That's your problem right there. You're calling inOrder where you should be calling postOrder.