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(str uct 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(p rsn));
if (!person)
{
puts("Stack is full. Cannot Push!\n\n");
break;
}
printf("\nEnter name : ");
scanf("%s",pers on->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",&per son->age);
// flush the input stream in case of bad input
fflush(stdin);
// store data and update top of list
top = sort_store(pers on,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(str uct 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");
} 3 5797
AMRa...@gmail.c om 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(str uct 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(p rsn)); if (!person) { puts("Stack is full. Cannot Push!\n\n"); break; } printf("\nEnter name : "); scanf("%s",pers on->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",&per son->age); // flush the input stream in case of bad input fflush(stdin);
// store data and update top of list top = sort_store(pers on,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(str uct 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 AM*****@gmail.c om 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(C ell));
memset(x,0,size of(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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 ?
|
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 other node, do
I need to add one more item in the ListNode OR I only call the queue
insert function to do it ?
|
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 other node, do
I need to add one more item in the ListNode OR I only call the queue
insert function to do it ?
|
by: Xarky |
last post by:
Hi,
I am writing a linked list in the following way.
struct list
{
struct list *next;
char *mybuff;
};
|
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) linked list to continousely (dynamically) free
up more memory when needed and put the buffers with the data of the
file on a sort of stack.
The problem is that i have no idea how to do this.
| |
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 elements are there in the linked list & print them..... then at the same program write aclass called queue that perform two operation <en equeue& de equeue> ..finally write aclass called stack that perform operation<empty,push,pup>.
note:
this must...
|
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 for
linked lists, I couldn't find one. I read something about Mcilroy's
"Optimistic Merge Sort" and studied some implementation, but they
were for arrays. Does anybody know if Mcilroys optimization is applicable to
truly linked lists at all?
|
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 i enqueue an elemet in the queue , it will be the last element can be popped from the stack. by c++
pls any body help ass son as posible.............
thanks all
|
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 linked list
Delete() – Deletes a string from the linked list
Search() – Search for a string and return 1 if found otherwise return 0.
isEmpty() – Returns 1 if the list is empty and 0 otherwise.
Display() – Display all the strings in the list.
...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |