473,789 Members | 2,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a different linked list(like doubly)

82 New Member
Hello.
we have this struct and we must to make a linked list
Expand|Select|Wrap|Line Numbers
  1. struct node {
  2.     char name[NAMESIZE];
  3.     char phone[PHONESIZE];
  4.     struct node *prevName;     // previous node alphabetically
  5.     struct node *prevNumber;   // previous node sorted by numbers
  6.     struct node *nextName;     // next node alphabetically
  7.     struct node *nextNumber;   // next node sorted by numbers
  8. } ;
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)
Jan 1 '08 #1
10 1980
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Jan 1 '08 #2
kalar
82 New Member
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
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. #define NAMESIZE 51
  6. #define PHONESIZE 11
  7.  
  8. /************************** TYPES ****************************/
  9.  
  10. enum {INSERT=1, DEL_NAME, DEL_NUM, PR_NAME, PR_NUM, EXIT};
  11.  
  12. typedef struct node {
  13.     char name[NAMESIZE];
  14.     char phone[PHONESIZE];
  15.     struct node *prevName;   
  16.     struct node *prevNumber;   
  17.     struct node *nextName;    
  18.     struct node *nextNumber;  
  19. } nodeT;
  20.  
  21. /************************** GLOBALS ***************************/
  22.  
  23. nodeT *root ;
  24.  
  25. /************************ PROTOTYPES  *************************/
  26. void printByName () ;
  27. void printByNumber () ;
  28. void initialize() ;
  29. void destroy() ;
  30. int searchByName( char name[]) ;
  31. int searchByNumber( char num[]) ;
  32. int insert ( char name[], char num[]) ;
  33. int removeName (char name[]) ;
  34. int removeNumber(char num[]) ;
  35. void print_menu() ;
  36.  
  37. /*************************** MAIN *****************************/
  38.  
  39. int main (int argc, char *argv[]) {
  40.  
  41.     int selection;
  42.     char name[NAMESIZE];
  43.     char last[NAMESIZE];
  44.     char first[NAMESIZE];
  45.     char num[PHONESIZE];
  46.     int op_result;
  47.     char dummy;
  48.  
  49.     initialize();
  50.     do {
  51.         print_menu();
  52.         scanf("%d", &selection) ;
  53.         scanf("%c", &dummy);
  54.  
  55.         switch(selection) {
  56.             case INSERT:
  57.                     break;
  58.             case DEL_NAME:
  59.                     break;
  60.             case DEL_NUM:
  61.                     break;
  62.             case PR_NAME:
  63.                     break;
  64.             case PR_NUM:
  65.                     break;
  66.             case EXIT:
  67.                     break;
  68.         }
  69.     } while (selection != EXIT);
  70.     destroy();
  71.     return 0;
  72. }    
  73.  
  74. /************************** FUNCTIONS ****************************/
Jan 1 '08 #3
kalar
82 New Member
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?
Jan 1 '08 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Jan 2 '08 #5
kalar
82 New Member
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
Jan 2 '08 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
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:
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     NodeT* listbyname = 0;
  4.     NodeT* listbynumber = 0;
  5.     NodeT* obj = CreateNode("John", "Frank", "Jones", "555-234-1345");
  6.     insert(&listbyname, &listbynumber, obj);
  7. }
  8.  
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.
Jan 3 '08 #7
kalar
82 New Member
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?)

Expand|Select|Wrap|Line Numbers
  1. int insert ( char name[], char num[]) {
  2.  
  3. struct node *newnode;
  4. newnode = (struct node *)malloc(sizeof(struct node));
  5.  
  6. /*copy the data in the fields of newnode and then */
  7.  
  8. /*while loop to find the right position alphabetically*/
  9. /*make the pointers*/
  10.  
  11.  
  12.  
  13. /*while loop to find the right position to be sorted by number*/
  14. /*make the pointers*/
  15. }
Jan 3 '08 #8
Raghunandan24
31 New Member
[quote=kalar]Hello.
we have this struct and we must to make a linked list
Expand|Select|Wrap|Line Numbers
  1. struct node {
  2.     char name[NAMESIZE];
  3.     char phone[PHONESIZE];
  4.     struct node *prevName;     // previous node alphabetically
  5.     struct node *prevNumber;   // previous node sorted by numbers
  6.     struct node *nextName;     // next node alphabetically
  7.     struct node *nextNumber;   // next node sorted by numbers
  8. } ;
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
Jan 4 '08 #9
kalar
82 New Member

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)
Jan 4 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

19
7828
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
7
14256
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...
2
2294
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
5
1464
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)?
10
1787
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.
3
1003
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.
9
3233
mbmccormick
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...
1
1104
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.
0
9511
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,...
0
10410
Oralloy
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...
0
9020
agi2029
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...
0
6769
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();...
0
5418
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...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
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
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
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...

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.