473,404 Members | 2,178 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,404 software developers and data experts.

a different linked list(like doubly)

82
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 1945
weaknessforcats
9,208 Expert Mod 8TB
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
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
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 Expert Mod 8TB
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
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 Expert Mod 8TB
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
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
[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

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
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)
There must be some use of the two arrays in the structure. What are you trying to do with the arrays?

Tell me if you encounter any problems.

Raghu
Jan 4 '08 #11

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

Similar topics

19
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
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#,...
2
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...
5
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
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
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
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...
1
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
tracyyun
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.