0% found this document useful (0 votes)
35 views14 pages

DSA Unit II

The document discusses linear data structures and array and linked list implementations of lists. It describes sequential and linked allocation of memory and the operations of singly and doubly linked lists like insertion and deletion. Array implementation of lists and the list abstract data type are also covered.

Uploaded by

kolavennela90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views14 pages

DSA Unit II

The document discusses linear data structures and array and linked list implementations of lists. It describes sequential and linked allocation of memory and the operations of singly and doubly linked lists like insertion and deletion. Array implementation of lists and the list abstract data type are also covered.

Uploaded by

kolavennela90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Unit 2- Linear Data Structure

Linear lists , Sequential and linked allocation, list ADT, array and linked
Implementations, Singly Linked List-Opertion-Insertion-Deletion, Doubly
Linked List-Operation-Insertion, Deletion.

Sequential Allocation
Sequential allocation refers to the arrays or a contiguous block of memory.
 It is hard to Insert and delete files and requires movement to do so.
 Space is wasted.
 Expensive.
 It requires less space as only information is stored.
 It cannot be extended or reduced as per the requirements.
 Similar amount of time is required to access every element.
 Elements are stored in consecutive memory locations.
 A direct route is present to reach element individually.

Dept of CS & AI , SR UNIVERSITY.


Linked Organization
The Linked allocation means the linked lists whose data structures areconnected by
various nodes.
A node carries at least two pieces of information, some data and a referencelink
(pointing to next field / node).
 Insertions and deletions can be done easily
without requiring any movement.
 Space is not wasted.
 Inexpensive.
 More space is required for the pointers
which are also stored along with
information.
 Size is not fixed.
 It can be extended or reduced
according to requirements.
 Different amount of time is required to
access every element.
 Elements might or might not be storedin
consecutive memory locations.
 There is no direct route to reach an
element.

Abstract Data Types


Abstract data type (ADT) is a specification of a set of data and the set of operations that
can beperformed on the data.
The abstract data type is written with the help of instances and operations. In ADT
instances represent the elements on which various operations can be performed.
Examples,
Array, stack, queue, tree, etc.,
Array as an ADT:
AbstractDataType Array
{
Instances:
An array A of some size, index i and total number of elements in the array n
Operations:
Store() – this operation stores the desired elements at each successive location.
Display() – this operation displays the elements of the array.
}

Dept of CS & AI , SR UNIVERSITY.


ADT is useful to handle the data type correctly. Always what is to be done is given in
ADT but how is to be done is not given in ADT. Note that we have only given what are the
operations in arrays in above example. But how it is to be done is not given.

List ADT
 A list is a ordered tuple of homogeneous elements
A0, A1, A2, …., An-1
where Ai is the i-th element of the list
 The position of element Ai is i; positions range from 0 to n-1 inclusive
 The size of a list is N (a list with no elements is called an “empty list”.

 Generic Operations on a list:


1. Create an empty list
2. printList – prints all elements in the list
3. find(x) – returns the position of the first occurrence of x.
4. remove(x) – removes x from the list if present
5. insert(x,postion) – inserts x into the list at the specified position.
6. isEmpty() – returns true if the list has no elements
7. findKth(int k) –returns the element in the specified position.
8. Sort() – sort the elements in ascending or descending order.

Array implementation of List


An array is a collection of elements of similar data type that are referred through a common
name. It is simply a grouping of similar data items.
The elements in the array are stored in contiguous memory locations. A specific element in
an array isaccessed by an index or subscript. Subscript or index should not be negative. The first
element of array willhave index as zero.

One-Dimensional Arrays:
A list of items can be given one variable name using only one subscript and such a variable
is called single-subscripted variable or one dimensional array. A one dimensional array can
be declared as follows:

General syntax: Example:


Datatype array_name[size_of_array]; int a[7];

Dept of CS & AI , SR UNIVERSITY.


The array ‘a’ of size 7, has all the elements which are of integer type. Let us understand such an
arrangement of array elements by following figure 1.1

a[0] a[1] a[2] a[3] a[4] a[5] a[6]

10 20 30 40 50 60 70

Figure. 1.1 Array a[7]


Now let us see how to handle this array. We will write a simple C program in which we are
simply goingto store the elements and then we will print those stored elements.
#include<stdio.h>
void main()
{
int a[7],i;
printf(“\nEnter the Elements in an Array a:”);
for(i=0;i<=6;i++)
scanf(“%d”,&a[i]);
printf(“\n You have Stored these numbers in the array a:”);
for(i=0;i<=6;i++)
printf(“\n%d”,a[i]);
}

Memory Representation of One-Dimensional Array:


The array name holds the address of the first element. It is called as BASE ADDRESS
of thatarray. The base address can’t be modified during execution, because it is static.
Consider the first element is stored in the address of 1020. It will look like in figure 1.2,
int a[7];

Address 1020 1022 1024 1026 1028 1030 1032

10 20 30 40 50 60 70
Index 0 1 2 3 4 5 6

a[0] means a + 0  1020 + 0  1020 (locates the 1020)


a[1] means a + 1  1020 + 1 * size of datatype  1020 + 2  1022[ for ‘int’ size is 2 byte]
a[2] means a + 2  1020 + 2 * size of datatype  1020 + 4  1024
a[3] means a + 3  1020 + 3 * size of datatype  1020 + 6  1026
a[4] means a + 4  1020 + 4 * size of datatype  1020 + 8  1028

Dept of CS & AI , SR UNIVERSITY.


Array Implementation of List:
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
int a1, a[20], n, p, elt, f, i, pos,flag=0,s;
void create()
{
printf("\n Enter the number of nodes");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n Enter the Element:");
scanf("%d",&a[i]);
}
}
void deletion()
{
printf("\n Enter the position u want to delete::");
scanf("%d", &pos);
if(pos>=n)
{
printf("\n Invalid Location::");
}
else
{
for(i=pos+1;i<n;i++)
a[i-1]=a[i];
n--;
}
printf("\n The Elements after deletion");
for(i=0;i<n;i++)
printf("\t%d", a[i]);
}
void search()
{
printf("\n Enter the Element to be searched:");
scanf("%d", &s);
for(i=0;i<n;i++)
{
if(a[i]==s)
{
printf("Element Found at position %d",i);
break;
}
}
if(i==n)
printf("Not Found");
}

Dept of CS & AI , SR UNIVERSITY.


void insert()
{
printf("\n Enter the position u need to insert::");
scanf("%d", &pos);
if(pos>=n)
printf("\n invalid Location::");
else
{
for(i=MAX-1;i>=pos-1;i--)
{
a[i+1]=a[i];
}
printf("\n Enter the element to insert::\n");
scanf("%d",&elt);
a[pos]=elt;
n++;
}
printf("\n The list after insertion::\n");
display();
}
void display()
{
printf("\n The Elements of The list ADT are:");
for(i=0;i<n;i++)
printf("\n\n%d", a[i]);
}
void main()
{
int ch;
char g='y';
do
{
printf("\n main Menu");
printf("\n 1.Create \n 2.Delete \n 3.Search \n 4.Insert \n 5.Display\n 6.Exit \n");
printf("\n Enter your Choice");
scanf("%d", &ch);
switch(ch)
{
case 1: create(); break;
case 2: deletion(); break;
case 3: search(); break;
case 4: insert(); break;
case 5: display();break;
case 6: exit(0); break;
default: printf("\n Enter the correct choice:");
}
printf("\n Do u want to continue:::");
scanf("\n%c", &g);
}while(g=='y'||g=='Y');
}

Dept of CS & AI , SR UNIVERSITY.


Linked Lists implementation
A linked list is a linear collection of data elements, called nodes, where the linear order
is given by means of pointers.
Types of Linked Lists:
There are different kinds of linked list. They are
1. Singly Linked List
2. Circular Linked List
3. Two-way or doubly linked lists
4. Circular doubly linked lists
Singly-linked list
A singly linked list is a linear collection of data elements, called nodes. Each and every
node hastwo parts:
 The first part contains the information of the element i.e. INFO(DATA).
 The second part is NEXT, which contains the address of the next node in the list.

Dept of CS & AI , SR UNIVERSITY.


 The linked list consists of series of nodes, which are not necessarily adjacent in memory.
 A list is a dynamic data structure i.e. the number of nodes on a list may vary
dramatically aselements are inserted and removed.
 The pointer of the last node contains a special value, called the null pointer. This null
pointer
signals the end of list.
The list with no nodes on it is called the empty list or null list.
 Example:
The single linked list with 4 nodes is shown below:

Start/Head/First Node Last Node

10 20 30 40 Null
Figure. 1.2 Singly Linked List containing four integer values

Basic Operations:
1. Insertion
a. At first
b. At last
c. At a given location or given data item (At middle)
2. Deletion
a. First Node
b. Last Node
c. Node in given location or given data item
Initial Condition
HEAD or START = NULL;
Address of the first node in the list is stored in HEAD/START. Initially there is no node in the
list. So, HEAD/START isinitialized to NULL (No address).

Insertion Operation
1. Insertion At First
void insert_at_beg()
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("\n Enter the data to insert at begin: ");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(start==NULL)
start=newnode;
else
{
newnode->next=start;

Dept of CS & AI , SR UNIVERSITY.


start=newnode;
}
}
2. Insertion At Mid
void insert_at_end()
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the data to insert at end: ");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(start==NULL)
{
start=newnode;
temp=newnode;
}
else
{
temp = start;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = newnode;
}
}
3. Insertion At End
void insert_mid()
{
int key;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\n Enter the data to insert at middle: ");
scanf("%d",&newnode->data);
if(start==NULL)
start=newnode;
else
{
printf("\n Enter the element after which you want to insert: ");
scanf("%d",&key);
temp=start;
do
{
if(temp->data==key)
{
newnode->next=temp->next;
temp->next=newnode;

Dept of CS & AI , SR UNIVERSITY.


return;
}
else
temp=temp->next;
}while(temp!=NULL);
}
}

Deletion Operation
1. Delete At First
void del_at_beg()
{
temp=start;
start=start->next; /*First element deleted*/
free(temp);
}

2. Delete At Mid
void del_at_mid()
{
int key;
printf("\n Enter element to delete..");
scanf("%d",&key);
p=start;

while(p->next!= NULL)
{
if(p->next->data==key) /*Element deleted in between*/
{
temp=p->next;
p->next=temp->next;
free(temp);
}
p=p->next;
}
}

3. Delete At End
void del_at_end()
{
temp=start;
while(temp->next!= NULL)
{
p=temp;
temp=temp->next;
}
free(p->next);
p->next=NULL;
}

Dept of CS & AI , SR UNIVERSITY.


Circular-linked list
In a circular-linked list, the first and final nodes are linked together. This can be done
for both singly and doubly linked lists. To traverse a circular linked list, we begin at any node
and follow the list in either direction until we return to the original node.
Singly-circular-linked list
In a singly-circular-linked list, each node has one link, similar to an ordinary singly-
linked list,except that the next link of the last node points back to the first node.
First /Start/Head Node

10 20 30 40

****************
Doubly-Linked List
A more sophisticated kind of linked list is a doubly-linked list or two-way linked list,
in which each node has two links: one points to the previous node, or points to a null value or
empty list if it is the first node; and one points to the next, or points to a null value or empty list
if it is the final node.
*prev data *next

Null 15 56 27 Null
First Node Last Node
Figure : Doubly Linked List

Basic Operations:
1. Insertion
a. At first
b. At last
c. At a given location or given data item (At middle)
2. Deletion
a. First Node
b. Last Node
c. Node in given location or given data item

Insertion Operation
1. Insertion At First
void insert_at_beg()
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the data to insert at begin: ");
scanf("%d",&newnode->data);
newnode->next=NULL;
newnode->prev=NULL;
if(start==NULL)
start=newnode;

Dept of CS & AI , SR UNIVERSITY.


else
{
newnode->next=start;
start->prev=newnode;
start=newnode;
}
}

2. Insertion At Mid
void insert_mid()
{
int key;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the data to insert at middle: ");
scanf("%d",&newnode->data);
newnode->next=NULL;
newnode->prev=NULL;
if(start==NULL)
start=temp=newnode;
else
{
printf("\nEnter the element after which you want to insert: ");
scanf("%d",&key);
temp=start;
do
{
if(temp->data==key)
{
newnode->next=temp->next;
temp->next->prev=newnode;
temp->next=newnode;
newnode->prev=temp;
return;
}
else
temp=temp->next;
}while(temp!=NULL);
}
}

Dept of CS & AI , SR UNIVERSITY.


3. Insertion At End
void insert_at_end()
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the data to insert at last: ");
scanf("%d",&newnode->data);
newnode->next=NULL;
newnode->prev=NULL;
if(start==NULL)
start=temp=newnode;
else
{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
newnode->prev=temp;
}
}
Deletion Operation
1. Delete At First
void del_at_beg()
{
temp=start;
start=start->next; /*First element deleted*/
free(temp);
start->prev=NULL;
}
2. Delete At Mid
void del_at_mid()
{
int key;
printf("\nEnter ele to delete..");
scanf("%d",&key);
p=start;
while(p->next!= NULL)
{
if(p->next->data==key)
{
temp=p->next;
p->next=temp->next;
temp->next->prev=p;
free(temp);
}

Dept of CS & AI , SR UNIVERSITY.


p=p->next;
}
}

3. Delete At End
void del_at_end()
{
temp=start;
while(temp->next!= NULL)
{
p=temp;
temp=temp->next;
} /*Last element deleted*/
free(temp);
p->next=NULL;
}
Doubly-circular-linked list
In a doubly-circular-linked list, each node has two links, similar to a doubly-linked
list, except that the previous link of the first node points to the last node and the next link of
the last node points to the first node. As in doubly-linked lists, insertions and removals can be
done at any point with access to any nearby node.

First Node Last Node


5 10 15 20

Figure 1.10 Doubly Circular Linked List


Advantages of Linked List
1. Linked List is dynamic data structure; the size of a list can grow or shrink during
the programexecution. So, maximum size need not be known in advance.
2. The Linked List does not waste memory
3. It is not necessary to specify the size of the list, as in the case of arrays.
4. Linked List provides the flexibility in allowing the items to be rearranged.
Pitfalls encountered in single linked list
1. A singly linked list allows traversal of the list in only one direction.
2. Deleting a node from a list requires keeping track of the previous node, that is, the
node whoselink points to the node to be deleted.
3. If the link in any node gets corrupted, the remaining nodes of the list become unusable.
Applications of linked list
 To implement of Stack, Queue, Tree, Graph etc.,
 Polynomial Manipulation
 To maintain Free-Storage List in memory

***********************

Dept of CS & AI , SR UNIVERSITY.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy