Upload
qundeel
View
3.956
Download
1
Embed Size (px)
DESCRIPTION
Citation preview
Linear Data Structures
• Arrays
• Strings
• Structures
• Stacks
• Queues
• Link List (Logically Linear)
Representation of Single Dimensional Arrays
• Dimension ?• Whole array is stored in a single contiguous
memory block.
• Address of the first element is called the base address and it is also the start address of array.
• The address of the ith element is given by the following formula:Addressi = base_address + i * size_of_element
• Arrays are very efficient way of organizing data since accessing array elements requires O(1).
elemn-1…elemi…elem2
elem1
elem0
float sum (float *list, int n){float temp = 0;int i;for (i = 0; i < n; i++)
temp = temp + *(list + i);return temp;
}
float sum (float list[], int n){float temp = 0;int i;for (i = 0; i < n; i++)temp = temp + list[i]);
return temp;}
Representation of Two Dimensional Array
• Can be called Table
• Needs two dimensions
• - One for Rows
• - Other for columns
• Two representations– Row Major– Column Major
Representations of Table
• Row Major ( First subscript changes least frequently and last subscript changes most frequently)
• (i,j)th element = Base Address + (i * N + j) * size of array
• Column Major ( Last subscript changes least frequently and first subscript changes more frequently)
• (i,j)th element = Base Address + (j * M + i) * size of array
void add (int a[][], int b[][],
int c[][],
int rows, int cols)
{
int i, j;
for (i=0; i<rows; i++)
for (j=0; j<cols; j++)
c[i][j] = a[i][j] + b[i][j];
}
What’s wrong?
const int N = 100;void add (int a[][N], int b[][N],
int c[][N], int rows, int cols)
{int i, j;for (i=0; i<rows; i++)for (j=0; j<cols; j++)c[i][j] = a[i][j] + b[i][j];
}
Why?
Two Dimensional Array
543451060
99827622
643823
6351
User’s view (abstraction)
543451060998276226438236351
System’s view (implementation)
Offset of a[i][j]?
Two Dimensional Array
543451060
99827622
643823
6351
User’s view (abstraction)
Offset of a[i][j]?Number of elements in each row?
ith row
total elements in the previous rows?
jth element in ith row
Offset (in units)
N
i * N
i * N + j
The addressing formula needs to know the number of columns but does not need the number of rows!
• Row major – C, Pascal, C++, etc.
• Column Major – FORTRAN• Ada - indefinite
Two Dimensional Array
• Row Major – Slice along the 1st dimension to
get an array of N-1 dimensions– Continue until you are left with
one dimension only.
– Offset of a[d0] [d1] …[dn-1]
Multi-Dimensional Arrays
A[D0] [D1]…[Dn-1]
(…((d0*D1 + d1)*D2 + d2)*D3 + …) + dn-1
Simple View
• General formula in Row Major
• A[s1,s2,s3,...sn] = Base Address + ((s1 *
D2*D
3*..D
n) + (s
2* D
3*D
4*..D
n) + (s
n-1*D
n) + S
n) *
Size of array
• General formula in Column Major
• A[s1,s2,s3,...sn] = Base Address + ((sn *
D1*D
2*..D
n-1) + (s
n-1* D
1*D
2*..D
n-2) + (s
2*D
1) + S
1)
* Size of array
Special Matrices
• Symmetric
• Diagonal
• Triangular
• Sparse
• * make programs of all these properties
Ordered Lists
• One of the most common data object is an ordered list.
• An ordered list has elements with definite ordering. The simplest example of an ordered list is an ordered pair which has only two elements.
• Examples of ordered lists:• Months of the years (Jan, Feb, Mar, Apr, May,
…., Dec)• English Alphabets (A, B, C, …, Z)• Words in a sentence (“This is a book on data
structures.”)• Names of the students in a class stored in the
order of their roll numbers.
Operations Defined on an Ordered List
• Find the length of list.• Check if the list is empty.• Traverse the list in some order.• Get the ith elements in the list.• Change the value of the ith element.• Insert a new element at the ith position.• Delete the ith element.• *Make programs of all these properties
Representation of an Ordered List
• The easiest way to represent an ordered list is by an one-dimensional array where we store the ith element of the list in the array element with index i.
• This is called sequential or linear mapping – mapping a one-dimensional vector onto a one-dimensional space.
class LinearList {public:
LinearList(int s); // constructor~ LinearList () {delete [ ] ListArray; } // destructorbool add (int value);bool remove (int index);bool change(int index, int value);bool get(int index, int & value);void printAll();
bool isFull() {return size == maxSize ;}bool isEmpty() {return size == 0; }int length() {return size;}
private:int maxSize; // max List sizeint *ListArray;int size; // no. of elements in the List
void readjust(int index);void moveForward(int index);void moveBackward(int index);
};
Linear List Using Arrays
LinearList :: LinearList(int s){
maxSize = s;ListArray = new int[maxSize];size = 0;
}
void LinearList :: printALL(){
for (i = 0; i < size; i++)cout << ListArray[i] << newline;
}
bool LinearList :: get(int index, int & value) {if (index < size && index >= 0) {
value = ListArray[index];return true;
}else
return false;}
bool LinearList::add(int value){
if (! isFull() ) { for (int i = size; i >= 1; i--) {
if (ListArray[i-1] > value)ListArray[i] = ListArray[i -1];
elsebreak;
}ListArray[i] = value;size++;return true;
}else return false;
}
bool LinearList::remove(int index){
if (index < size && index >= 0) { for (int i = index; i < size - 1; i++)
ListArray[i] = ListArray[i +1];size--;return true;
}else return false;
}
bool LinearList :: change(int index, int value) {if (index < size && index >= 0) {
ListArray[index] = value;readjust(index);return true;
}else
return false;}
void LinearList :: readjust(int index) {
if ((index < (size - 1)) && (ListArray[index] > ListArray[index + 1]))
moveForward(index);
else if ((index > 0) && (ListArray[index] < ListArray[index - 1]))
moveBackward(index);
}
void LinearList :: moveForward(int index) {
int temp = ListArray[index];
int i = index;
while ((i < (size - 1)) && (ListArray[i] > ListArray[i + 1])) {
ListArray[i] = ListArray[i+1];
i = i + 1;
}
if (i != index)
ListArray[i] = temp;
}
void LinearList :: moveBackward(int index) {
int temp = ListArray[index];
int i = index;
while ((i > 0 ) && (ListArray[i] < ListArray[i - 1])) {
ListArray[i] = ListArray[i-1];
i = i - 1;
}
if (i != index)
ListArray[i] = temp;
}
Assignment# 1 last date of submission : 02-09-2008 (Tuesday)
– Obtain an addressing formula for elements a[i,j] in the upper triangular matrix of order nxn. You can assume that this upper triangular is stored row by row
– Arrays - Read a matrix , find upper triangular matrix, find diagonal matrix, find determinant of a matrix
– Strings - Read a string , find the length of string, a particular
word, replace a particular word with new word. - Read an array of names of students and sort it.
– Structures - Make an array of structures (Roll-No, Name, Marks, Grade) , Display all records, find all the students with particular grade , find the record of a particular student, delete record of a particular student