Connecting Tech Pros Worldwide Forums | Help | Site Map

doubly linked list

Newbie
 
Join Date: Sep 2009
Posts: 9
#1: Sep 22 '09
write a program for the creation of a doubly linked list using poiters in 'c' language.there should be provision for insertion and deletion of elements from the list using functions.your liked list should store all the necessary details of a number e.g. integer. your program should ask the user to enter the number.

Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 833
#2: Sep 22 '09

re: doubly linked list


What is your question?
Surely you're not asking for somebody here to do your homework assignment for you.
ssnaik84's Avatar
Member
 
Join Date: Aug 2009
Location: Bengaluru, India
Posts: 125
#3: Sep 22 '09

re: doubly linked list


looks like that.. homework.. :)
buy a book - "Let Us C" by yashavant kanetkar
or browse his website
Member
 
Join Date: Dec 2007
Location: Bangalore India
Posts: 54
#4: Sep 23 '09

re: doubly linked list


What you have done on this now?

Thanks,
Manjunath
Newbie
 
Join Date: Sep 2009
Posts: 9
#5: Sep 27 '09

re: doubly linked list


this is not my assignment question but i want to preapare it for my final exam. pz help me in doing this.
Newbie
 
Join Date: Sep 2009
Posts: 9
#6: Sep 27 '09

re: doubly linked list


can anyone write that program for me
Banfa's Avatar
Administrator
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,211
#7: Sep 27 '09

re: doubly linked list


Many of us could but none of us will. You will learn more by writing it yourself. My advice is to start with a singly linked list.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,953
#8: Sep 27 '09

re: doubly linked list


Quote:

Originally Posted by shaleni sen View Post

can anyone write that program for me

No. Quit asking. We do not supply code to those who beg for it. It is also against our forum guidelines to supply code for homework questions. Consider this a friendly warning.

Mark (moderator).
Newbie
 
Join Date: Sep 2009
Posts: 9
#9: Sep 28 '09

re: doubly linked list


i didnt beg for the answer, i was asking help for some hints only but since you guys dont want to help me so thats it. thankyou very much. byeee
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,953
#10: Sep 28 '09

re: doubly linked list


You asked for someone to write the program for you, not for 'hints'.

Good bye.
Newbie
 
Join Date: Sep 2009
Posts: 9
#11: Sep 30 '09

re: doubly linked list


seems you guys dont know how to write the codes of that questions thats why making such comments.
dheerajjoshim's Avatar
Needs Regular Fix
 
Join Date: Jul 2009
Location: Bangalore, INDIA
Posts: 274
#12: Sep 30 '09

re: doubly linked list


I have wrote doubly linked list code in different languages.....

Its not about whether people here know how to write code or not...

People in this forum will definitely help you if you write your own code and if you stuck up some where in between code development stage... Put your effort to write it.

Plenty of pseudo codes are available in the internet, pleas make use of it....

Enjoy coding... :)

Regards
Dheeraj Joshi
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 833
#13: Sep 30 '09

re: doubly linked list


Maybe we misunderstood your intent. This thread is already pretty long and it hasn't given you any guidance yet. How about if we start off with a clean slate?

I suggest you ask your question again in a new thread. This is usually frowned on as "double posting", but I think in this case the moderators might allow it. If not, they can censure me, not you.

Wait! Before you repost your question let's try to avoid repeating the same unpleasant trajectory. Please read the Posting Guideline regarding homework. You may not have meant it that way, but your first post in this thread looks exactly like what this guideline forbids. What we prefer to see is a specific question of the form "I tried this [insert code], but A happened instead of B [describe difference between actual results and desired results]. How do I get past this?"
Newbie
 
Join Date: Sep 2009
Posts: 9
#14: Sep 30 '09

re: doubly linked list


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct dllist {
  5.  int number;
  6.  struct dllist *next;
  7.  struct dllist *prev;
  8. };
  9.  
  10. struct dllist *head, *tail;
  11.  
  12. void append_node(struct dllist *lnode);
  13. void insert_node(struct dllist *lnode, struct dllist *after);
  14. void remove_node(struct dllist *lnode);
  15.  
  16. int main(void) {
  17.  struct dllist *lnode;
  18.  int i = 0;
  19.  
  20.  /* add some numbers to the double linked list */
  21.  for(i = 0; i <= 5; i++) {
  22.   lnode = (struct dllist *)malloc(sizeof(struct dllist));
  23.   lnode->number = i;
  24.   append_node(lnode);
  25.  }
  26.  
  27.  /* print the dll list */
  28.  for(lnode = head; lnode != NULL; lnode = lnode->next) {
  29.   printf("%d\n", lnode->number);
  30.  }
  31.  
  32.  /* destroy the dll list */
  33.  while(head != NULL)
  34.   remove_node(head);
  35. system("pause");
  36.  return 0;
  37. }
  38.  
  39. void append_node(struct dllist *lnode) {
  40.  if(head == NULL) {
  41.   head = lnode;
  42.   lnode->prev = NULL;
  43.  } else {
  44.   tail->next = lnode;
  45.   lnode->prev = tail;
  46.  }
  47.  
  48.  tail = lnode;
  49.  lnode->next = NULL;
  50. }
  51.  
  52. void insert_node(struct dllist *lnode, struct dllist *after) {
  53.  lnode->next = after->next;
  54.  lnode->prev = after;
  55.  
  56.  if(after->next != NULL)
  57.   after->next->prev = lnode;
  58.  else
  59.   tail = lnode;
  60.  
  61.  after->next = lnode;
  62. }
  63.  
  64. void remove_node(struct dllist *lnode) {
  65.  if(lnode->prev == NULL)
  66.   head = lnode->next;
  67.  else
  68.   lnode->prev->next = lnode->next;
  69.  
  70.  if(lnode->next == NULL)
  71.   tail = lnode->prev;
  72.  else
  73.   lnode->next->prev = lnode->prev;
  74. }
  75.  
above is the code which i wrote, is there any other way of writing this without using the for loop.
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 833
#15: Sep 30 '09

re: doubly linked list


Post repeated with code tags to make it easier to refer to the source code.
Quote:

Originally Posted by shaleni sen View Post

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct dllist {
  5.     int number;
  6.     struct dllist *next;
  7.     struct dllist *prev;
  8. };
  9. struct dllist *head, *tail;
  10.  
  11. void append_node(struct dllist *lnode);
  12. void insert_node(struct dllist *lnode, struct dllist *after);
  13. void remove_node(struct dllist *lnode);
  14.  
  15.  
  16. int main(void) {
  17.     struct dllist *lnode;
  18.     int i = 0;
  19.  
  20.     /* add some numbers to the double linked list */
  21.     for(i = 0; i <= 5; i++) {
  22.         lnode = (struct dllist *)malloc(sizeof(struct dllist));
  23.         lnode->number = i;
  24.         append_node(lnode);
  25.     }
  26.  
  27.     /* print the dll list */
  28.     for(lnode = head; lnode != NULL; lnode = lnode->next) {
  29.         printf("%d\n", lnode->number);
  30.     }
  31.  
  32.     /* destroy the dll list */
  33.     while(head != NULL)
  34.         remove_node(head);
  35.     system("pause");
  36.     return 0;
  37. }
  38.  
  39.  
  40. void append_node(struct dllist *lnode) {
  41.     if(head == NULL) {
  42.         head = lnode;
  43.         lnode->prev = NULL;
  44.     } else {
  45.         tail->next = lnode;
  46.         lnode->prev = tail;
  47.     }
  48.  
  49.     tail = lnode;
  50.     lnode->next = NULL;
  51. }
  52.  
  53.  
  54. void insert_node(struct dllist *lnode, struct dllist *after) {
  55.     lnode->next = after->next;
  56.     lnode->prev = after;
  57.  
  58.     if(after->next != NULL)
  59.         after->next->prev = lnode;
  60.     else
  61.         tail = lnode;
  62.  
  63.     after->next = lnode;
  64. }
  65.  
  66.  
  67. void remove_node(struct dllist *lnode) {
  68.     if(lnode->prev == NULL)
  69.         head = lnode->next;
  70.     else
  71.         lnode->prev->next = lnode->next;
  72.  
  73.     if(lnode->next == NULL)
  74.         tail = lnode->prev;
  75.     else
  76.         lnode->next->prev = lnode->prev;
  77. }
above is the code which i wrote, is there any other way of writing this without using the for loop.

There are two for loops. The first (line 21) is used to initially construct the list. It could be readily unwrapped if you wish. The second (line 28) is used to traverse the list. You have to use a loop instruction here.

General comments:
  1. Separate head and tail pointers (like you have on line 9) can be replaced by a single statically allocated list node. This node must never be inserted into the list. You only use its next and prev fields; the number field would be unused.
  2. You ought to initialize head and tail to NULL. True, they will be set to 0 when the program launches. Personally I'm willing to be a little redundant and explicitly initialize variables when a particular initial value is needed.
  3. You should always check if malloc returned NULL.
  4. Memory leak! You never free the list nodes created by malloc.
  5. All three functions should confirm their arguments are not NULL before dereferencing them.
  6. Corner case: a paranoid append_node would confirm lnode is not already in the list.
  7. Corner case: a paranoid insert_node would confirm lnode is not already in the list and that after is indeed in the list.
  8. Corner case: a paranoid remove_node would confirm lnode is in the list.
dheerajjoshim's Avatar
Needs Regular Fix
 
Join Date: Jul 2009
Location: Bangalore, INDIA
Posts: 274
#16: Oct 1 '09

re: doubly linked list


You can take the inputs from the user and make your list extends to any length.

donbock is right.....
Quote:
3 You should always check if malloc returned NULL.
4 Memory leak! You never free the list nodes created by malloc.
Please do take care about these two issues...

Regards
Dheeraj Joshi
Reply