0% found this document useful (0 votes)
2 views41 pages

Abstract Data Type

The document provides an overview of data structures, focusing on their definition, types, and implementations, particularly linked lists and arrays. It details operations associated with abstract data types (ADTs), including insertion and deletion routines for linked lists, along with pseudo-code examples. Additionally, it compares linked lists and arrays, highlighting the advantages of linked lists in terms of data manipulation.

Uploaded by

kanimozhisasi345
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
0% found this document useful (0 votes)
2 views41 pages

Abstract Data Type

The document provides an overview of data structures, focusing on their definition, types, and implementations, particularly linked lists and arrays. It details operations associated with abstract data types (ADTs), including insertion and deletion routines for linked lists, along with pseudo-code examples. Additionally, it compares linked lists and arrays, highlighting the advantages of linked lists in terms of data manipulation.

Uploaded by

kanimozhisasi345
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 41

Data Structure

• Data structure is a data organization,


management, and storage format that
enables efficient access and
modification.
• Data structure is a collection of data
values, the relationships among them,
and the functions or operations that can
be applied to the data.
Data Structure
• A Data Structure is an aggregation of atomic
and composite data into a set with defined
relationships.
• Structure means a set of rules that holds the
data together.
• Taking a combination of data and fit them into
such a structure that we can define its relating
rules, we create a data structure.
Some Data Structures
The Abstract Data Type (ADT)
• An abstract data type (ADT) is a set of
operations. Abstract data types are
mathematical abstractions; nowhere in an
ADT's definition is there any mention of how
the set of operations is implemented.
• This can be viewed as an extension of
modular design.
The Abstract Data Type (ADT)

• Objects such as lists, sets, and graphs,


along with their operations, can be viewed
as abstract data types, just as integers,
reals, and booleans are data types.
Typical ADTs:

1. Linear Data 2. Non-Linear Data


Structure: Structure:
• Lists • Trees

• Stacks • Heaps

• Queues • Graphs

• Arrays
LIST ADT
• We will deal with a general list of the form a1, a2, a3, . .
. , an. We say that the size of this list is n. We will call the
special list of size 0 a null list.
• For any list except the null list, we say that ai+l follows
(or succeeds) ai (i < n) and that ai-1 precedes ai (i > 1).
• The first element of the list is a1, and the last element is
an.
The List ADT: Operations
Popular choices:
• –insert(x, k) (add element at kth position)
• –delete(k) (delete elem. at kth position)
• –findKth (find element at position k)
• –find(x) (find position of element)
• –next (find element after a specified element)
• –previous (find element before a specified element)
• –printList (display list)
• –makeEmpty (get rid of contents)
LIST ADT Implementations

There are two basic structures we can use to


implement an ADT list: arrays and linked lists.
In this section we discuss the basic linked-list
implementation.
Array Implementations
• An array implementation allows print_list and find to be
carried out in linear time, which is as good as can be
expected, and the find_kth operation takes constant time.
• However, insertion and deletion are expensive.

• For example, inserting at position 0 (which amounts to


making a new first element) requires first pushing the entire
array down one spot to make room, whereas deleting the
first element requires shifting all the elements in the list up
one, so the worst case of these operations is O(n).
Array Implementations
• On average, half the list needs to be moved for
either operation, so linear time is still required.
• Merely building a list by n successive inserts would
require quadratic time.
• Because the running time for insertions and
deletions is so slow and the list size must be
known in advance, simple arrays are generally
not used to implement lists.
Linked Lists
• In order to avoid the linear cost of insertion and deletion, we need
to ensure that the list is not stored contiguously, since
otherwise entire parts of the list will need to be moved.
• The linked list consists of a series of structures, which are
not necessarily adjacent in memory.
• Each structure contains the element and a pointer to a
structure containing its successor.
• We call this the next pointer. The last cell's next pointer
points to NULL.
Linked Lists
• A Linked List is an ordered collection of data in which each
element contains the location of the next element or elements.
• In a linked list, each element contains two parts: data and one
or more links.
• The data part holds the application data – the data to be
processed.
• Links are used to chain the data together. They contain
pointers that identify the next element or elements in the list.
Example: Linked List

The above figure shows a linked list with a


header representing the list a1, a2, . . . , a5.
Linear and non-linear Linked Lists

• In linear linked lists, each element has


only zero or one successor.

• In non-linear linked lists, each element can


have zero, one or more successors.
Nodes
 A node is a structure that has two parts: the data and one or
more links.
 The nodes in a linked list are called self-referential structures. In
such a structure, each instance of the structure contains one or
more pointers to other instances of the same structural type.
Nodes

• The data part in a node can be a single


field, multiple fields, or a structure that
contains several fields, but it always acts
as a single field.
Linked Lists
• Recall that a pointer variable is just a variable that
contains the address where some other data is stored.
• Thus, if p is declared to be a pointer to a structure,
then the value stored in p is interpreted as the location,
in main memory, where a structure can be found.
• A field of that structure can be accessed by
pfield_name, where field_name is the name of the
field we wish to examine.
An Example of Linked List
• Consider the following figure: The list contains five structures, which
happen to reside in memory locations 1000, 800, 712, 992, and 692
respectively.
• The next pointer in the first structure has the value 800, which provides the
indication of where the second structure is.
• The other structures each have a pointer that serves a similar purpose. Of
course, in order to access this list, we need to know where the first cell
can be found.
• A pointer variable can be used for this purpose. It is important to
remember that a pointer is just a number.
• For the rest of this chapter, we will draw pointers with arrows, because
they are more illustrative.
A linked list examples
Linked list

Linked list with actual pointer values

Deletion from a linked list

Insertion into a linked list


Linked Lists vs. Arrays
• The major advantage of the linked list over the array is
that data are easily inserted and deleted.
• It is not necessary to shift elements of a linked list to
make room for a new elements or to delete an
element.
• However, because the elements are no longer
physically sequenced in a linked list, we are limited to
sequential searches.
Type Declarations of Linked List
struct node
{
element_type element;
node_ptr next;
};
Insertion routine for linked lists
• We will pass an element to be inserted along with the list L and
a position p. Our particular insertion routine will insert an
element after the position implied by p.
• This decision is arbitrary and meant to show that there are no
set rules for what insertion does.
• It is quite possible to insert the new element into position p
(which means before the element currently in position p), but
doing this requires knowledge of the element before position p.
• This could be obtained by a call to find_previous.
Illustration: Insertion
A B C

Item to be
tmp X inserted

A B C

curr
X

Spring 2012 Programming and Data Structure 25


Pseudo-code for insertion
struct nd {
int data;
struct nd * next;
} node;

void insert(node *curr)


{
node * tmp;

tmp=(node *) malloc(sizeof(node));
tmp->next=curr->next;
curr->next=tmp;
}
Spring 2012 Programming and Data Structure 26
Function to test whether a linked list is
empty
int
Is_empty( LIST L )
{
return( L->next == NULL );
} Empty list with
header
Function to test whether current position is
the last in a linked list

int

Is_last( position p, LIST L )

return( p->next == NULL );

}
Find routine
/* Return position of x in L; NULL if not found */
position
find ( element_type x, LIST L )
{
position p;
/*1*/ p = L->next;
/*2*/ while( (p != NULL) && (p->element != x) )
/*3*/ p = p->next;
/*4*/ return p;
}
Find routine
• Find, shown in above Figure, returns the
position in the list of some element.
• Line 2 takes advantage of the fact that the
and (&&) operation is short-circuited: if the
first half of the and is false, the result is
automatically false and the second half is not
executed.
Deletion routine for Linked Lists
• Our fourth routine will delete some element x in list L. We
need to decide what to do if x occurs more than once or not at
all.
• Our routine deletes the first occurrence of x and does nothing
if x is not in the list.
• To do this, we find p, which is the cell prior to the one
containing x, via a call to find_previous.
• The code to implement this is shown in following Figure . The
find_previous routine is similar to find and is shown in next
Figure.
Illustration: Deletion
Item to be deleted

A B C

tmp
curr

A B C

Spring 2012 Programming and Data Structure 32


Pseudo-code for deletion

void delete(node *curr)


{
node * tmp;
tmp=curr->next;
curr->next=tmp->next;
free(tmp);
}

Spring 2012 Programming and Data Structure 33


Program for Single LL
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
void create();
void insert();
void delet();
void display();
struct node
{
int data;
struct node *link;
};
struct node *first=NULL,*last=NULL,*next,*prev,*cur;
Create
void create()
{
cur=(struct node*)malloc(sizeof(struct node));
printf("\nENTER THE DATA: ");
scanf("%d",&cur->data);
cur->link=NULL;
first=cur;
last=cur;
}
Insert
void insert()
{
int pos,c=1;
cur=(struct node*)malloc(sizeof(struct node));
printf("\nENTER THE DATA: ");
scanf("%d",&cur->data);
printf("\nENTER THE POSITION: ");
scanf("%d",&pos);
if((pos==1) &&(first!=NULL))
{
cur->link = first;
first=cur;
}
Insert – contd…
else {
next=first;
while(c<pos) {
prev=next;
next=prev->link;
c++; }
if(prev==NULL) {
printf("\nINVALID POSITION\n"); }
else {
cur->link=prev->link;
prev->link=cur; } } }
Delete
void delet() first=NULL;
{ }
int pos,c=1; else if(pos==1 && first->link!=NULL)
printf("\nENTER THE POSITION : "); {
scanf("%d",&pos); cur=first;
if(first==NULL) first=first->link;
{ cur->link=NULL;
printf("\nLIST IS EMPTY\n"); printf("\n DELETED ELEMENT IS %d\
} n",cur->data);
else if(pos==1 && first->link==NULL) free(cur);
{ }
printf("\n DELETED ELEMENT IS %d\
n",first->data);
free(first);
Delete contd…
else if(next==NULL)
{ {
next=first; printf("\nINVALID POSITION\n");
while(c<pos) }
{ else
cur=next; {
next=next->link; printf("\n DELETED ELEMENT IS
c++; %d\n",next->data);
} free(next);
cur->link=next->link; }
next->link=NULL; }
}
void display()
{
cur=first;
while(cur!=NULL)
{
printf("\n %d",cur->data);
cur=cur->link;
}
}
void main()
{
int ch;
clrscr();
printf("\n\nSINGLY LINKED LIST");
do
{
printf("\n\n1.CREATE\n2.INSERT\n3.DELETE\n4.EXIT");
printf("\n\nENTER YOUR CHOICE : ");
scanf("%d",&ch);
switch(ch)
{ case 4:
case 1: exit(0);
create(); default:
display(); printf("Invalid choice...");
break; }
case 2: }while(1);
insert(); }
display();
break;
case 3:
delet();
display();
break;

You might also like