basic data structures – continued (lists). 2 basic data types stack last-in, first-out (lifo)...
TRANSCRIPT
![Page 1: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/1.jpg)
Basic Data Structures – Continued Basic Data Structures – Continued (Lists)(Lists)
![Page 2: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/2.jpg)
2
Basic Data TypesBasic Data Types
StackLast-In, First-Out (LIFO)initialize, push, pop, status
QueueFirst-In, First-Out (FIFO)initialize, append, serve, status
• List
![Page 3: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/3.jpg)
3
OverviewOverview
• Abstract Data Types (ADT).
• Programming Styles.
• Lists.
• Operations.
• Implementations of Lists.
![Page 4: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/4.jpg)
4
Abstract Data Type (ADT)Abstract Data Type (ADT)
• A Data Structure together with operations defined on it.
• Useful to consider what operations are required before starting implementation.
• Led to the development of object oriented programming.
![Page 5: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/5.jpg)
5
A Stack ADTA Stack ADT
• Initialize the stack.• Determine whether the stack is empty.• Determine whether the stack is full.• Push an item onto the top of the stack. • Pop an item off the top of the stack.
A sequence of elements together with these operations:
![Page 6: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/6.jpg)
6
A Queue ADTA Queue ADT
• Initialize the queue.• Determine whether the queue is empty.• Determine whether the queue is full.• Find the size of the queue.• Append an item to the rear of the queue.• Serve an item at the front of the queue.
A sequence of elements together with these operations:
![Page 7: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/7.jpg)
7
A List ADTA List ADT
• Initialize the list.• Determine whether the list is empty.• Determine whether the list is full.• Find the size of the list.• Insert an item anywhere in the list.• Delete an item anywhere in a list.• Go to a particular position in a list.
A sequence of elements together with these operations:
![Page 8: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/8.jpg)
8
ComparisonComparison
• Stacks– Insert at the top of the stack (push)– Delete at the top of the stack (pop)
• Queues– Insert at the rear of the queue (append)– Delete at the front of the queue (serve)
• Lists– Insert at any position in the list.– Delete at any position in the list.
![Page 9: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/9.jpg)
9
A Rational Number ADTA Rational Number ADT
• Initialize the rational number.• Get the numerator.• Get the denominator.• Simplify a rational number.• Add two rational numbers.• Determine whether two rational numbers are
equal.• etc.
Two integers together with these operations:
![Page 10: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/10.jpg)
10
A String ADTA String ADT
• Initialize the string.• Copy a string.• Read in a line of input.• Concatenate two strings.• Compare two strings.• Find a length of a string.• etc.
A array of characters together with these operations:
![Page 11: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/11.jpg)
11
Programming StylesProgramming Styles• Procedural Programming
Decide which procedures you want: use the best algorithms you can find.
• Modular ProgrammingDecide which modules you want: partition the program so
that data is hidden in modules.
• Data AbstractionDecide which types you want: provide a full set of
operations for each type.
• Object-Oriented ProgrammingDecide which classes you want: provide a full set of operations for each class; make commonality explicit
by using inheritance.
![Page 12: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/12.jpg)
12
ListsLists
SEALFOXDEERAPE EMU0 1 3 42
![Page 13: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/13.jpg)
13
InsertionInsertion
SEALFOXDEERAPE0 1 32
EMU Inserted at position 2
SEALFOXDEERAPE0 1 3 4
EMU2
Before:
After:
![Page 14: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/14.jpg)
14
DeletionDeletion
SEALFOXDEERAPE0 1 3 4
EMU2
Delete item at position 3
SEALDEERAPE0 1 3
EMU2
Before:
After:
![Page 15: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/15.jpg)
15
List OperationsList Operations
• Initialize the list.
• Determine whether the list is empty.
• Determine whether the list is full.
• Find the size of the list.
• Insert an item anywhere in the list.
• Delete an item anywhere in a list.
• Go to a particular position in a list.
![Page 16: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/16.jpg)
16
Simple List ImplementationSimple List Implementation
0 1 2 3 4 5
APE DEER FOX SEAL
EMU
![Page 17: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/17.jpg)
17
Simple List ImplementationSimple List Implementation
0 1 2 3 4 5
APE DEER FOX SEAL
EMU
![Page 18: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/18.jpg)
18
Simple List ImplementationSimple List Implementation
0 1 2 3 4 5
APE DEER FOX SEALEMU
![Page 19: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/19.jpg)
19
#ifndef LISTH#define LISTH #include <stdlib.h>#include <stdio.h>#include <stdbool.h> #define MAXLIST 10 struct ListRec{
char list[MAXLIST];int count;
}; typedef struct ListRec List; void Initialise(List *lptr);void Insert(List *lptr, char item);void Delete(List *lptr, int pos);bool IsFull(List *lptr);bool IsEmtpy(List *lptr);int Find(List *lptr, char item); /* and any more functions you can think of) */ #endif
![Page 20: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/20.jpg)
20
#include <stdio.h>#include "list.h" void Initialise(List *lptr){
lptr->count = 0;}
bool isFull(List * lptr){
if(lptr->count == MAXLIST)return true;
elsereturn false;
}
bool isEmpty(List *lptr){
if(lptr->count == 0)return true;
elsereturn false;
}
![Page 21: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/21.jpg)
21
List InsertList Insert
- If the list isn't full:- step to position to insert item- move elements after this position along one
position starting from the end of the list, working back to the position.
- copy item into the position- increase count
![Page 22: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/22.jpg)
22
void Insert(List *lptr, char item){int pos, current;
if(IsFull(lptr)){
fprintf(stderr, "List is full.");exit(1);
}
for(pos = 0; pos < lptr->count; pos++){
if(lptr->list[pos] > item)break;
}
current = lptr->count - 1;while (current >= pos){
/* Copy the element into the next position along */lptr->list[current+1] = lptr->list[current];current--;
} lptr->list[pos] = item;
lptr->count++;}
![Page 23: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/23.jpg)
23
Linked List Implementation:Linked List Implementation:Using Array IndexUsing Array Index
DEER FOX APE SEAL
0 1 2 3 4 5
1 3 0 -1
data
marks last item
start
link to next item23
![Page 24: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/24.jpg)
24
Linked List Implementation:Linked List Implementation:Using Array IndexUsing Array Index
DEER FOX APE SEAL
0 1 2 3 4 5
3 0 -1
startinsert: EMU
EMU
11
![Page 25: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/25.jpg)
25
Linked List Implementation:Linked List Implementation:Using Array IndexUsing Array Index
DEER FOX APE SEAL
0 1 2 3 4 5
4 3 0 -1
startinsert: EMU
EMU
1
![Page 26: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/26.jpg)
26
Linked List Implementation:Linked List Implementation:Using PointersUsing Pointers
DEER FOX APE SEAL
0x2000 0x2008 0x2010 0x2018 0x2020 0x2018
0x2020 0x2018 0x2000 NULL
start
EMU
0x2008
![Page 27: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/27.jpg)
27
Linked List Implementation:Linked List Implementation:Using PointersUsing Pointers
DEER FOX APE SEAL
0x2000 0x2008 0x2010 0x2018 0x2020 0x2018
0x2020 0x2018 0x2000 NULL
start
EMU
0x2008
special pointerconstant
![Page 28: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/28.jpg)
28
#ifndef LLISTH #define LLISTH #include <stdlib.h>#include <stdio.h>#include <stdbool.h>#define MAXLIST 10 typedef struct EntryRec{
char ch;int next;
} Entry;
typedef struct LinkedListRec{
Entry list[MAXLIST];int count;int start;
} LList; void Initialise(LList *lptr);void Insert(LList *lptr, char item);void Delete(LList *lptr, int pos);bool IsFull(LList *lptr);bool IsEmtpy(LList *lptr);int Find(LList *lptr, char item);/* and any more functions you can think of) */#endif
![Page 29: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/29.jpg)
29
#include <stdio.h>#include "llist.h" void Initialise(LList *lptr){
lptr->count = 0;lptr->start = -1;
}
bool isFull(LList *lptr){
if(lptr->count == MAXLIST)return true;
elsereturn false;
}
bool isEmpty(LList *lptr){
if(lptr->count == 0)return true;
elsereturn false;
}
![Page 30: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/30.jpg)
30
Linked List InsertLinked List Insert- If the list isn't full:
- insert item at next free position.- step through list to find the element that comes before item
and the one after.- change the new item's next value to be the position of the
next element.- if the new item is not the first element
- change the previous element's next value to be the position of the new item.
- if the new item is the first element, change the start of the list to equal the new item's position.
- increase count.
![Page 31: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/31.jpg)
31
void Insert(LList *lptr, char item){ int current, previous; if(IsFull(lptr)) { fprintf(stderr, "Linked List is full."); exit(1); } lptr->list[lptr->count].ch = item; current = lptr->start; previous = -1; if (IsEmpty(lptr)) { lptr->list[0].next = -1; lptr->start = 0; } else { while(current!=-1 && lptr->list[current].ch < item) { previous = current; current = lptr->list[current].next; } if (previous==-1) { lptr->list[lptr->count].next=lptr->start; lptr->start=lptr->count; } else { lptr->list[lptr->count].next = lptr->list[previous].next; lptr->list[previous].next = lptr->count; } } lptr->count++;}
![Page 32: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/32.jpg)
32
Linked List ImplementationsLinked List Implementations
- There are many different types of implementations for linked lists in arrays. - Free positions always at the end (count), when
deleting need to shuffle items to close the gap- Using –2 to indicate a 'free' position – still slow
to find a free spot when inserting- Using a second linked list – a 'free list' inside the
same array – requires a second 'start' indicator. This is the most common implementation.
![Page 33: Basic Data Structures – Continued (Lists). 2 Basic Data Types Stack Last-In, First-Out (LIFO) initialize, push, pop, status Queue First-In, First-Out](https://reader036.vdocuments.mx/reader036/viewer/2022062801/56649e695503460f94b66147/html5/thumbnails/33.jpg)
33
NextNext
• Dynamic Memory
• Allocate/Deallocate Memory