Eee 243b applied computer programming
This presentation is the property of its rightful owner.
Sponsored Links
1 / 28

EEE 243B Applied Computer Programming PowerPoint PPT Presentation


  • 36 Views
  • Uploaded on
  • Presentation posted in: General

EEE 243B Applied Computer Programming. Linked list II Searching, Inserting and deleting. Review. Where in memory are linked lists stored? Does each node in a linked list have a symbol name (variable name)? What is the utility of the head pointer?. Outline. Linked Lists (LL)

Download Presentation

EEE 243B Applied Computer Programming

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Eee 243b applied computer programming

EEE 243BApplied Computer Programming

Linked list II

Searching, Inserting and deleting


Review

Review

  • Where in memory are linked lists stored?

  • Does each node in a linked list have a symbol name (variable name)?

  • What is the utility of the head pointer?

Maj JGA Beaulieu & Capt MWP LeSauvage


Outline

Outline

  • Linked Lists (LL)

    • Traversing/searching in linked lists

    • Inserting a node

    • Deleting a node

Maj JGA Beaulieu & Capt MWP LeSauvage


Traversing searching through a ll

Traversing/searching through a LL

  • Recall from lecture 19:

    typedef struct STUDENT_NODE_TAG

    {

    char firstName[15];

    char lastName[25];

    unsigned long collegeNumber;

    float average;

    struct STUDENT_NODE_TAG *pNextStdNode;

    } STUDENT_NODE; //name of the type

Maj JGA Beaulieu & Capt MWP LeSauvage


Traversing searching through a ll1

Traversing/searching through a LL

  • Declare a pointer to the type-defined structure in your declaration section (say pTraverse)

    STUDENT_NODE *pTraverse = NULL;

  • Set pTraverse to the head of the list

    pTraverse = pStudentList;

  • Check if the pointer is NULL

  • Process the current node

  • Set the pointer to the next node

    pTraverse = pTraverse->pNextStdNode;

  • Go back to step 3 until done

Maj JGA Beaulieu & Capt MWP LeSauvage


Traversing searching through a ll2

Traversing/searching through a LL

int count = 0;

STUDENT_NODE *pTraverse = pStudentList;

while (pTraverse != NULL)

{

count++; //process the current node

pTraverse = pTraverse->pNextStdNode;

}//end while pTraverse

Maj JGA Beaulieu & Capt MWP LeSauvage


Eee 243b applied computer programming

count = 0

pTraverse

Allard

20001

Allen

22456

pNextStdNode

pNextStdNode

pStudentList

Alpo

22460

Baar

20002

pNextStdNode

pNextStdNode

0

pTraverse = pStudentList;


Eee 243b applied computer programming

count = 1

pTraverse

Allard

20001

Allen

22456

pNextStdNode

pNextStdNode

pStudentList

Alpo

22460

Baar

20002

pNextStdNode

pNextStdNode

0

pTraverse = pTraverse->pNextStdNode;


Eee 243b applied computer programming

pTraverse

count = 4

Allard

20001

Allen

22456

pNextStdNode

pNextStdNode

pStudentList

Alpo

22460

Baar

20002

pNextStdNode

pNextStdNode

0

while (pTraverse->pNextStdNode != NULL) {…}


Inserting a node in the ll

Inserting a node in the LL

  • We already saw how to insert the first node in a LL that was empty in the previous lecture.

  • Before we insert any node in a LL we must first traverse it in search of our insertion point.

  • The only exception to the above rule is if we use an unordered list and always add the nodes at the beginning of the list.

    • This is a simple case that is covered by the more general ordered list insertion steps

Maj JGA Beaulieu & Capt MWP LeSauvage


Inserting a node in the ll1

Inserting a node in the LL

  • The first steps in inserting a node are:

    • Declare a temporary node (pTemp) and request memory through malloc

      • We verify if there is enough memory available

    • Set all the fields pointed to by pTemp with the appropriate values

  • To insert at the head of the list:

    • Point the new block to the first node by using the head pointer:

      pTemp->pNextStdNode = pStudentList;

    • Make the head point to the new node (pTemp)

      pStudentList = pTemp;

Maj JGA Beaulieu & Capt MWP LeSauvage


Inserting a node in the ll2

Inserting a node in the LL

//First steps to create node

STUDENT_NODE *pTemp = NULL;

pTemp = (STUDENT_NODE *)malloc(sizeof(STUDENT_NODE));

strcpy(pTemp->firstName,"Jack");

strcpy(pTemp->lastName,"Rabbit");

pTemp->collegeNumber = 22222;

pTemp->average = 99.9;

//Steps to include node in structure

pTemp->pNextStdNode = pStudentList;

pStudentList = pTemp;

Maj JGA Beaulieu & Capt MWP LeSauvage


Eee 243b applied computer programming

Rabbit

22222

pTemp

0

pNextStdNode

Allard

20001

0

pNextStdNode

pStudentList

First steps of creating the node


Eee 243b applied computer programming

Rabbit

22222

pTemp

pNextStdNode

1

Allard

20001

2

0

pNextStdNode

pStudentList

pTemp->pNextStdNode = pStudentList;

pStudentList = pTemp;


Inserting a node in the ll3

Inserting a node in the LL

  • To insert a node anywhere else:

    • Repeat the steps to create the new node

  • To insert in the the list:

    • Traverse the list using pTraverse (here we assume that we want to insert after pTraverse)

    • Make the new node point where pTraverse points

    • Make pTraverse point to the new node

Maj JGA Beaulieu & Capt MWP LeSauvage


Inserting a node in the ll4

Inserting a node in the LL

//First steps to create node

STUDENT_NODE *pTemp = NULL;

pTemp = (STUDENT_NODE *)malloc(sizeof(STUDENT_NODE));

strcpy(pTemp->firstName,"Jack");

strcpy(pTemp->lastName,"Rabbit");

pTemp->collegeNumber = 22222;

pTemp->average = 99.9;

//Steps to include node in structure

pTemp->pNextStdNode = pTraverse->pNextStdNode;

pTravers->pNextStdNode = pTemp;

Maj JGA Beaulieu & Capt MWP LeSauvage


Eee 243b applied computer programming

Rabbit

22222

pTemp

0

pNextStdNode

pTraverse

All

20001

Allard

20001

pNextStdNode

pNextStdNode

pStudentList

0

First steps of creating the node


Eee 243b applied computer programming

Rabbit

22222

pTemp

pNextStdNode

1

pTraverse

Allen

22456

Allard

20001

pNextStdNode

pNextStdNode

2

pStudentList

0

pTemp->pNextStdNode = pTravers->pNextStdNode;

pTravers->pNextStdNode = pTemp;


Deleting a node in the ll

Deleting a node in the LL

  • Declare a new pointer called pPred that will follow pTraverse but one node behind

  • Check if the list is empty, if so print an error

  • All the nodes in the LL can be deleted in the same fashion except for one. If the node to be deleted is the head, you set the head pointer to the next node in the list.

  • Free the memory

Maj JGA Beaulieu & Capt MWP LeSauvage


Deleting a node in the ll head

Deleting a node in the LL - head

…//Special condition node is the head

unsigned long delNumber = 0; //get this value somehow

STUDENT_NODE *pTraverse = pStudentList;

STUDENT_NODE *pPred = pStudentList;

if (pStudentList == NULL){…} //print error and get out

if (pStudentList->collegeNumber == delNumber)

{

pStudentList = pStudentList->pNext;

free(pTraverse);

}

else

pTraverse = pTraverse->pNextStdNode;

…//pTraverse is now one node ahead of pPred

Maj JGA Beaulieu & Capt MWP LeSauvage


Eee 243b applied computer programming

pPred

delNumber = 20001

pTraverse

Allard

20001

Allen

22456

pNextStdNode

pNextStdNode

pStudentList

Alpo

22460

Baar

20002

pNextStdNode

pNextStdNode

0

pStudentList = pStudentList->pNextStdNode;


Eee 243b applied computer programming

pPred

delNumber = 20001

pTraverse

Allard

20001

Allen

22456

pNextStdNode

pNextStdNode

pStudentList

Alpo

22460

Baar

20002

pNextStdNode

pNextStdNode

0

pStudentList = pStudentList->pNextStdNode;


Deleting a node in the ll general

Deleting a node in the LL - general

  • Do a search for the node to delete by testing a condition (i.e. collegeNumber match) inside a loop

  • Advance both pointers (pTraverse and pPred)

  • Delete the node by setting the next node of pPred as follows:

    pPred->pNextStdNode = pTraverse->pNextStdNode;

  • Free the memory where pTraverse points

Maj JGA Beaulieu & Capt MWP LeSauvage


Deleting a node in the ll general1

Deleting a node in the LL - general

…//General condition delete

//This code follows the code for deleting the head

while (pTraverse->collegeNumber != delNumber)

{

pPred = pTraverse;

pTraverse = pTraverse->pNextStdNode;

if (pTraverse == NULL) {…} //print error and exit fctn

}

pPred->pNextStdNode = pTraverse->pNextStdNode;

free(pTraverse);

Maj JGA Beaulieu & Capt MWP LeSauvage


Eee 243b applied computer programming

pPred

delNumber = 20002

Allard

20001

Allen

22456

pNextStdNode

pNextStdNode

pStudentList

Alpo

22460

Baar

20002

pTraverse

pNextStdNode

pNextStdNode

0

Advancing both pointers pTraverse ahead of pPred


Eee 243b applied computer programming

pPred

delNumber = 20002

Allard

20001

Allen

22456

pNextStdNode

pNextStdNode

pStudentList

Alpo

22460

Baar

20002

pTraverse

pNextStdNode

pNextStdNode

0

pPred->pNextStdNode = pTraverse->pNextStdNode;


Deleting a node in the ll general2

Deleting a node in the LL - general

  • You can also use a level of indirection to keep a hold of the predecessor to the node being deleted

  • The following code is equivalent to the one on slide 24

  • When using this kind of indirection be careful what you free!

    while ((pTraverse->pNextStdNode)->collegeNumber != delNumber)

    {

    pTraverse = pTraverse->pNextStdNode;

    if (pTraverse == NULL) {…} //print error and exit fctn

    }

    pTemp = pTraverse->pNextStdNode;

    pTraverse->pNextStdNode = (pTraverse->pNextStdNode)->pNextStdNode;

    free(pTemp); //This line is important

Maj JGA Beaulieu & Capt MWP LeSauvage


Quiz time

Quiz Time

  • Describe the steps to traverse a LL

  • Describe the steps to perform a general insert operation in a LL

Maj JGA Beaulieu & Capt MWP LeSauvage


  • Login