473,378 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Linked list stack and queue

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");
}

Nov 14 '05 #1
3 5776

AMRa...@gmail.com wrote:
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");
}

knuth vol 1

Nov 14 '05 #2
AM*****@gmail.com wrote:
# 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.

typedef struct Cell Cell,*pCell;
typedef struct List List;

struct Cell {
pCell next;
...
};
struct List {
pCell first,last;
};

pCell newcell(void) {
pCell x = malloc(sizeof(Cell));
memset(x,0,sizeof(Cell));
return x;
}

pCell push(List *list) {
pCell x = newCell();
x->next = list->first;
if (!list->first) list->last = x;
list->first = x;
return x;
}

pCell pop(List *list) {
if (list->first) {
pCell x = list->first;
list->first = x->next;
return x;
}else {
return 0;
}
}

pCell top(List *list) {
return list->first;
}

#define empty(list) (!top(list))

pCell append(List *list) {
pCell x = newcell();
if (list->first) list->first = x;
else list->last->next = x;
list->last = x;
return x;
}

pCell bottom(List *list) {
return list->first ? list->last : 0;
}

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I think that's kinda of personal; I don't think I should answer that.
Nov 14 '05 #3

Darius wrote:

[HUGE snip]
knuth vol 1

Is there some reason you felt the need to quote over 100 lines just to
add on rather cryptic statement?


Brian

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kay | last post by:
I would like to ask a question about linked list and queue. 1) Can each node of linked list contain a queue ? 2) Is it the same queue or different queue ?
1
by: Kay | last post by:
A linked list is storing several names. I want to make a queue if I input a name that is same as the linked list. How to make each node of a linked list storing a queue that are different with each...
2
by: Kay | last post by:
A linked list is storing several names. I want to make a queue if I input a name that is same as the linked list. How to make each node of a linked list storing a queue that are different with each...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
4
by: Henk | last post by:
Hi, I am new to the c-programming language and at the moment I am struggling with the following: I want to read a file using fread() and then put it in to memory. I want to use a (singel)...
1
by: oeillet | last post by:
hi every one iwant ahelp in this assignment(program ,code in c++) its as follows; aprogram that contain all operation that performed by linked list ; also contain amethod that calculate how many...
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
2
by: waceys | last post by:
hello everybody i need some body help me to make code for a linked list and arrays build stack and queue such that when i push an element in the stack i can dequeue it first from the queue and when...
1
by: yaarnick | last post by:
Create a linked list data structure library to hold strings. Then library should support the following linked list operations. Create() – Creates the linked list Insert() – Insert a string into the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.