Documente Academic
Documente Profesional
Documente Cultură
AAPP001-3-2
Learning Objectives
Self-Referential Structures
Dynamic Memory Allocation
Dynamic Structures
Linked Lists
Linked List Explained
Linked List Example
Overview
Slide 2 of 19
Introduction
Dynamic data structures
Data structures that grow and shrink during execution
Linked lists
Allow insertions and removals anywhere
Stacks
Allow insertions and removals only at top of stack
Queues
Allow insertions at the back and removals from the front
Binary trees
High-speed searching and sorting of data and efficient
elimination of duplicate data items
Overview
Slide 3 of 19
Self-Referential Structures
Self-referential structures
Structure that contains a pointer to a structure of the same type
Can be linked together to form useful data structures such as
lists, queues, stacks and trees
Terminated with a NULL pointer (0)
Diagram of two self-referential structure objects linked together
15
Data member
and pointer
Overview
10
Slide 4 of 19
Self-Referential Structures
struct node
{
int data;
struct node *nextPtr;
};
nextPtr
Points to an object of type node
Referred to as a link
Ties one node to another node
Overview
Slide 5 of 19
Overview
Slide 6 of 19
Example
#include <stdio.h> //http://www.programiz.com/c-programming/c-dynamic-memory-allocation
#include <stdlib.h>
int main(){
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc
if(ptr==NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
}
Overview
Slide 7 of 19
Linked Lists
Overview
Linked list
Linear collection of self-referential class objects, called nodes
Connected by pointer links
Accessed via a pointer to the first node of the list
Subsequent nodes are accessed via the link-pointer member of
the current node
Link pointer in the last node is set to Null to mark the lists end
Use a linked list instead of an array when
You have an unpredictable number of data elements
Your list needs to be sorted quickly
Slide 8 of 19
Linked Lists
Types of linked lists:
Singly linked list
Begins with a pointer to the first node
Terminates with a null pointer
Only traversed in one direction
Overview
Slide 9 of 19
5<
1?
previousPtr
5<
NULL
currentPtr
4?
data = 1
nextPtr
nextPtr == NULL
newPtr
data = 4
nextPtr
nextPtr == NULL
Create a new
node
data
data == 55
nextPtr
nextPtr == NULL
Overview
data = 7
nextPtr = NULL
Insert
done !
Slide 10 of 19
Overview
Slide 11 of 19
Overview
Slide 12 of 19
void menu()
{
printf(" linked list\n");
printf("1-- Insertion\n");
printf("2-- Deletion\n");
printf("3-- Display\n");
printf("select your option\n");
scanf("%d",&option);
}
Overview
Slide 13 of 19
if (list==NULL)
list=newnode;
else if(element<list->data)
{
newnode->next=list;
list=newnode;
}
Overview
Slide 14 of 19
Overview
Slide 15 of 19
Overview
Slide 16 of 19
Overview
Slide 17 of 19
Overview
Slide 18 of 19
Summary
Self-Referential Structures
Dynamic Memory Allocation
Dynamic Structures
Linked Lists
Linked List Explained
Linked List Example
Overview
Slide 19 of 19
Stacks
Overview
Slide 20 of 19