Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS148 Introduction to Programming II

Similar presentations


Presentation on theme: "CS148 Introduction to Programming II"— Presentation transcript:

1 CS148 Introduction to Programming II
Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 12: 2/24/2003 Lecture 12: 2/24/2003 CS148 Spring 2003

2 Outline Linked lists Operations on linked lists Insert at back
Insert at front Lecture 12: 2/24/2003 CS148 Spring 2003

3 Linked Lists A list represented as an array is a sequential structure (successive components of the array are located next to each other in memory Efficient for searching in a sorted list using a binary search Insertion/deletion in a sorted array? A linked list is a collection of nodes. Nodes are scattered in memory, not in consecutive memory locations Each node is represented by a struct consisting of two main components Data Component (one or more struct members) Link Component (gives location of next node in the list) Lecture 12: 2/24/2003 CS148 Spring 2003

4 Linked Lists struct node { int ID; //Node ID string name;
Data Component Link A node in a linked list struct node { int ID; //Node ID string name; node* next; //pointer to next node }; HEAD 1 2 (N-1) N points to NULL (the end of the list) A linked list (HEAD gives the location of the first node in the list) Lecture 12: 2/24/2003 CS148 Spring 2003

5 Insertion at back of list
p HEAD NewNode struct node { int ID; //Node ID string name; node* next; //pointer to next node }; node *p,*NewNode; //allocate new node and fill NewNode = new node; NewNode->next = NULL; //link NewNode to the node pointed to by p p->next = NewNode; NewNode p Lecture 12: 2/24/2003 CS148 Spring 2003

6 Insertion at front of list
NewNode HEAD struct node { int ID; //Node ID string name; node* next; //pointer to next node }; HEAD NewNode node *p,*NewNode; //allocate new node NewNode = new node; //make NewNode the first node in the list NewNode->next = HEAD; HEAD = NewNode; HEAD NewNode Lecture 12: 2/24/2003 CS148 Spring 2003


Download ppt "CS148 Introduction to Programming II"

Similar presentations


Ads by Google