What are some hints for inserting a node into a linked list in C?

  • Thread starter rambo3131
  • Start date
  • Tags
    List
In summary, to insert a new node at the beginning of a linked list, you create a new node with the desired data, make its next pointer point to the current head, and make the head point to the new node. The time complexity of this operation is O(1). To insert a new node at the end of a linked list, you traverse to the end and make the last node's next pointer point to the new node. The time complexity of this operation is O(n). To insert a new node at a specific position in a linked list, you traverse to the node at the position before the desired position and adjust the next pointers accordingly.
  • #1
rambo3131
18
0
hi ,i have a problem about c.i should write a function that struct node *insert(struct node root,int n) .This function should create a node and then place it in list (whose head pointer is root )for n th node .Please give me some hints ..
 
Technology news on Phys.org
  • #2
Hint: At least do a Google search before asking something answered exhaustively in wikipedia.

There's pseudocode and just about everything you'd ever want to know about linked lists here:

http://en.wikipedia.org/wiki/Linked_list
 

1. How do you insert a new node at the beginning of a linked list?

To insert a new node at the beginning of a linked list, you first create a new node with the data you want to insert. Then, you make the new node's next pointer point to the current head of the linked list. Finally, you make the head of the linked list point to the new node.

2. What is the time complexity of inserting a node at the beginning of a linked list?

The time complexity of inserting a node at the beginning of a linked list is O(1). This is because you only need to perform a few constant time operations to insert the new node, regardless of the size of the linked list.

3. How do you insert a new node at the end of a linked list?

To insert a new node at the end of a linked list, you first traverse to the end of the linked list by following the next pointers until you reach the last node. Then, you make the last node's next pointer point to the new node, and you make the new node's next pointer point to null.

4. What is the time complexity of inserting a node at the end of a linked list?

The time complexity of inserting a node at the end of a linked list is O(n), where n is the number of nodes in the linked list. This is because you need to traverse through the entire linked list to reach the end.

5. How do you insert a new node at a specific position in a linked list?

To insert a new node at a specific position in a linked list, you first traverse to the node at the position before the desired position. Then, you make the new node's next pointer point to the next node, and you make the previous node's next pointer point to the new node.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
4
Replies
107
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
20
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
830
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top