Sunteți pe pagina 1din 20

Databases and Data Structures

AAPP001-3-2

Dynamic Data Storage

Copyright 2004 Asia Pacific Institute of Information Technology

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

NULL pointer (points to nothing)

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

Dynamic Memory Allocation

Overview

Obtain and release memory during execution


malloc
Takes number of bytes to allocate
Use sizeof to determine the size of an object
Returns pointer of type void *
A void * pointer may be assigned to any pointer
If no memory available, returns NULL
Example
struct node *newPtr;
newPtr =(struct node*) malloc (sizeof(struct node));
free
Deallocates memory allocated by malloc
Takes a pointer as an argument
free ( newPtr );

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

Circular, singly linked


Pointer in the last node points back to the first node

Doubly linked list


Two start pointers first element and last element
Each node has a forward pointer and a backward pointer
Allows traversals both forwards and backwards

Circular, doubly linked list


Forward pointer of the last node points to the first node and
backward pointer of the first node points to the last node

Overview

Slide 9 of 19

Linked Lists Explained


5<
7?

Singly linked list


startPtr
NULL

5<
1?

previousPtr
5<
NULL

currentPtr

4?

data = 1
nextPtr
nextPtr == NULL
newPtr

Setup for sorted


insertion

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

Linked List Example


//Operating and maintaining a list
#include <stdio.h>
#include <stdlib.h> // for malloc function
void insertion();
void deletion();
void createlist();
void display();
void menu();
int option,element;
struct node
{
int data;
struct node *next;
}*newnode,*list,*prev,*temp,*tmpdisplay;

Overview

Slide 11 of 19

Linked List Example


void main()
{
createlist();
do
{
menu();
switch (option)
{
case 1: insertion();break;
case 2: deletion();break;
case 3: display();break;
case 4: break;
}
}while (option !=4);
}

Overview

Slide 12 of 19

Linked List Example


void createlist()
{
list=NULL;
}

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

Linked List Example


void insertion()
{
newnode=(struct node*) malloc (sizeof(struct node));

printf("enter the element:");


scanf("%d",&element);
newnode->data=element;
newnode->next=NULL;

if (list==NULL)
list=newnode;
else if(element<list->data)
{
newnode->next=list;
list=newnode;
}

Overview

Slide 14 of 19

Linked List Example


else
{
temp=list;
while(temp !=NULL )
{
if(element > temp->data)
{
prev=temp;
}
temp=temp->next;
}
newnode->next=prev->next;
prev->next=newnode;
}
}

Overview

Slide 15 of 19

Linked List Example


void deletion()
{
printf(element to delete?);
scanf(%d, &element);
if (list==NULL)
printf("list is empty");
else if (element==list->data)
list=list->next;
else
{
temp=list;
while(temp->data!=element)
{
prev=temp;
temp=temp->next;
}
prev->next=prev->next->next;
free(temp);
}
}

Overview

Slide 16 of 19

Linked List Example


void display()
{
if (list==NULL)
printf("\n\nList is empty");
else
{
tmpdisplay=list;
while(tmpdisplay!=NULL)
{
printf("%d\n",tmpdisplay->data);
tmpdisplay=tmpdisplay->next;
}
}
}

Overview

Slide 17 of 19

Linked List Example

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

What we will cover next

Stacks

Overview

Slide 20 of 19

S-ar putea să vă placă și