Hello.
we have this struct and we must to make a linked list - struct node {
-
char name[NAMESIZE];
-
char phone[PHONESIZE];
-
struct node *prevName; // previous node alphabetically
-
struct node *prevNumber; // previous node sorted by numbers
-
struct node *nextName; // next node alphabetically
-
struct node *nextNumber; // next node sorted by numbers
-
} ;
As i can see i must make a linked list(like doubly but with 4 pointers) or two doubly linked lists:
1)one with prevName and nextName
2) and one with prevNumber and nextNumber
From these lists i will print the elements alphabetically or sorted by numbers
I believe that i must make ONE list such as:
malloc for the newnode and then just change the pointesr to be sorted
My problem is that i can't do this in one list.I don't know what i need(maybe 2 while loops for the pointers?) and i am confused.Please give any idea?
(if you want i can post the code to add a number in a doubly linked list sorted)
10 1980
Where did this struct come from?? It is absolutely how you do not design a node for a linked list.
You
a) do not have the data and the next/prev pointer in the same struct
b) do not use multiple sets of next/prev pointers for various list sort sequences.
I can make a recommendation but I need to know about where this struct originated.
Where did this struct come from?? It is absolutely how you do not design a node for a linked list.
You
a) do not have the data and the next/prev pointer in the same struct
b) do not use multiple sets of next/prev pointers for various list sort sequences.
I can make a recommendation but I need to know about where this struct originated.
If i understand what you say( i don't speak very good english)
for a) why? in the struct i gave you there are in the same struct
for help i can give a skeleton in which you can make the programm
Any idea how to make it would be very helpfull - #include<stdio.h>
-
#include<stdlib.h>
-
#include<string.h>
-
-
#define NAMESIZE 51
-
#define PHONESIZE 11
-
-
/************************** TYPES ****************************/
-
-
enum {INSERT=1, DEL_NAME, DEL_NUM, PR_NAME, PR_NUM, EXIT};
-
-
typedef struct node {
-
char name[NAMESIZE];
-
char phone[PHONESIZE];
-
struct node *prevName;
-
struct node *prevNumber;
-
struct node *nextName;
-
struct node *nextNumber;
-
} nodeT;
-
-
/************************** GLOBALS ***************************/
-
-
nodeT *root ;
-
-
/************************ PROTOTYPES *************************/
-
void printByName () ;
-
void printByNumber () ;
-
void initialize() ;
-
void destroy() ;
-
int searchByName( char name[]) ;
-
int searchByNumber( char num[]) ;
-
int insert ( char name[], char num[]) ;
-
int removeName (char name[]) ;
-
int removeNumber(char num[]) ;
-
void print_menu() ;
-
-
/*************************** MAIN *****************************/
-
-
int main (int argc, char *argv[]) {
-
-
int selection;
-
char name[NAMESIZE];
-
char last[NAMESIZE];
-
char first[NAMESIZE];
-
char num[PHONESIZE];
-
int op_result;
-
char dummy;
-
-
initialize();
-
do {
-
print_menu();
-
scanf("%d", &selection) ;
-
scanf("%c", &dummy);
-
-
switch(selection) {
-
case INSERT:
-
break;
-
case DEL_NAME:
-
break;
-
case DEL_NUM:
-
break;
-
case PR_NAME:
-
break;
-
case PR_NUM:
-
break;
-
case EXIT:
-
break;
-
}
-
} while (selection != EXIT);
-
destroy();
-
return 0;
-
}
-
-
/************************** FUNCTIONS ****************************/
I can't edit my post so i post again
Am I in the right way if try to make it like a list or not?
I wanted to know if you are taking a class and you instructor said to use this struct, or have you just made it up yourself.
I wanted to know if you are taking a class and you instructor said to use this struct, or have you just made it up yourself.
yes i must use this struct, it is an exercise
OK. I just want you to know that this struct is not how you design a linked list.
That said, you should be able to use one list. But You will need two roots.
One for the squence by name and the other fof the seqence by phone number.
These roots should be local variable in main() rather than global variables. You pass them along as function arguments.
That implies two insert functions. One for the name sequence and one for the phone number sequence.
Then when you insert, say using the phone number sequence, from inside that function you have to also insert by name so all four pointers are correct.
This means the insert functions cannot create the node to be inserted. It will have to be passed in.
Both roots also need to be passed in.
Maybe: -
int main()
-
{
-
NodeT* listbyname = 0;
-
NodeT* listbynumber = 0;
-
NodeT* obj = CreateNode("John", "Frank", "Jones", "555-234-1345");
-
insert(&listbyname, &listbynumber, obj);
-
}
-
In this way you can use whichever root to display the list in different sequences.
Again, I repeat, this is not how to do this in real life.
Thanks for your answer.I also think something like this that you suggest me but the function prototype for insert() has been given to me as :
int insert ( char name[], char num[]) ;
So i shouldn't pass to the function anything else.If i make root1 and root2 globals and a code like this i hope that i have the result i want.What's your opinion?(or we say the same things?) - int insert ( char name[], char num[]) {
-
-
struct node *newnode;
-
newnode = (struct node *)malloc(sizeof(struct node));
-
-
/*copy the data in the fields of newnode and then */
-
-
/*while loop to find the right position alphabetically*/
-
/*make the pointers*/
-
-
-
-
/*while loop to find the right position to be sorted by number*/
-
/*make the pointers*/
-
}
[quote=kalar]Hello.
we have this struct and we must to make a linked list - struct node {
-
char name[NAMESIZE];
-
char phone[PHONESIZE];
-
struct node *prevName; // previous node alphabetically
-
struct node *prevNumber; // previous node sorted by numbers
-
struct node *nextName; // next node alphabetically
-
struct node *nextNumber; // next node sorted by numbers
-
} ;
QUOTE]
As i see it you need to make two doubly linked lists. One for the names and one for the numbers.
Why do you have two arrays inside the structure? do you have to add all the array values to the linked lists?
To do this you need to create an instance of the structure in the main method.
Using
[code]
// create instance of structure in main method
instance -> data= <values>
instance -> next= <new instance> // this points to the next node
//Similarly you need to use ->prev to point to the previous instance.
[code]
Hope this helps
Raghu
Why do you have two arrays inside the structure? do you have to add all the array values to the linked lists?
Raghu
Thanks but i must use the struct as it is .I can't change the struct(as i say in another post it is an exercise)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Dongsheng Ruan |
last post by:
with a cell class like this:
#!/usr/bin/python
import sys
class Cell:
def __init__( self, data, next=None ):
self.data = data
|
by: Zeba |
last post by:
Hi,
I have to write program in C# to merge sort a linked list (doubly
linked). Is it true that the best way to do it is copy it into an
array, sort it and then convert back ?
I'm new to C#, but I tried to develop a merge sort program myself, but
have got stuck with a Null reference exception for the pos variable in
merge function. Also I wrote a function to get the actual nodes from
teh index values before calling merge function. Is...
|
by: LTDEV |
last post by:
Hi,
Im looking for a linked-list like structure in C#. Im a C++ programmer that
needs to make little module (TCP sockets communication) in C#.
I need to attach a buffer for incoming data because i have to decipher
incoming data and extract them into datapackets, and the forward them onto
the application.
In C++ i normally create a linked list, where i then push() the packets of
|
by: Daniel Nogradi |
last post by:
The etree.Element (or ElementTree.Element) supports a number of
list-like methods: append, insert, remove. Any special reason why it
doesn't support pop and extend (and maybe count)?
|
by: =?utf-8?B?5Lq66KiA6JC95pel5piv5aSp5rav77yM5pyb5p6B |
last post by:
Is there a simple function to generate a list like ? The range() just can generate the numeric list.
| |
by: noagbodjivictor |
last post by:
>>s =
This is what I want so that I can put it in a module then import that
module to work with my variable.
|
by: mbmccormick |
last post by:
In Visual Studio when developing on VB.NET, we would get an instant error list- before compiling or building the solution. Why doesn't C# have this same functionality? Whenever I want to see my errors, I have to compile first...
|
by: =?Utf-8?B?S3VuIE5pdQ==?= |
last post by:
Dear all,
I'm trying to write a program with a control like the left flist of outlook.
A header or button above with a list under it.
But it's a bit difficult to emulate the animation effect when the button
rises up or sets down.
I wonder if there's such a sample.
Thanks for any hints in advance.
|
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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
| |
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: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
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...
| |