Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Linear Structures 2
4
Chapter 2- Linear Structures
Objectives
To understand the concept of linear data structure called list
To learn the various implementation of lists such as array and linked list
To understand the use of data strcuctures
Operations performed
Traversal : Visit every part of the data structure
Search : Traversal through the data structure for a given element
Insertion : Adding new elements to the data structure
Deletion: Removing an element from the data structure.
5
Chapter 2- Linear Structures
1. Abstract Data Types (ADT's) are a model used to understand the design of a data
structure
2. 'Abstract ' implies that we give an implementation-independent view of the data structure
3. ADTs specify the type of data stored and the operations that support the data
4. Viewing a data structure as an ADT allows a programmer to focus on an idealized model
of the data and its operations
List ADT refers a linear collection of data items. A list L can be described as L ={a1, a2,
a3…an} Where n is the size of the list. The first element of the list is a1 and the last element is
an.In the list, ai+1 succeeds ai (i< n) and ai-1 precedes ai (i>1).
2.4 Array
6
Chapter 2- Linear Structures
Some arrays are multi-dimensional, meaning they are indexed by a fixed number of integers, for
example by a tuple of four integers. Generally, one- and two-dimensional arrays are the most
common.
Most programming languages have a built-in array data type.
• An array is a collection of similar data elements.
• The elements of the array are stored in consecutive memory locations and are referenced
by an index (also known as the subscript).
• Arrays are declared using the following syntax.
type name[size];
In computer science, a linked list is one of the fundamental data structures used in
computer programming. It consists of a sequence of nodes, each containing arbitrary data fields
and one or two references ("links") pointing to the next and/or previous nodes. A linked list is a
self-referential data type because it contains a link to another data of the same type. Linked lists
permit insertion and removal of nodes at any point in the list in constant time, but do not allow
random access.
7
Chapter 2- Linear Structures
The structure of a node consists of two parts namely, Data and Next.
Data Next
For example, the data stored in the above linked list are A1, A2, A3 and A4. Next field is
nothing but the address of where the next data is stored in the memory. The structure of a node
can be defined in C:
struct Linkedlist
{
int data;
struct Linkedlist *next;
};
typedef struct Linkedlist node;
1. Linearly-linked List
o Singly-linked list
o Doubly-linked list
2. Circularly-linked list
o Singly-circularly-linked list
o Doubly-circularly-linked list
8
Chapter 2- Linear Structures
A Linked list is a chain of structs or records called Nodes. Each node has at least two members,
one of which points to the next Node in the list and the other holds the data. The structure of
singly linked list is shown in Figure 2.2. The operations that can be performed in a singly linked
list are:
o Creating a linked list
o Insertion at linked list
o Deletion of an element from linked list
o Displaying the contents of the linked list
o Searching an element from the linked list
o Counting the number of nodes in a linked list
o Reversing the linked list
Algorithm
This strategy is fast and efficient, but each item is added to the head of the list.
9
Chapter 2- Linear Structures
Algorithm
void insert ( element_type x, LIST L, Position p )
{
Position newnode;
newnode = (Position) malloc (sizeof (struct node));
newnode->element = x;
newnode->next = p->next;
p->next = newnode;
}
Algorithm
void delete ( element_type x, LIST L )
{
Position p, temp;
p = find_previous( x, L );
if( p->next != NULL )
{
temp = p->next;
p->next = temp->next;
free (temp);
}
}
10
Chapter 2- Linear Structures
Algorithm
Position display()
{
Position *temp;
temp = head;
while(temp != NULL)
{
display (temp->data);
temp = temp->next;
}
return;
}
11
Chapter 2- Linear Structures
Algorithm
Algorithm
int count()
{
Position *temp;
int i=0;
temp = head;
while(temp!= NULL)
{
temp = temp->next;
i=i+1;
}
display i;
return;
}
12
Chapter 2- Linear Structures
Algorithm
Position reverse()
{
Position *temp,*temp1,*var;
temp=head;
var=NULL;
while(temp!=NULL)
{
temp1=var;
var=temp;
temp=temp->next;
var->next=temp1;
}
head=var;
}
13
Chapter 2- Linear Structures
clrscr();
while(1)
{
printf(“\t\t\t Linked List \n\n\n”);
printf(“\t 1. Create \n”);
printf(“\t 2. Insert \n”);
printf(“\t 3. Delete \n”);
printf(“\t 4. Display \n”);
printf(“\t 5. Count \n”);
printf(“\t 6. Reverse \n”);
printf(“\t 7. Exit \n”);
printf(“\t Enter Your Choice:\n”);
scanf(“%d”,&choice);
switch (choice)
{
case 1 : create();
break;
case 2 : insert();
break;
case 3 : delet();
break;
case 4 : display();
break;
case 5 : count();
break;
case 6: reverse();
break;
case 7: exit(0);
}/* switch end*/
} /*while end*/
}/*main end*/
14
Chapter 2- Linear Structures
newnode->next = NULL;
if(head == NULL)
head = newnode;
else
{
temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = newnode;
}
return;
}
if(head->data = = key)
{ newnode->next = head;
head = newnode;
}
else
{
temp = head;
while(temp->next != NULL)
{
if(temp->next->data = = key)
{
newnode->next = temp->next;
15
Chapter 2- Linear Structures
temp->next =newnode;
break;
}
temp=temp->next;
} /*while end */
} /*else end*/
return;
}
/* Function to delete an element */
void delet()
{
node *pr, *temp;
int key;
printf(“Enter the value to be deleted :”);
scanf(“%d”, &key);
if(head->data == key)
{
temp=head->next;
free(head);
head=temp;
}
else
{
temp=head;
while(temp->next !=NULL)
{
if(temp->next->data == key)
{
pr = temp->next->next;
free(temp->next);
temp->next=pr;
break;
} /*if end */
temp=temp->next;
} /*while end*/
}/* else end*/
return;
}
16
Chapter 2- Linear Structures
17
Chapter 2- Linear Structures
}
head=var;
}
The doubly linked list consists of two pointers, one pointing to the next item and the other
pointing to the preceding item. This makes life nice in many ways:
You can traverse lists forward and backward.
You can insert anywhere in a list easily. This includes inserting before a node, after a
node, at the front of the list, and at the end of the list and
You can delete nodes very easily.
A doubly linked list can be represented as follows:
struct dnode
{
int data;
struct dnode *next;
struct dnode *prev;
};
3 5 7
Head
18
Chapter 2- Linear Structures
Algorithm
Position create(DLIST L)
{
Position L;
L = (Position) malloc(sizeof (struct node) );
L-> next = NULL;
}
19
Chapter 2- Linear Structures
Algorithm
20
Chapter 2- Linear Structures
Algorithm
Position display()
{
Position *temp;
temp = head;
while(temp != NULL)
{
print(temp->data);
temp = temp->next;
}
return;
}
21
Chapter 2- Linear Structures
22
Chapter 2- Linear Structures
In circular linked list the pointer of the last node points to the first node.
Head
Figure 2.9 – Empty Linked List
Figure 2.9 shown an empty linked list. Here, we have an header node called Head and the
address field of header to NULL which means that there no no any element in the linked list.
Polynomial ADT
Radix Sort
Multilists - Sparse matrix representation
23
Chapter 2- Linear Structures
24
Chapter 2- Linear Structures
An example
Original, unsorted list:
170, 45, 75, 90, 802, 24, 2, 66
Sorting by least significant digit (1s place) gives: [*Notice that we keep 802 before 2, because
802 occurred before 2 in the original list, and similarly for pairs 170 & 90 and 45 & 75.]
170, 90, 802, 2, 24, 45, 75, 66
Sorting by next digit (10s place) gives: [*Notice that 802 again comes before 2 as 802 comes
before 2 in the previous list.]
802, 2, 24, 45, 66, 170, 75, 90
Sorting by most significant digit (100s place) gives:
2, 24, 45, 66, 75, 90, 170, 802
It is important to realize that each of the above steps requires just a single pass over the data,
since each item can be placed in its correct bucket without having to be compared with other
items.
Some LSD radix sort implementations allocate space for buckets by first counting the number of
keys that belong in each bucket before moving keys into those buckets. The number of times that
each digit occurs is stored in an array. Consider the previous list of keys viewed in a different
way:
170, 045, 075,090, 002, 024, 802, 066
The first counting pass starts on the least significant digit of each key, producing an array of
bucket sizes:
2 (bucket size for digits of 0: 170, 090)
25
Chapter 2- Linear Structures
2.13.3 Multilists
Our last example shows a more complicated use of linked lists. A university with 40,000
students and 2,500 courses needs to be able to generate two types of reports. The first report lists
the class registration for each class, and the second report lists, by student, the classes that each
student is registered for. The obvious implementation might be to use a two-dimensional array.
Such an array would have 100 million entries.
As the figure shows, we have combined two lists into one. All lists use a header and are circular.
To list all of the students in class C3, we start at C3 and traverse its list (by going right). The first
cell belongs to student S1. Although there is no explicit information to this effect, this can be
determined by following the student's linked list until the header is reached. Once this is done,
we return to C3's list (we stored the position we were at in the course list before we traversed the
student's list) and find another cell, which can be determined to belong to S3. We can continue
and find that S4 and S5 are also in this class. In a similar manner, we can determine, for any
student, all of the classes in which the student is registered.
26
Chapter 2- Linear Structures
Many languages, such as BASIC and FORTRAN, do not support pointers. If linked lists
are required and pointers are not available, then an alternate implementation must be used. The
alternate method we will describe is known as a cursor implementation.
27
Chapter 2- Linear Structures
The two important items present in a pointer implementation of linked lists are
1. The data is stored in a collection of structures. Each structure contains the data and a pointer to
the next structure.
2. A new structure can be obtained from the system's global memory by a call to malloc and
released by a call to free.
Algorithm
2. Is_empty()
This is used to check whether the linked list is empty or not. If the header of linked list is NULL
then return TRUE otherwise return FALSE.
Algorithm
int is_empty( LIST L ) /* using a header node */
{
return( CURSOR_SPACE[L].next == 0
}
3. Insert()
This procedure is used to insert an element x at the position p in the linked list L. This is done by
Create a new node as tmpcell
Assign the value x in the data filed of tmpcell
Identify the position p and assign the address of next cell of p to the pointer filed of
tmpcell
Assign the address of tmpcell to the address filed of p
28
Chapter 2- Linear Structures
Algorithm
4. Delete()
This is used to delete an element x from the linked list L. This is done by
Identify the element x in the linked list L using findprevious() function
Name the node x as tmpcell
Assign the address of next cell of x to the previous cell.
Remove the node x
Algorithm
5.Find()
This is used to check whether the element x is present in the linked list L or not. This is done by
searching an element x from the beginning of the linked list. If it is matched then return TRUE,
29
Chapter 2- Linear Structures
else move the next node and continue this process until the end of the linked list. If an element is
matched with any of the node then return FALSE.
Algorithm
2.15 Summary
There are two types of data structures. Array, linked list, stack and queue are linear data
structures. Trees and graphs are nonlinear data structures.
Linear Data structures, Nonlinear data structures, Abstract Data Type, Array, Linked list,
Doubly linked list, Circular linked list.
Big Questions
1. Write a program to implement a singly linked list
2. Write an procedure to perform insert and delete operation in doubly linked list
3. Explain in detail about the cursor implementation of linked list
30
Chapter 2- Linear Structures
31