LinkedLists02 - Inserting an Element into a Linked List In...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Last time, we simply looked at how to place an element at the front of a linked list. However, we may want to maintain a linked list in order. In this case, we must insert a new element into its proper location instead of the front. Here are the cases we will consider: 1) Inserting into a NULL list. 2) Inserting an element into the front of the list. 3) Inserting an element later in the list. You would naturally add, "inserting an element to the end of a list" as a choice, but it turns out that the code for this case and for case 3 are the same. Before we move further, let's look at the prototype of our insert function. It will be the same as the insert function from last time: struct ll* insert(struct ll *front, int num); Notice that we must return a pointer to the front of the newly formed list, just in case it's different from the previous front pointer. Furthermore, we must also first create the node to insert given num, before we actually do the insertion. Here is how we can create the node:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/25/2010 for the course COMPUTER S COP3223 taught by Professor Lobo during the Spring '10 term at University of Central Florida.

Page1 / 5

LinkedLists02 - Inserting an Element into a Linked List In...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online