Click here to Skip to main content
15,564,534 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello i ve learned in class about simple and double linked list but really didnt understand it.
I have to write a code to add 2 elements at the top and 2 elements on the bottom of the simple linked list and i have no idea how cand someone help me please ?

What I have tried:

I ve searched the internet but really diddnt find something usefull
Posted
Updated 8-Nov-22 14:45pm

Quote:
I ve searched the internet but really diddnt find something usefull
Try harder to search: There is plenty of info about linked list, on the web. See, for instance: Linked List[^].
 
Share this answer
 
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
Think about what a linked list is, and try to emulate it on paper. Then think about how you would add an element to the head of that paper list, and write yourself instructions to do that. Check they work, and then it should be pretty simple to convert that to code. I'd start by writing code to construct then print a populated list and make sure that works, then add your insert function and try that. Take it in little steps, one at a time, making sure everything works before you move on and it isn't too difficult. If you rush in and slam out a big block of code, then you have problems because you don't know which bits are really working!
 
Share this answer
 
Quote:
i ve learned ... i have no idea

1. Obviously you should define the list first. this usually looks in C like this:
C
typedef struct mylist {
	int val;
	struct mylist *next;
} listtyp;

2. After you have defined how an element of the list should look like, you can create one:
C
listtyp* list_create_node(int val)
{
  listtyp* tmp = NULL;
  tmp = (listtyp *)calloc(1, sizeof(listtyp));
  tmp->val = val;
  return tmp;
}

3. Usually you should be able to output the list:
C
void list_print(listtyp* head) 
{
  listtyp* current = head;

  while (current != NULL) {
    printf("%d\n", current->val);
    current = current->next;
  }
}

4. Your task is now to write the two functions:
C
// write a code to add elements at the top
listtyp* list_add_top(listtyp* head)
{
  listtyp* tmp = NULL;
  // TODO: write your code here
  return tmp;
}

// write a code to add elements on the bottom
listtyp* list_add_bottom(listtyp* head)
{
  listtyp* tmp = NULL;
  // TODO: write your code here
  return tmp;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900