Sei sulla pagina 1di 28

Chapter 2- Linear Structures

Linear Structures 2

2.1 Linear Vs. Non Linear Data Structures


2.2 Abstract Data Type (ADT)
2.3 List ADT
2.4 Array
2.5 Linked list
2.6 Difference between Array and Linked List
2.7 Types of linked list
2.8 Singly linked list
2.9 Doubly linked list
2.10 Circular Linked List
2.11 Empty Linked List
2.12 Empty Circular Linked List
2.13 Applications of Linked List
2.14 Cursor Implementation of Linked List
2.15 Summary
2.16 Key Terms
2.17 Review Questions

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

2.1 Linear Vs. Non Linear Data Structures

Definition- Linear Data Structures


A data structure is said to be linear if its elements form a sequence or a linear list.
Examples
 Array
 Linked List
 Stacks
 Queues

Operations on linear Data Structures


 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.
 Sorting : Rearranging the elements in some type of order(e.g Increasing or Decreasing)
 Merging : Combining two similar data structures into one

Definition- Non Linear Data Structures:


A data structure is said to be nonlinear if its elements are not in a sequence, but in
hierarchical relationship.
Examples
 Tree
 Graph

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

2.2 Abstract Data Type (ADT)

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

2.3 List ADT

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).

Operations performed in the list are:


 Print list – Print the elements of the list
 Make empty – Make the list empty
 Find – The position of the first occurrence of a key
 Insert – Insert the element into the list
 Delete - Delete the element from the list
 Find Kth – Print the element in the Kth position

List can be implemented in three ways. They are:


i. Array implementation
ii. Linked list implementation
iii. Cursor implementation

2.4 Array

In computer programming, a group of homogeneous elements of a specific data type is


known as an array, one of the simplest data structures. Arrays hold a series of data elements,
usually of the same size and data type. Individual elements are accessed by their position in the
array. The position is given by an index, which is also called a subscript. The index usually uses
a consecutive range of integers, (as opposed to an associative array) but the index can have any
ordinal set of values.

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];

For example int marks[10];


1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th
element element element element element element element element element element

The limitations with arrays include:


• Arrays are of fixed size.
• Data elements are stored in continuous memory locations which may not be available
always.
• Adding and removing of elements is problematic because of shifting the elements from
their positions.

Figure 2.1 – Structure of an array

2.5 Linked List

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

Figure 2.2 – Structure of Linked List

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;

2.6Difference between Array and Linked list

SNo Arrays Linked List


1 Array is defined as collection of items A Linked list is a chain of structs or records
of similar data type 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
2 Static memory allocation Dynamic memory allocation
3 Wastage of memory space No wastage of memory
4 Insertion and delete takes more time Insertion and deletion takes less time

2.7Types of Linked List

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

2.8 Singly linked list

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

2.8.1 Creating a linked list


This operation is used to create an empty linked list without any data. This is done by create a
header node L and assign the address field of header to NULL.

Algorithm

Position create (LIST L)


{
Position L;
L = (position) malloc (sizeof (struct node) );
L-> next = NULL;
}

2.8.2 Insertion at linked list


This operation is used to insert an element in to the linked list. This is done by
Step 1 : Allocate space for a new node
Step 2 : Copy the item into the data field
Step 3 : Make the new node's next pointer point to the current head of the list and
Step 4 : Make the head of the list point to the newly allocated node.

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;
}

Figure 2.3 – Insert an element into the singly linked list

2.8.3 Deletion of an element from linked list


This is used to delete an element from the linked list. This is done by
Step 1: Identify the position of the element to be deleted
Step2: Rearrange the pointers
Step3: Free the memory allocated the deleted element

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

Figure 2.4 – Delete an element from singly linked list

2.8.4 Displaying the contents of the linked list


This is used to display the list of elements from the singly linked list. If the linked list is empty,
then there are no elements in the singly linked list. Otherwise, display the element from the first
cell of the linked list, second element of the linked list and so on until the end of eth linked list.

Algorithm

Position display()
{
Position *temp;
temp = head;
while(temp != NULL)
{
display (temp->data);
temp = temp->next;
}
return;
}

2.8.5 Searching an element from the linked list


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,
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.

11
Chapter 2- Linear Structures

Algorithm

Position find ( element_type x, LIST L )


{
Position p;
p = L->next;
while( (p != NULL) && (p->element != x) )
p = p->next;
return p;
}

2.8.5 Counting the number of nodes in a linked list


This is used to display the number of elements that are stored in the singly list. If there is no
element is there, then it displays 0. Otherwise, it displays the number of items.

Algorithm

int count()
{
Position *temp;
int i=0;
temp = head;
while(temp!= NULL)
{
temp = temp->next;
i=i+1;
}
display i;
return;
}

2.8.6 Reversing the linked list


This is used to display the elements in the singly linked list in reverse order which means that the
last item in the linked list is displayed first.

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;
}

2.8.7 “C” implementation of Singly linked list


/* C Program for linked list creation, insertion, deletion, counting and reversing of a
Linked list*/
# include <stdio.h>
# include <stdlib.h>
# include <conio.h>
# define NULL 0
struct linkedlist
{
int data;
struct linkedlist *next;
};
typedef struct linkedlist node;
node *head = NULL;
void main()
{
int choice;
void create();
void insert();
void delet();
void display();

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*/

// creation of linked list


void create()
{
node *newnode, *temp;
newnode = (node *)malloc(sizeof(node));
printf(“Enter the data field :”);
scanf(“%d”,&newnode->data);

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;
}

/*Function to insert an element*/


void insert()
node *newnode, *temp;
int key;
newnode = (node *)malloc(sizeof(node));
printf(“Enter the value to be inserted:”);
scanf(“%d”,&newnode->data);
if(head == NULL)
head = newnode;
else
{
printf(Enter before which node to insert”);
scanf(“%d”,&key);

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

/* Function to display the elements */


void display()
{
node *temp;
temp=head;
while(temp != NULL)
{
printf(“%d---------- >”, temp->data);
temp = temp->next;
}
return;
}

/* Function to count the number of elements in the list*/


void count()
{
node *temp, int i=0;
temp = head;
while(temp!= NULL)
{
temp = temp->next;
i=i+1;
}
printf(“Number of elements in the list is : %d”, i);
return;
}

/* Function to reverse the elements in the linked list


void reverse()
{
node *temp,*temp1,*var;
temp=head;
var=NULL;
while(temp!=NULL)
{
temp1=var;
var=temp;
temp=temp->next;
var->next=temp1;

17
Chapter 2- Linear Structures

}
head=var;
}

2.9 Doubly Linked List

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:

Prev Data Next

Each node contains three parts:


1. A data field which contains the element.
2. A pointer field next which contains the location of the next node in the list.
3. A pointer field prev which contains the location of the preceding node in the list.

The structure for doubly linked list is:

struct dnode
{
int data;
struct dnode *next;
struct dnode *prev;
};

Prev Data Next

3 5 7
Head

Figure 2.5 – Structure of Doubly Linked List

18
Chapter 2- Linear Structures

Doubly Linked List ADT


 Creating a doubly linked list
 Insertion at doubly linked list
 Deletion of an element from doubly linked list
 Displaying the contents of the linked list

2.9.1 Creating a doubly linked list


This operation is used to create an empty doubly linked list without any data. This is done by
create a header node L and assign the address field of header to NULL.

Algorithm

Position create(DLIST L)
{
Position L;
L = (Position) malloc(sizeof (struct node) );
L-> next = NULL;
}

2.9.2 Insertion at doubly linked list


This procedure is used to insert an element x after the node p in the doubly linked list. This is
done by,
 Create a new node and assign the value x into the data field of new node.
 Rearrange the pointers by assigning the address of x to the pointer field of p and vice
versa
 Assigning the address of next cell of p to the address filed of x and vice versa

Figure 2.6 – Insert an element into the doubly linked list

19
Chapter 2- Linear Structures

Algorithm

void insert( element_type x, DLIST L, Position P )


{
temp=head;
while(temp->next !=NULL && temp->next->data <= element)
temp=temp->next;
P=temp->next;
Newnode->next = temp->next;
Newnode->prev=temp;
temp->next=Newnode;
P->prev=Newnode;
}

2.9.3 Deletion of an element from doubly linked list


This is used to remove a node B from the doubly linked list. Removing a node from the middle
requires that the preceding node skips over the node being removed, and the following node to
skip over in the reverse direction.
Algorithm

void delete( element_type x, LIST L )


{
temp=head;
while(temp->next!=NULL)
{
if(temp->next->data = =key)
{
Pr=temp->next->next;
free(temp->next);
temp->next=pr;
break;
}
temp=temp->next;
}
if(temp->next = = NULL)
printf(“Key Not Found”);
}

20
Chapter 2- Linear Structures

Figure 2.7 – Delete an element from doubly linked list

2.9.4 Displaying the contents of the linked list


This is used to display the list of elements from the doubly linked list. This is also same to the
singly linked list, since, we use only the next pointer to forward the next cell in the doubly linked
list. Here, if the header field of the doubly linked list is empty, then display NULL. Otherwise,
display the element from the first cell of the doubly linked list; use the next pointer, display
second element of the doubly linked list and so on until the end of the linked list.

Algorithm

Position display()
{
Position *temp;
temp = head;
while(temp != NULL)
{
print(temp->data);
temp = temp->next;
}
return;
}

21
Chapter 2- Linear Structures

2.9.5 C implementation of doubly Linked List


Write a C program for implementing doubly linked list.(Hint. Use the doubly linked list
Algorithm)

22
Chapter 2- Linear Structures

2.10 Circular Linked List

In circular linked list the pointer of the last node points to the first node.

Figure 2.8 – Circular Linked List


Advantages
 It allows to traverse the list starting at any point.
 It allows quick access to the first and last element.
 It allows to traverse the list in either direction.

2.11 Empty Linked List

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.

2.12 Empty Circular Linked List

Figure 2.10 – Empty Circular Linked List


Empty circulat linked list is one of the type of linked list in which the address of the last cell is
pointed to the header node. The figure 2.10 shown an empty circulatr linked list.

2.13 Applications of Linked List

 Polynomial ADT
 Radix Sort
 Multilists - Sparse matrix representation

23
Chapter 2- Linear Structures

2.13.1 The polynomial ADT


The polynomial can be represented by using linked list. Here, each node that consists of 2 data
field (coefficient and exponent) and one pointer field that points the address of the next cell.

Figure 2.11 – Polynomial ADT


Algorithm

void add_polynomial( POLYNOMIAL poly1, POLYNOMIAL poly2,


POLYNOMIAL poly_sum )
{
int i;
zero_polynomial(poly_sum );
poly_sum->high_power = max( poly1->high_power,poly2->high_power);
for( i=poly_sum->high_power; i>=0; i-- )
poly_sum->coeff_array[i] = poly1->coeff_array[i]+ poly2->coeff_array[i];
}

voidmult_polynomial( POLYNOMIAL poly1, POLYNOMIAL poly2,


POLYNOMIAL poly_prod )
{
unsigned int i, j;
zero_polynomial(poly_prod );
poly_prod->high_power = poly1->high_power+ poly2->high_power;
if(poly_prod->high_power> MAX_DEGREE )
error("Exceeded array size");
else
for( i=0; i<=poly->high_power; i++ )
for( j=0; j<=poly2->high_power; j++ )
poly_prod->coeff_array[i+j] +=poly1->coeff_array[i] * poly2->coeff_array[j];
}

24
Chapter 2- Linear Structures

2.13.2 Radix sort


Radix sort is one of the linear sorting algorithms for integers. It functions by sorting the input
numbers on each digit, for each of the digits in the numbers. Each key is first figuratively
dropped into one level of buckets corresponding to the value of the rightmost digit. Each bucket
preserves the original order of the keys as the keys are dropped into the bucket. There is a one-
to-one correspondence between the number of buckets and the number of values that can be
represented by a digit. Then, the process repeats with the next neighboring digit until there are no
more digits to process. In other words:
1. Take the least significant digit (or group of bits, both being examples of radices) of each
key.
2. Group the keys based on that digit, but otherwise keep the original order of keys. (This is
what makes the LSD radix sort a stable sort).
3. Repeat the grouping process with each more significant digit.
The sort in step 2 is usually done using bucket sort or counting sort, which are efficient in this
case since there are usually only a small number of digits.

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 (bucket size for digits of 2: 002, 802)


1 (bucket size for digits of 4: 024)
2 (bucket size for digits of 5: 045, 075)
1 (bucket size for digits of 6: 066)
A second counting pass on the next more significant digit of each key will produce an array of
bucket sizes:
2 (bucket size for digits of 0: 002, 802)
1 (bucket size for digits of 2: 024)
1 (bucket size for digits of 4: 045)
1 (bucket size for digits of 6: 066)
2 (bucket size for digits of 7: 170, 075)
1 (bucket size for digits of 9: 090)
A third and final counting pass on the most significant digit of each key will produce an array of
bucket sizes:
6 (bucket size for digits of 0: 002, 024, 045, 066, 075, 090)
1 (bucket size for digits of 1: 170)
1 (bucket size for digits of 8: 802)

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

Figure 2.12 – Muilti list

2.14 Cursor Implementation of Linked List

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.

Slot Element Next


0 - 6
1 B 9
2 F 0
3 Header 7
4 - 0
5 Header 10
6 - 4
7 C 8
8 D 2
9 E 0
10 A 1

Figure 2.13 – Cursor implemetation of linked list

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.

Circular Linked List ADT


1. Is_last()
This procedure is used to check whether the position p is an end of the linked list L

Algorithm

int is_last( position p, LIST L) /* using a header node */


{
return( CURSOR_SPACE[p].next == 0
}

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

void insert( element_type x, LIST L, Position p )


{
Position tmp_cell;
tmp_cell = cursor_alloc( );
CURSOR_SPACE[tmp_cell].element = x;
CURSOR_SPACE[tmp_cell].next = CURSOR_SPACE[p].next;
CURSOR_SPACE[p].next = tmp_cell;
}

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

void delete( element_type x, LIST L )


{
Position p, tmp_cell;
p = find_previous( x, L );
if( !is_last( p, L) )
{
tmp_cell = CURSOR_SPACE[p].next;
CURSOR_SPACE[p].next = CURSOR_SPACE[tmp_cell].next;
cursor_free(tmp_cell );
}
}

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

Position find( element_type x, LIST L) /* using a header node */


{
Position p;
CURSOR_SPACE[L].next;
while( p && CURSOR_SPACE[p].element != x )
p = CURSOR_SPACE[p].next;
return p;
}

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.

2.15 Key Terms

Linear Data structures, Nonlinear data structures, Abstract Data Type, Array, Linked list,
Doubly linked list, Circular linked list.

2.16 Review Questions

Two Mark Questions


1. What is linear and non linear data structure?
2. Define array and linked list.
3. Differentiate between arrays and linked list.
4. What are the types of linked list?
5. Define doubly linked list and draw a diagram of a doubly linked list.
6. Define circular linked list.
7. What are the applications of 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

4. Write an algorithm for the following operations on a singly linked list:


(a) Sum up the values stored in the nodes of a list.
(b) Determine the average of the numbers stored in the linked list.
(c) Count the even numbers in a list.
(d) Concatenate two lists.
(e) Split an ordered list into two – one with values greater than an index and the
other with values less than an index.
(f) Delete the node with an element x in (i) ordered (ii) unordered linked list
5. Write an algorithm to insert and delete an element in a sorted Doubly linked list.
(a) Write an algorithm to perform each of the following:
i. Reverse a list, so that last element comes first and so on.
ii. Return the sum of the integers in a list.
iii. Delete every third element from a list.

31

Potrebbero piacerti anche