A
AMRaymo
Can someone guide me in the right direction on how to enqueue and
dequeue/pop and push within a linked list? I've figured out the basic
idea, but getting the other options in it seems ot be a problem.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct info {
char name[16];
int age;
struct info *next; // pointer to next data item in list
} prsn;
struct info *top; // establish start of list
// proto types
struct info *sort_store(struct info *new, struct info *top);
void display(struct info *start);
int main()
{ struct info *person, *sort_store();
system("cls");
printf("What would you like to do?\n");
printf("1 - Push to the stack\n");
printf("2 - Pop from the stack\n");
printf("3 - Display the stack\n");
printf("\n - Quit\n");
system("cls");
printf("What would you like to do?\n");
printf("1 - Push to the stack\n");
printf("2 - Pop from the stack\n");
printf("3 - Display the stack\n");
printf("\n0 - Quit\n");
scanf("%i", &intChoice);
case: 1
(
);break;
case: 2
(
);break;
case: 3
(
);break;
case
printf("\nEnter names and ages:\n");
for (;
{
person = (struct info *) malloc(sizeof(prsn));
if (!person)
{
puts("Stack is full. Cannot Push!\n\n");
break;
}
printf("\nEnter name : ");
scanf("%s",person->name);
// flush the input stream in case of bad input
fflush(stdin);
// mimic an AND situation
if (strlen(person->name) == 1)
{
if (person->name[0] == 'q') break;
}
printf("Enter age : ");
scanf("%d",&person->age);
// flush the input stream in case of bad input
fflush(stdin);
// store data and update top of list
top = sort_store(person,top);
}
// display the sorted list from the top
display(top);
getchar(); // wait
return 0;
}
//
// insert new data to the list in sorted order
//
struct info *sort_store(struct info *new, struct info *top)
{
static struct info *last = NULL;
struct info *old, *start;
start = top;
if (!last)
{
new->next = NULL;
last = new;
return (new);
}
old = NULL;
while (top)
{
// sort by name in ascending order
if (strcmp(top->name, new->name) < 0)
{
old = top;
top = top->next;
}
else
{
if (old)
{
old->next = new;
new->next = top;
return (start);
}
new->next = top;
return (new);
}
}
last->next = new;
new->next = NULL;
last = new;
return (start);
}
//
// walk through the linked list and display the content
//
void display(struct info *start)
{
while (start)
{
printf(" [%s , %2d]", start->name, start->age);
start = start->next;
}
printf("\n");
}
dequeue/pop and push within a linked list? I've figured out the basic
idea, but getting the other options in it seems ot be a problem.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct info {
char name[16];
int age;
struct info *next; // pointer to next data item in list
} prsn;
struct info *top; // establish start of list
// proto types
struct info *sort_store(struct info *new, struct info *top);
void display(struct info *start);
int main()
{ struct info *person, *sort_store();
system("cls");
printf("What would you like to do?\n");
printf("1 - Push to the stack\n");
printf("2 - Pop from the stack\n");
printf("3 - Display the stack\n");
printf("\n - Quit\n");
system("cls");
printf("What would you like to do?\n");
printf("1 - Push to the stack\n");
printf("2 - Pop from the stack\n");
printf("3 - Display the stack\n");
printf("\n0 - Quit\n");
scanf("%i", &intChoice);
case: 1
(
);break;
case: 2
(
);break;
case: 3
(
);break;
case
printf("\nEnter names and ages:\n");
for (;
{
person = (struct info *) malloc(sizeof(prsn));
if (!person)
{
puts("Stack is full. Cannot Push!\n\n");
break;
}
printf("\nEnter name : ");
scanf("%s",person->name);
// flush the input stream in case of bad input
fflush(stdin);
// mimic an AND situation
if (strlen(person->name) == 1)
{
if (person->name[0] == 'q') break;
}
printf("Enter age : ");
scanf("%d",&person->age);
// flush the input stream in case of bad input
fflush(stdin);
// store data and update top of list
top = sort_store(person,top);
}
// display the sorted list from the top
display(top);
getchar(); // wait
return 0;
}
//
// insert new data to the list in sorted order
//
struct info *sort_store(struct info *new, struct info *top)
{
static struct info *last = NULL;
struct info *old, *start;
start = top;
if (!last)
{
new->next = NULL;
last = new;
return (new);
}
old = NULL;
while (top)
{
// sort by name in ascending order
if (strcmp(top->name, new->name) < 0)
{
old = top;
top = top->next;
}
else
{
if (old)
{
old->next = new;
new->next = top;
return (start);
}
new->next = top;
return (new);
}
}
last->next = new;
new->next = NULL;
last = new;
return (start);
}
//
// walk through the linked list and display the content
//
void display(struct info *start)
{
while (start)
{
printf(" [%s , %2d]", start->name, start->age);
start = start->next;
}
printf("\n");
}