SlideShare a Scribd company logo
VELAMMAL ENGINEERING COLLEGE
An Autonomous Institution, Affiliated to Anna University
Chennai, & Approved by AICTE Delhi
Online Classes on Data Structures ( 19CS208T )
CLASS: II YEAR AUTO
PRESENTED
SARANYA P
AP / DEPT OF CSE
DATA+STRUCTURE
Data
Values or set of values of
different data types such as
int, float, etc.,
Way of organizing and
storing information so
that it is easy to use
Structure
Data +
Structure
Way of organizing and storing
data on a computer so that it
can be used easily and
effectively
DATA STRUCTURE
•Data Structure is defined as the group of data
elements provides an efficient way of storing or
organising data in a computer.
•It is used in almost every program or software
system.
Examples: Arrays, Linked lists, Stack and
Queue.
COURSE OBJECTIVES
• To implement the ADTs stack, queue, list
• To understand the performance of the
implementations of non linear data
structures.
• To understand the performance of various
sorting and searching algorithms.
UNIT I-INTRODUCTION TO DATA
STRUCTURE
Abstract Data Types (ADT) – List ADT – array-based
implementation – issues in array based implementation
– Singly linked list: Creation, insertion at first, insertion
at middle, insertion at end, deletion at first, deletion at
middle and deletion at end and display – doubly-linked
lists: Creation, insertion, deletion and display -
Application of lists – Polynomial manipulation:
creation, Addition and subtraction.
UNIT II-LINEAR DATA
STRUCTURES
Stack ADT – Operations: push and pop –
Applications –Arithmetic Expressions – convert of
infix expression to postfix Expressions-Queue ADT
– operations: Enqueue and Dequeue – issues in
queue. Application of queue: circular Queue -
Creation, insertion, deletion and display.
Data structure - STACK
Data structure - Queue
UNIT III -TREE STRUCTURES
Tree ADT – Binary Tree Representation – Binary
Search Tree ADT – Insertion –Deletion- FindMin –
FindMax- Expression trees – Tree traversals: in-order
traversals, pre-order traversals, post-order traversals .
Balanced Tree: Adelson Velski Landis tree(AVL):
Single rotation with right, Single rotation with left,
Double rotation with left and double rotation with right.
UNIT IV - GRAPHS
Definitions – Types of graph – Directed graph,
UnDirected graph, Weighted graph, Unweighted Graph,
Cyclic and Acyclic graph, Complete graph -
Representation –Adjacency Matrix representation –
Adjacency List Representation – Topological sort -
shortest-path algorithms- Dijkstra’s Algorithm –
Minimum spanning tree – Prim's algorithm - Kruskal's
algorithms.
UNIT V - ALGORITHMS DESIGN
AND ANALYSIS
Greedy algorithms – Divide and conquer –
Dynamic programming – back tracking –branch
and bound – Randomized algorithms –
algorithm analysis – asymptotic notations –
recurrences – NP complete problems.
GRAPHS
TREE
COURSE OUTCOMES
• Apply abstract data types for various linear an non linear
data structures.
• Illustrate the role of key pre-processing algorithms in
various data structures.
• Use knowledge to balance a Binary Search trees.
• Distinguishes various graph algorithms for finding
minimum path.
• Choose various memory models to represent static and
dynamic Hashed structures.
• Understand the concepts of various searching and sorting
algorithms.
TEXT BOOKS
1. M. A. Weiss, “Data Structures and Algorithm
Analysis in C”, Second Edition , Pearson Education,
2016.
2. Reema Thareja, “Data Structures Using C”, Second
Edition , Oxford University Press, 2011
REFERENCE BOOKS
1. R. F. Gilberg, B. A. Forouzan, “Data
Structures”, Second Edition, Thomson India
Edition, 2005.
2. Ellis Horowitz, Sartaj Sahni, Susan Anderson-
Freed, “Fundamentals of Data Structures in
C”, Second Edition, University Press, 2008
ONLINE LINKS TO REFER
• https://www.geeksforgeeks.org/data-structures/
• https://www.tutorialspoint.com/data_structures
_algorithms
• https://www.w3schools.com
• www.tutorialspoint.com
UNIT 1
Introduction to Data
Structure
What is Data structure
• Group of Data Elements provides an efficient
way of Storing and organising data in
computer.
Example: Array, Linked List, Stack & Queue.
Classification of Data structure
Data structure are generally classified into two classes:
1. Primitive Data Structure
2. Non- Primitive Data Structure
Primitive DS:
The Fundamental Data types which are supported by
the programming language.
Some of the primitive data types are integer, real,
character, Boolean.
Non-Primitive DS:
Data structures which are created using primitive data
structures.
Some of the Non-Primitive data types are Trees,
Graphs, Stacks and Lists.
They are further classified into Linear and Non-Linear
Data structure.
Data Structure
Linear Data Structure Non-Linear Data
Structure
Static Dynamic Tree Graph
Stack
Queue
Linked List
Array
Definitions:
1.Primitive / Non-Primitive DS
2.Linear / Non-Linear DS
3.Homogeneous / Non-homogeneous
DS
4. Static /Dynamic DS
5.Contiguous / Dynamic DS
Data Structure
Primitive
int char float boolean
Non - Primitive
Linear
Array Linked List Stack Queue
Non - Linear
Tree Graph
Linear Structures Non-Linear Structures
Elements of DS are stored in a
linear or sequential order.
Examples: Arrays, linked lists,
stacks, and queues.
Represented in 2 ways.
1. By means of sequential
memory locations.
2. By means of links.
Single level of elements.
•Elements of a data structure are
not stored in a sequential order.
Examples: Trees and graphs.
•The relationship of adjacency is
not maintained between
elements.
•Can have Multiple Level of
Elements.
•Traversal is not easy as linear
Structures.
Operations of Data Structure
The different operations that can be performed
under Data structure are,
1. Traversing
2. Searching
3. Inserting
4. Deleting
5. Sorting
6. Merging Lists
Abstract Data Types
• Mathematical model for data types are known
as Abstract Data Types or ADT.
• ADT is a special kind of data type defines by
set of value and set of operations.
• ADT is made of with primitive data types, but
operation logics are hidden.
• ADT lets user to know what a data type can do
but not how it will be implemented.
Example: Stack, Queue, List.
ABSTRACT DATA TYPE:
1. ADT is a mathematical abstractions which
means representing the implementation of the
set of operations by using the modular design.
2. Example: List, sets and graphs along with
their operations.
3. Implementing ADT is that operations are
written once in a program and can be called
by any part of the program.
ADT- Abstract Data Type
LIST
• List is an ordered set of elements(linear
collections of data items)
• General form :A1,A2,A3…….An(suffix).The size
of the list is n.
A1-first element
A2-second element
An-nth element
• A list of size 0 is called the empty list or empty
null list.
LIST ADT
• List is an ADT representing countable number
of values, same values can occur more than
once.
• List is an example of containers, they contain
values.
• List is a linear collection, like stack and queue
but is more flexible.
• Adding and removing elements from a list does
not have to happen at one end.
List ADT can be represented in two
ways
•Array
•Linked List
Types of LIST ADT
Ordered List–Names in Alphabetical order. Elements
themselves defines where they are stored in list.
Front Rear
18
Unordered List – Shopping List. New element is
inserted at front, rear, after a particular element already
in the list.
Inserted anywhere
5 10 15 20
Indexed List
Elements are referenced by their numeric position in
the list called index.
Every time list changes the position of the element
may change.
Example: Current First Place in a Race
1 2 3 4 5
Inserted anywhere but list changes
REPRESENTATION OF LIST ADT
• A list is a series of related data items.it can be an
ordered collection of integer items like
30,40,75,93,87 (or) it can be like a structures like,
struct student
{
int age;
char name;
int mark;
};
With each structure containing a set of related items.
Array Based Implementation
• To implement lists using array, we need an array for
storing entries: List Array [0,1,2,….m], a variable
curr to keep track of number of array elements
currently assigned.
List Array Curr
Size 0 1 2 n-1 maxsize m
No. of items in list
Current size of List, variable to record max length of array
n a1 a2 a3 ………… an unused
Issues in Array
• Fixed size: Resizing is expensive
• Requires continuous memory for allocation
– Difficult when array size is larger
• Insertions and deletions are inefficient
– Elements are usually shifted
• Wastage of memory space unless the array is
full
List Implementation using array:
1. Create
2. Display
3. Insert
4. Delete
5. Search
• Fix one end of list at index0 and elements will be
shifted when an element is added or removed.
• Insert at position 0, will push entire array to
right one spot to make room for new element.
• Deleting an element at position 0 shifts all
elements to left one spot.
Insert 2 here
0 1 2 3 4
5 10 7 12
2 5 10 7 12
Advantages of Array Based
implementation of list:
• Array is a natural way to implement list,
convenient and easy.
• Array allows fast, random access of elements.
• Array Based implementation is memory
efficient since very little memory is required.
Disadvantages:
• Size of the list must be known when the array is
created and is fixed.
• Array Implementation of lists use a static data
structure.
• Deletion and insertion of elements into the list is
slow since it involves shifting the elements.
• If insertion and deletion is towards the front,
elements must shuffle down.
• This is slow and inefficient.
Data structures - unit 1
List implementation using Array:
The following operations are performed
1.Create
2.Display or Traverse
3.Insert
4.Delete
5.Search or Find
Example program
1. Creation:
#include<stdio.h>
#define MAX 10
int a[MAX],i,n,data,pos;
void create()
{
printf("Enter the size of array");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the element %d:",i+1);
scanf("%d",&a[i]);
}
}
2. Display:
void display()
{
printf("The elements in the listn");
for(i=0;i<n;i++)
{
printf("%dn",a[i]);
}
}
3. Insert
void insert()
{
printf("Enter the position to insert");
scanf("%d",&pos);
if(pos<1 || pos>n)
{
printf("enter the correct position");
}
else
{
printf("Enter the data to insert");
scanf("%d",&data);
for(i=n-1;i>=pos-1;i--)
{
a[i+1]=a[i];
}
a[pos-1]=data;
printf("the data inserted"); n++;
}
}
4. Deletion:
void deletion()
{
printf("enter the position to delete");
scanf("%d",&pos);
if(pos<1 || pos>n)
{
printf("enter the correct position");
}
else
{
for(i=pos-1;i<=n;i++)
{
a[i]=a[i+1];
}
n--;
printf("element deleted from the position:%d", pos);
}
}
5. Search:
void search()
{
int flag=1;
printf("enter the element to search"); scanf("%d",&data);
for(i=0;i<n;i++)
{
if(a[i]==data)
{
printf("data found at index:%d",i); flag=0;
}
}
if(flag==1)
{
printf("element not found");
}
}
void main()
{
create();
display();
insert();
display();
search();
deletion();
display();
}
Linked List
•Linked list is a data structure which is in linear
form of representing the data nodes.
•A linked list is a sequence of data structures,
which are connected together via links.
•Each link contains a connection to another link. It
is the second most-used data structure after array.
2parts: Data Section - Contains data element to be stored
Address/Pointer Section - Contains the address of
next node
Declaration of a node:
struct node
{
int data;
struct node *next;
};
Types of Linked List
1. Singly Linked List − Item navigation is forward
only.
2. Doubly Linked List − Items can be navigated
forward and backward.
3. Circular Linked List − Last item contains link
of the first element as next and the first element
has a link to the last element as previous.
Singly Linked List
• Singly Linked List is unidirectional and it can be
traversed in only one direction from head to last
node.
• A single node contains data and a pointer to the
next node.
• Visualized as a chain of nodes, every node points
to next node.
Insertion Operation
• Memory is to be allocated for the new node
before reading the data.
• The new node will contain empty data field
and empty next field.
• The data field of the new node is then stored
with the information read from the user.
• The next field of the new node is assigned to
NULL.
3ways to insert a node
The new node can then be inserted at three
different places namely:
• Inserting a node at the beginning.
• Inserting a node at the end.
• Inserting a node at intermediate position.
Insertion Example
• Adding a new node in linked list is a more than one
step activity. First, create a node using the same
structure and find the location where it has to be
inserted.
• Imagine that we are inserting a
node B (NewNode), between A (LeftNode)
and C (RightNode). Then point B.next to C
• NewNode.next −> RightNode
• Now, the next node at the left should point to
the new node.
• LeftNode.next −> NewNode; This will put the
new node in the middle of the two.
• Similar procedure takes place when a node is
being inserted at the beginning of the list. While
inserting it at the end, the second last node of the
list should point to the new node and the new
node will point to NULL.
Data structures - unit 1
Data structures - unit 1
Inserting a node at Middle
Deletion Operation
• Deletion of a node is an another primitive
operation that can be done in a singly linked list.
• Memory is released for the node to be deleted.
• A node can be deleted from the list from three
different places namely.
• Deleting a node at the beginning.
• Deleting a node at the end.
• Deleting a node at intermediate position.
Deletion Example
• Deletion is also a more than one step process.
First, locate the target node to be removed, by
using searching algorithms.
• The left (previous) node of the target node
now should point to the next node of the
target node
• LeftNode.next −> TargetNode.next;
• This will remove the link that was pointing to the target
node. Now, using the following code, we will remove
what the target node is pointing at.
• TargetNode.next −> NULL; we can simply deallocate
memory and wipe off the target node completely.
Data structures - unit 1
Data structures - unit 1
Deleting a node at intermediate position:
Functions used in LL Programs:
• malloc() is a system function which allocates a
block of memory in the "heap" and returns a
pointer to the new block.
• The prototype of malloc() and other heap
functions are in stdlib.h. malloc() returns NULL if
it cannot fulfill the request.
void *malloc (number_of_bytes)
• Since a void * is returned the C standard states
that this pointer can be converted to any type.
char *cp;
cp = (char *) malloc (100);
• Attempts to get 100 bytes and assigns the starting
address to cp. We can also use the sizeof()
function to specify the number of bytes.
int *ip;
ip = (int *) malloc (100*sizeof(int));
• free() is the opposite of malloc(), which de-
allocates memory. The argument to free() is a
pointer to a block of memory in the heap — a
pointer which was obtained by a malloc()
function.
• The syntax is:
free (ptr);
• The advantage of free() is memory management
when we no longer need a block.
Singly Linked List
Operations performed:
Create
Display
Insert at beginning
Insert at last
Insert at middle
Delete at beginning
Delete at last
Delete at middle
Search
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
typedef struct Node node;
node *head=NULL;
node* getnode()
{
node *newnode;
newnode=(node*)malloc(sizeof(node));
printf("enter the data");
scanf("%d",&newnode->data);
newnode->next=NULL;
return newnode;
}
Create
void createlist()
{
int n,i;
node *newnode,*temp;
printf("enter the number of nodes");
scanf("%d",&n);
for(i=0;i<n;i++)
{
newnode=getnode();
if(head==NULL)
{
head=newnode;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}
}
Display
void display()
{
node *temp;
temp=head;
if(head==NULL)
{
printf("list is empty");
}
else
{
while(temp!=NULL)
{
printf("%d-->",temp->data);
temp=temp->next;
}
printf("n");
}
}
Insert node at beginning
void insertatfirst()
{
node *newnode;
printf("insert at firstn");
newnode=getnode();
if(head==NULL)
{
head=newnode;
}
else
{
newnode->next=head;
head=newnode;
}
}
Insert node at the end
void insertatlast()
{
node *newnode,*temp;
printf("insert at last");
newnode=getnode();
if(head==NULL)
{
head=newnode;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}
Insert node at intermediate position
void insertatmiddle()
{
int pos,i;
node *newnode,*temp,*prev;
printf("enter the position");
scanf("%d",&pos);
if(pos>1 && pos<=numberofnodes())
{
newnode=getnode();
temp=prev=head;
for(i=1;i<pos;i++)
{
prev=temp;
temp=temp->next;
}
newnode->next=prev->next;
prev->next=newnode;
}
else
{
printf("enter the correct poition");
}
}
int numberofnodes()
{
int count=0;
node *temp;
temp=head;
while(temp!=NULL)
{
count++;
temp=temp->next;
}
return count;
}
Delete a node at Beginning
void deleteatfirst()
{
node *temp;
if(head==NULL)
{
printf("list is empty");
}
else
{
temp=head;
head=head->next;
free(temp);
printf("node deleted");
}
}
Delete a node at End
void deleteatlast()
{
node *temp,*prev;
if(head==NULL)
{
printf("list is empty");
}
else
{
temp=prev=head;
while(temp->next!=NULL)
{
prev=temp;
temp=temp->next;
}
prev->next=NULL;
free(temp);
printf("node deleted");
}
}
Delete a node at middle
void deleteatmiddle()
{
int pos,i;
node *temp,*prev;
printf("enter the position to delete");
scanf("%d",&pos);
if(pos>1 && pos<numberofnodes())
{
temp=prev=head;
for(i=1;i<pos;i++)
{
prev=temp;
temp=temp->next;
}
prev->next=temp->next;
free(temp);
printf("node deleted");
}
else
{
printf("enter the correct position");
}
}
Search
void search()
{
int x,flag=1;
node *temp;
printf("enter the data to search");
scanf("%d",&x);
temp=head;
while(temp!=NULL)
{
if(temp->data==x)
{
printf("element found");
flag=0;
}
temp=temp->next;
}
if(flag==1)
{
printf("element not found");
void main()
{
int x;
createlist();
display();
insertatfirst();
display();
insertatlast();
display();
x=numberofnodes();
printf("no of nodes=%d",x);
insertatmiddle();
display();
deleteatfirst();
display();
deleteatfirst();
display();
deleteatmiddle();
display();
search();
}
Doubly Linked List
•Doubly Linked List is a variation of Linked list in
which navigation is possible in both ways, either
forward and backward easily.
• It has an extra pointer pointing to the previous node,
adding an extra link to the list.
•It simplifies deletion as it no longer have to refer to the
key using pointer to the previous cell.
Terms used under DLL
• Link − Each link of a linked list can store a data called an
element.
• Next − Each link of a linked list contains a link to the next link
called Next.
• Prev − Each link of a linked list contains a link to the previous
link called Prev.
• Linked List − A Linked List contains the connection link to
the first link called First and to the last link called Last.
• Doubly Linked List contains a link element called first
and last.
• Each link carries a data field(s) and two link fields called
next and prev.
• Each link is linked with its next link using its next link.
• Each link is linked with its previous link using its
previous link.
• The last link carries a link as null to mark the end of the
list.
Basic Operations
Following are the basic operations supported by a list.
• Insertion − Adds an element at the beginning of the list.
• Deletion − Deletes an element at the beginning of the list.
• Insert Last − Adds an element at the end of the list.
• Delete Last − Deletes an element from the end of the list.
• Insert After − Adds an element after an item of the list.
• Delete − Deletes an element from the list using the key.
• Display forward − Displays the complete list in a forward
manner.
• Display backward − Displays the complete list in a
backward manner.
Insertion
void insertFirst(int key, int data)
{
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(isEmpty())
{
last = link;
}
else
{
head->prev = link;
}
link->next = head;
head = link;
}
Delete
struct node* deleteFirst()
{
struct node *tempLink = head;
if(head->next == NULL)
{
last = NULL;
}
else
{
head->next->prev = NULL;
}
head = head->next;
return tempLink;
}
Insert link at the last location
void insertLast(int key, int data)
{
struct node *link = (struct node*)
malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(isEmpty())
{
last = link;
}
else
{
last->next = link;
link->prev = last;
}
last = link;
}
Circular Linked List
• Circular Linked List is a variation of Linked
list in which the first element points to the last
element and the last element points to the first
element.
• Both Singly Linked List and Doubly Linked
List can be made into a circular linked list.
Singly Linked List as Circular
• In singly linked list, the next pointer of the last
node points to the first node.
Doubly Linked List as Circular
• In doubly linked list, the next pointer of the last node
points to the first node and the previous pointer of the
first node points to the last node making the circular
in both directions.
• The last link's next points to the first link of the list in
both cases of singly as well as doubly linked list.
• The first link's previous points to the last of the list in
case of doubly linked list.
Basic Operations
• insert − Inserts an element at the start of the
list.
• delete − Deletes an element from the start of
the list.
• display − Displays the list.
APPLICATIONS OF LINKED LISTS :
•Linked lists can be used to represent
polynomials and the different operations that
can be performed on them.
•In this section, we will see how polynomials
are represented in the memory using linked
lists.
Polynomial Representation:
• Let us see how a polynomial is represented in the memory using
a linked list. Consider a polynomial 6x3 + 9x2 + 7x + 1. Every
individual term in a polynomial consists of two parts, a
coefficient and a power. Here, 6, 9, 7, and 1 are the coefficients
of the terms that have 3, 2, 1, and 0 as their powers respectively.
Every term of a polynomial can be represented as a node of the
linked list.
Polynomial Addition:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct Node
{
int coef; int exp;
struct Node *next;
};
typedef struct Node node;
node *poly1=NULL,*poly2=NULL,*poly3=NULL;
node* getnode()
{
node *newnode;
newnode=(node*)malloc(sizeof(node));
printf("enter the coeff and expn");
printf("enter the exp in descending order");
scanf("%d%d",&newnode->coef,&newnode->exp);
newnode->next=NULL;
return newnode;
}
node* createpoly()
{
node *poly=NULL,*newnode,*temp;
char ch;
do
{
newnode=getnode();
if(poly==NULL)
{
poly=newnode;
}
else
{
temp=poly;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
printf("Do you want to continue:n press y for yes and n for NO");
ch=getche();
if(ch=='n')
break;
}while(1);
return poly;
}
void display(node *poly)
{
node *temp;
temp=poly;
printf("n");
while(temp->next!=NULL)
{
printf("%dx^%d+",temp->coef,temp->exp);
temp=temp->next;
}
printf("%dx^%d",temp->coef,temp->exp);
}
node* createnode()
{
node *newnode;
newnode=(node*)malloc(sizeof(node));
newnode->coef=newnode->exp=0;
newnode->next=NULL;
return newnode;
}
void insert(node *newnode)
{
node *temp;
if(poly3==NULL)
{
poly3=newnode;
}
else
{
temp=poly3;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}
void addpoly(node *p1,node *p2)
{
node *temp,*ptr1,*ptr2;
ptr1=p1;
ptr2=p2;
while(ptr1!=NULL && ptr2!=NULL)
{
temp=createnode();
if(ptr1->exp==ptr2->exp)
{
temp->coef=ptr1->coef+ptr2->coef;
temp->exp=ptr1->exp;
insert(temp);
}
else
{
ptr1=ptr1->next; ptr2=ptr2->next;
if(ptr1->exp>ptr2->exp)
{
temp->coef=ptr1->coef;
temp->exp=ptr1->exp;
}
else
{
insert(temp); ptr1=ptr1->next;
temp->coef=ptr2->coef; temp->exp=ptr2->exp; insert(temp);
ptr2=ptr2->next;
}
}
}
while(ptr1!=NULL)
{
temp=createnode();
temp->coef=ptr1->coef;
temp->exp=ptr1->exp;
insert(temp);
ptr1=ptr1->next;
}
while(ptr2!=NULL)
{
temp=createnode();
temp->coef=ptr2->coef;
temp->exp=ptr2->exp;
insert(temp);
ptr2=ptr2->next;
}
}
void main()
{
poly1=createpoly();
poly2=createpoly();
display(poly1);
display(poly2);
addpoly(poly1,poly2);
display(poly3);
}
THANK YOU

More Related Content

PPTX
DATA VISUALIZATION.pptx
PPT
2D transformation (Computer Graphics)
PPTX
CS 3491 Artificial Intelligence and Machine Learning Unit I Problem Solving
PPT
DOC
Tqm notes five units
PDF
Techiques for Writing Embedded Code_Internet of Things
PPTX
Presentation on-exception-handling
PPTX
sorting and its types
DATA VISUALIZATION.pptx
2D transformation (Computer Graphics)
CS 3491 Artificial Intelligence and Machine Learning Unit I Problem Solving
Tqm notes five units
Techiques for Writing Embedded Code_Internet of Things
Presentation on-exception-handling
sorting and its types

What's hot (20)

PPTX
queue & its applications
PPTX
stack & queue
PPTX
Binary Tree Traversal
PPTX
Array Introduction One-dimensional array Multidimensional array
PDF
Python tuple
PPT
PPTX
Data structure tries
PPTX
Graph traversals in Data Structures
PPTX
Queue in Data Structure
PDF
Binary Search - Design & Analysis of Algorithms
PPTX
Linked List
PPTX
Analysis of algorithm
PPTX
Ordered lists
PPT
Data Structure and Algorithms
PPTX
PPT
Data structures & algorithms lecture 3
PPTX
heap Sort Algorithm
PPTX
Arrays In C++
PPTX
linked list
PPTX
Asymptotic Notation
queue & its applications
stack & queue
Binary Tree Traversal
Array Introduction One-dimensional array Multidimensional array
Python tuple
Data structure tries
Graph traversals in Data Structures
Queue in Data Structure
Binary Search - Design & Analysis of Algorithms
Linked List
Analysis of algorithm
Ordered lists
Data Structure and Algorithms
Data structures & algorithms lecture 3
heap Sort Algorithm
Arrays In C++
linked list
Asymptotic Notation
Ad

Similar to Data structures - unit 1 (20)

PPTX
unit 1_Linked list.pptx
PPTX
DS Module 1.pptx
PPTX
DS Module 1.pptx
PPTX
Data Structures - Array presentation .pptx
PPT
Unit 1.ppt
PPT
lecture 02.2.ppt
DOCX
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
PDF
INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS
PDF
INTRODUCTION TO DATA STRUCTURES AND ALGORITHM
PPTX
unit 1.pptx
PPTX
Chapter 1 _edited.pptx.software engineering
PDF
UNITIII LDS.pdf
PDF
M v bramhananda reddy dsa complete notes
PDF
Unit-I PPT hususi sisooshsgv. Eijeieieooekejj
PPTX
Lecture 1.pptxffffffffffffffcfffffffffff
PPTX
DSA - Copy.pptx
PPTX
Data Structure and Algorithms by Sabeen Memon03.pptx
PPTX
project on data structures and algorithm
PPTX
DATA STRUCTURE INTRODUCITON FULL NOTES.pptx
PPTX
Introduction to Data Structures and their importance
unit 1_Linked list.pptx
DS Module 1.pptx
DS Module 1.pptx
Data Structures - Array presentation .pptx
Unit 1.ppt
lecture 02.2.ppt
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS
INTRODUCTION TO DATA STRUCTURES AND ALGORITHM
unit 1.pptx
Chapter 1 _edited.pptx.software engineering
UNITIII LDS.pdf
M v bramhananda reddy dsa complete notes
Unit-I PPT hususi sisooshsgv. Eijeieieooekejj
Lecture 1.pptxffffffffffffffcfffffffffff
DSA - Copy.pptx
Data Structure and Algorithms by Sabeen Memon03.pptx
project on data structures and algorithm
DATA STRUCTURE INTRODUCITON FULL NOTES.pptx
Introduction to Data Structures and their importance
Ad

Recently uploaded (20)

PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
composite construction of structures.pdf
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Construction Project Organization Group 2.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PPT
Project quality management in manufacturing
PPTX
OOP with Java - Java Introduction (Basics)
Lesson 3_Tessellation.pptx finite Mathematics
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
Embodied AI: Ushering in the Next Era of Intelligent Systems
bas. eng. economics group 4 presentation 1.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
composite construction of structures.pdf
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
Foundation to blockchain - A guide to Blockchain Tech
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Arduino robotics embedded978-1-4302-3184-4.pdf
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Construction Project Organization Group 2.pptx
Lecture Notes Electrical Wiring System Components
Structs to JSON How Go Powers REST APIs.pdf
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
Project quality management in manufacturing
OOP with Java - Java Introduction (Basics)

Data structures - unit 1

  • 1. VELAMMAL ENGINEERING COLLEGE An Autonomous Institution, Affiliated to Anna University Chennai, & Approved by AICTE Delhi Online Classes on Data Structures ( 19CS208T ) CLASS: II YEAR AUTO PRESENTED SARANYA P AP / DEPT OF CSE
  • 2. DATA+STRUCTURE Data Values or set of values of different data types such as int, float, etc., Way of organizing and storing information so that it is easy to use Structure Data + Structure Way of organizing and storing data on a computer so that it can be used easily and effectively
  • 3. DATA STRUCTURE •Data Structure is defined as the group of data elements provides an efficient way of storing or organising data in a computer. •It is used in almost every program or software system. Examples: Arrays, Linked lists, Stack and Queue.
  • 4. COURSE OBJECTIVES • To implement the ADTs stack, queue, list • To understand the performance of the implementations of non linear data structures. • To understand the performance of various sorting and searching algorithms.
  • 5. UNIT I-INTRODUCTION TO DATA STRUCTURE Abstract Data Types (ADT) – List ADT – array-based implementation – issues in array based implementation – Singly linked list: Creation, insertion at first, insertion at middle, insertion at end, deletion at first, deletion at middle and deletion at end and display – doubly-linked lists: Creation, insertion, deletion and display - Application of lists – Polynomial manipulation: creation, Addition and subtraction.
  • 6. UNIT II-LINEAR DATA STRUCTURES Stack ADT – Operations: push and pop – Applications –Arithmetic Expressions – convert of infix expression to postfix Expressions-Queue ADT – operations: Enqueue and Dequeue – issues in queue. Application of queue: circular Queue - Creation, insertion, deletion and display.
  • 9. UNIT III -TREE STRUCTURES Tree ADT – Binary Tree Representation – Binary Search Tree ADT – Insertion –Deletion- FindMin – FindMax- Expression trees – Tree traversals: in-order traversals, pre-order traversals, post-order traversals . Balanced Tree: Adelson Velski Landis tree(AVL): Single rotation with right, Single rotation with left, Double rotation with left and double rotation with right.
  • 10. UNIT IV - GRAPHS Definitions – Types of graph – Directed graph, UnDirected graph, Weighted graph, Unweighted Graph, Cyclic and Acyclic graph, Complete graph - Representation –Adjacency Matrix representation – Adjacency List Representation – Topological sort - shortest-path algorithms- Dijkstra’s Algorithm – Minimum spanning tree – Prim's algorithm - Kruskal's algorithms.
  • 11. UNIT V - ALGORITHMS DESIGN AND ANALYSIS Greedy algorithms – Divide and conquer – Dynamic programming – back tracking –branch and bound – Randomized algorithms – algorithm analysis – asymptotic notations – recurrences – NP complete problems.
  • 13. TREE
  • 14. COURSE OUTCOMES • Apply abstract data types for various linear an non linear data structures. • Illustrate the role of key pre-processing algorithms in various data structures. • Use knowledge to balance a Binary Search trees. • Distinguishes various graph algorithms for finding minimum path. • Choose various memory models to represent static and dynamic Hashed structures. • Understand the concepts of various searching and sorting algorithms.
  • 15. TEXT BOOKS 1. M. A. Weiss, “Data Structures and Algorithm Analysis in C”, Second Edition , Pearson Education, 2016. 2. Reema Thareja, “Data Structures Using C”, Second Edition , Oxford University Press, 2011
  • 16. REFERENCE BOOKS 1. R. F. Gilberg, B. A. Forouzan, “Data Structures”, Second Edition, Thomson India Edition, 2005. 2. Ellis Horowitz, Sartaj Sahni, Susan Anderson- Freed, “Fundamentals of Data Structures in C”, Second Edition, University Press, 2008
  • 17. ONLINE LINKS TO REFER • https://www.geeksforgeeks.org/data-structures/ • https://www.tutorialspoint.com/data_structures _algorithms • https://www.w3schools.com • www.tutorialspoint.com
  • 18. UNIT 1 Introduction to Data Structure
  • 19. What is Data structure • Group of Data Elements provides an efficient way of Storing and organising data in computer. Example: Array, Linked List, Stack & Queue.
  • 20. Classification of Data structure Data structure are generally classified into two classes: 1. Primitive Data Structure 2. Non- Primitive Data Structure Primitive DS: The Fundamental Data types which are supported by the programming language. Some of the primitive data types are integer, real, character, Boolean.
  • 21. Non-Primitive DS: Data structures which are created using primitive data structures. Some of the Non-Primitive data types are Trees, Graphs, Stacks and Lists. They are further classified into Linear and Non-Linear Data structure.
  • 22. Data Structure Linear Data Structure Non-Linear Data Structure Static Dynamic Tree Graph Stack Queue Linked List Array
  • 23. Definitions: 1.Primitive / Non-Primitive DS 2.Linear / Non-Linear DS 3.Homogeneous / Non-homogeneous DS 4. Static /Dynamic DS 5.Contiguous / Dynamic DS
  • 24. Data Structure Primitive int char float boolean Non - Primitive Linear Array Linked List Stack Queue Non - Linear Tree Graph
  • 25. Linear Structures Non-Linear Structures Elements of DS are stored in a linear or sequential order. Examples: Arrays, linked lists, stacks, and queues. Represented in 2 ways. 1. By means of sequential memory locations. 2. By means of links. Single level of elements. •Elements of a data structure are not stored in a sequential order. Examples: Trees and graphs. •The relationship of adjacency is not maintained between elements. •Can have Multiple Level of Elements. •Traversal is not easy as linear Structures.
  • 26. Operations of Data Structure The different operations that can be performed under Data structure are, 1. Traversing 2. Searching 3. Inserting 4. Deleting 5. Sorting 6. Merging Lists
  • 27. Abstract Data Types • Mathematical model for data types are known as Abstract Data Types or ADT. • ADT is a special kind of data type defines by set of value and set of operations. • ADT is made of with primitive data types, but operation logics are hidden. • ADT lets user to know what a data type can do but not how it will be implemented. Example: Stack, Queue, List.
  • 28. ABSTRACT DATA TYPE: 1. ADT is a mathematical abstractions which means representing the implementation of the set of operations by using the modular design. 2. Example: List, sets and graphs along with their operations. 3. Implementing ADT is that operations are written once in a program and can be called by any part of the program.
  • 30. LIST • List is an ordered set of elements(linear collections of data items) • General form :A1,A2,A3…….An(suffix).The size of the list is n. A1-first element A2-second element An-nth element • A list of size 0 is called the empty list or empty null list.
  • 31. LIST ADT • List is an ADT representing countable number of values, same values can occur more than once. • List is an example of containers, they contain values. • List is a linear collection, like stack and queue but is more flexible. • Adding and removing elements from a list does not have to happen at one end.
  • 32. List ADT can be represented in two ways •Array •Linked List
  • 33. Types of LIST ADT Ordered List–Names in Alphabetical order. Elements themselves defines where they are stored in list. Front Rear 18 Unordered List – Shopping List. New element is inserted at front, rear, after a particular element already in the list. Inserted anywhere 5 10 15 20
  • 34. Indexed List Elements are referenced by their numeric position in the list called index. Every time list changes the position of the element may change. Example: Current First Place in a Race 1 2 3 4 5 Inserted anywhere but list changes
  • 35. REPRESENTATION OF LIST ADT • A list is a series of related data items.it can be an ordered collection of integer items like 30,40,75,93,87 (or) it can be like a structures like, struct student { int age; char name; int mark; }; With each structure containing a set of related items.
  • 36. Array Based Implementation • To implement lists using array, we need an array for storing entries: List Array [0,1,2,….m], a variable curr to keep track of number of array elements currently assigned. List Array Curr Size 0 1 2 n-1 maxsize m No. of items in list Current size of List, variable to record max length of array n a1 a2 a3 ………… an unused
  • 37. Issues in Array • Fixed size: Resizing is expensive • Requires continuous memory for allocation – Difficult when array size is larger • Insertions and deletions are inefficient – Elements are usually shifted • Wastage of memory space unless the array is full
  • 38. List Implementation using array: 1. Create 2. Display 3. Insert 4. Delete 5. Search • Fix one end of list at index0 and elements will be shifted when an element is added or removed. • Insert at position 0, will push entire array to right one spot to make room for new element.
  • 39. • Deleting an element at position 0 shifts all elements to left one spot. Insert 2 here 0 1 2 3 4 5 10 7 12 2 5 10 7 12
  • 40. Advantages of Array Based implementation of list: • Array is a natural way to implement list, convenient and easy. • Array allows fast, random access of elements. • Array Based implementation is memory efficient since very little memory is required.
  • 41. Disadvantages: • Size of the list must be known when the array is created and is fixed. • Array Implementation of lists use a static data structure. • Deletion and insertion of elements into the list is slow since it involves shifting the elements. • If insertion and deletion is towards the front, elements must shuffle down. • This is slow and inefficient.
  • 43. List implementation using Array: The following operations are performed 1.Create 2.Display or Traverse 3.Insert 4.Delete 5.Search or Find
  • 44. Example program 1. Creation: #include<stdio.h> #define MAX 10 int a[MAX],i,n,data,pos; void create() { printf("Enter the size of array"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter the element %d:",i+1); scanf("%d",&a[i]); } }
  • 45. 2. Display: void display() { printf("The elements in the listn"); for(i=0;i<n;i++) { printf("%dn",a[i]); } }
  • 46. 3. Insert void insert() { printf("Enter the position to insert"); scanf("%d",&pos); if(pos<1 || pos>n) { printf("enter the correct position"); } else { printf("Enter the data to insert"); scanf("%d",&data);
  • 48. 4. Deletion: void deletion() { printf("enter the position to delete"); scanf("%d",&pos); if(pos<1 || pos>n) { printf("enter the correct position"); } else { for(i=pos-1;i<=n;i++) { a[i]=a[i+1]; } n--; printf("element deleted from the position:%d", pos); } }
  • 49. 5. Search: void search() { int flag=1; printf("enter the element to search"); scanf("%d",&data); for(i=0;i<n;i++) { if(a[i]==data) { printf("data found at index:%d",i); flag=0; } } if(flag==1) { printf("element not found"); } }
  • 51. Linked List •Linked list is a data structure which is in linear form of representing the data nodes. •A linked list is a sequence of data structures, which are connected together via links. •Each link contains a connection to another link. It is the second most-used data structure after array. 2parts: Data Section - Contains data element to be stored Address/Pointer Section - Contains the address of next node
  • 52. Declaration of a node: struct node { int data; struct node *next; };
  • 53. Types of Linked List 1. Singly Linked List − Item navigation is forward only. 2. Doubly Linked List − Items can be navigated forward and backward. 3. Circular Linked List − Last item contains link of the first element as next and the first element has a link to the last element as previous.
  • 54. Singly Linked List • Singly Linked List is unidirectional and it can be traversed in only one direction from head to last node. • A single node contains data and a pointer to the next node. • Visualized as a chain of nodes, every node points to next node.
  • 55. Insertion Operation • Memory is to be allocated for the new node before reading the data. • The new node will contain empty data field and empty next field. • The data field of the new node is then stored with the information read from the user. • The next field of the new node is assigned to NULL.
  • 56. 3ways to insert a node The new node can then be inserted at three different places namely: • Inserting a node at the beginning. • Inserting a node at the end. • Inserting a node at intermediate position.
  • 57. Insertion Example • Adding a new node in linked list is a more than one step activity. First, create a node using the same structure and find the location where it has to be inserted. • Imagine that we are inserting a node B (NewNode), between A (LeftNode) and C (RightNode). Then point B.next to C
  • 58. • NewNode.next −> RightNode • Now, the next node at the left should point to the new node.
  • 59. • LeftNode.next −> NewNode; This will put the new node in the middle of the two. • Similar procedure takes place when a node is being inserted at the beginning of the list. While inserting it at the end, the second last node of the list should point to the new node and the new node will point to NULL.
  • 62. Inserting a node at Middle
  • 63. Deletion Operation • Deletion of a node is an another primitive operation that can be done in a singly linked list. • Memory is released for the node to be deleted. • A node can be deleted from the list from three different places namely. • Deleting a node at the beginning. • Deleting a node at the end. • Deleting a node at intermediate position.
  • 64. Deletion Example • Deletion is also a more than one step process. First, locate the target node to be removed, by using searching algorithms.
  • 65. • The left (previous) node of the target node now should point to the next node of the target node • LeftNode.next −> TargetNode.next;
  • 66. • This will remove the link that was pointing to the target node. Now, using the following code, we will remove what the target node is pointing at. • TargetNode.next −> NULL; we can simply deallocate memory and wipe off the target node completely.
  • 69. Deleting a node at intermediate position:
  • 70. Functions used in LL Programs: • malloc() is a system function which allocates a block of memory in the "heap" and returns a pointer to the new block. • The prototype of malloc() and other heap functions are in stdlib.h. malloc() returns NULL if it cannot fulfill the request. void *malloc (number_of_bytes)
  • 71. • Since a void * is returned the C standard states that this pointer can be converted to any type. char *cp; cp = (char *) malloc (100); • Attempts to get 100 bytes and assigns the starting address to cp. We can also use the sizeof() function to specify the number of bytes. int *ip; ip = (int *) malloc (100*sizeof(int));
  • 72. • free() is the opposite of malloc(), which de- allocates memory. The argument to free() is a pointer to a block of memory in the heap — a pointer which was obtained by a malloc() function. • The syntax is: free (ptr); • The advantage of free() is memory management when we no longer need a block.
  • 73. Singly Linked List Operations performed: Create Display Insert at beginning Insert at last Insert at middle Delete at beginning Delete at last Delete at middle Search
  • 74. #include<stdio.h> #include<stdlib.h> struct Node { int data; struct Node *next; }; typedef struct Node node; node *head=NULL; node* getnode() { node *newnode; newnode=(node*)malloc(sizeof(node)); printf("enter the data"); scanf("%d",&newnode->data); newnode->next=NULL; return newnode; }
  • 75. Create void createlist() { int n,i; node *newnode,*temp; printf("enter the number of nodes"); scanf("%d",&n); for(i=0;i<n;i++) { newnode=getnode(); if(head==NULL) { head=newnode; }
  • 77. Display void display() { node *temp; temp=head; if(head==NULL) { printf("list is empty"); } else { while(temp!=NULL) { printf("%d-->",temp->data); temp=temp->next; } printf("n"); } }
  • 78. Insert node at beginning void insertatfirst() { node *newnode; printf("insert at firstn"); newnode=getnode(); if(head==NULL) { head=newnode; } else { newnode->next=head; head=newnode; } }
  • 79. Insert node at the end void insertatlast() { node *newnode,*temp; printf("insert at last"); newnode=getnode(); if(head==NULL) { head=newnode; } else { temp=head; while(temp->next!=NULL) { temp=temp->next; } temp->next=newnode; } }
  • 80. Insert node at intermediate position void insertatmiddle() { int pos,i; node *newnode,*temp,*prev; printf("enter the position"); scanf("%d",&pos); if(pos>1 && pos<=numberofnodes()) { newnode=getnode(); temp=prev=head; for(i=1;i<pos;i++) { prev=temp; temp=temp->next;
  • 81. } newnode->next=prev->next; prev->next=newnode; } else { printf("enter the correct poition"); } } int numberofnodes() { int count=0; node *temp; temp=head; while(temp!=NULL) { count++; temp=temp->next; } return count; }
  • 82. Delete a node at Beginning void deleteatfirst() { node *temp; if(head==NULL) { printf("list is empty"); } else { temp=head; head=head->next; free(temp); printf("node deleted"); } }
  • 83. Delete a node at End void deleteatlast() { node *temp,*prev; if(head==NULL) { printf("list is empty"); } else { temp=prev=head; while(temp->next!=NULL) { prev=temp; temp=temp->next; }
  • 85. Delete a node at middle void deleteatmiddle() { int pos,i; node *temp,*prev; printf("enter the position to delete"); scanf("%d",&pos); if(pos>1 && pos<numberofnodes()) { temp=prev=head; for(i=1;i<pos;i++) { prev=temp; temp=temp->next; }
  • 87. Search void search() { int x,flag=1; node *temp; printf("enter the data to search"); scanf("%d",&x); temp=head; while(temp!=NULL) { if(temp->data==x) { printf("element found"); flag=0; } temp=temp->next; } if(flag==1) { printf("element not found");
  • 88. void main() { int x; createlist(); display(); insertatfirst(); display(); insertatlast(); display(); x=numberofnodes(); printf("no of nodes=%d",x); insertatmiddle(); display(); deleteatfirst(); display(); deleteatfirst(); display(); deleteatmiddle(); display(); search(); }
  • 89. Doubly Linked List •Doubly Linked List is a variation of Linked list in which navigation is possible in both ways, either forward and backward easily. • It has an extra pointer pointing to the previous node, adding an extra link to the list. •It simplifies deletion as it no longer have to refer to the key using pointer to the previous cell.
  • 90. Terms used under DLL • Link − Each link of a linked list can store a data called an element. • Next − Each link of a linked list contains a link to the next link called Next. • Prev − Each link of a linked list contains a link to the previous link called Prev. • Linked List − A Linked List contains the connection link to the first link called First and to the last link called Last.
  • 91. • Doubly Linked List contains a link element called first and last. • Each link carries a data field(s) and two link fields called next and prev. • Each link is linked with its next link using its next link. • Each link is linked with its previous link using its previous link. • The last link carries a link as null to mark the end of the list.
  • 92. Basic Operations Following are the basic operations supported by a list. • Insertion − Adds an element at the beginning of the list. • Deletion − Deletes an element at the beginning of the list. • Insert Last − Adds an element at the end of the list. • Delete Last − Deletes an element from the end of the list. • Insert After − Adds an element after an item of the list. • Delete − Deletes an element from the list using the key. • Display forward − Displays the complete list in a forward manner. • Display backward − Displays the complete list in a backward manner.
  • 93. Insertion void insertFirst(int key, int data) { struct node *link = (struct node*) malloc(sizeof(struct node)); link->key = key; link->data = data; if(isEmpty()) { last = link; } else { head->prev = link; } link->next = head; head = link; }
  • 94. Delete struct node* deleteFirst() { struct node *tempLink = head; if(head->next == NULL) { last = NULL; } else { head->next->prev = NULL; } head = head->next; return tempLink; }
  • 95. Insert link at the last location void insertLast(int key, int data) { struct node *link = (struct node*) malloc(sizeof(struct node)); link->key = key; link->data = data; if(isEmpty()) { last = link; } else { last->next = link; link->prev = last; } last = link; }
  • 96. Circular Linked List • Circular Linked List is a variation of Linked list in which the first element points to the last element and the last element points to the first element. • Both Singly Linked List and Doubly Linked List can be made into a circular linked list.
  • 97. Singly Linked List as Circular • In singly linked list, the next pointer of the last node points to the first node.
  • 98. Doubly Linked List as Circular • In doubly linked list, the next pointer of the last node points to the first node and the previous pointer of the first node points to the last node making the circular in both directions. • The last link's next points to the first link of the list in both cases of singly as well as doubly linked list. • The first link's previous points to the last of the list in case of doubly linked list.
  • 99. Basic Operations • insert − Inserts an element at the start of the list. • delete − Deletes an element from the start of the list. • display − Displays the list.
  • 100. APPLICATIONS OF LINKED LISTS : •Linked lists can be used to represent polynomials and the different operations that can be performed on them. •In this section, we will see how polynomials are represented in the memory using linked lists.
  • 101. Polynomial Representation: • Let us see how a polynomial is represented in the memory using a linked list. Consider a polynomial 6x3 + 9x2 + 7x + 1. Every individual term in a polynomial consists of two parts, a coefficient and a power. Here, 6, 9, 7, and 1 are the coefficients of the terms that have 3, 2, 1, and 0 as their powers respectively. Every term of a polynomial can be represented as a node of the linked list.
  • 102. Polynomial Addition: #include<stdio.h> #include<stdlib.h> #include<conio.h> struct Node { int coef; int exp; struct Node *next; }; typedef struct Node node; node *poly1=NULL,*poly2=NULL,*poly3=NULL; node* getnode() { node *newnode; newnode=(node*)malloc(sizeof(node));
  • 103. printf("enter the coeff and expn"); printf("enter the exp in descending order"); scanf("%d%d",&newnode->coef,&newnode->exp); newnode->next=NULL; return newnode; } node* createpoly() { node *poly=NULL,*newnode,*temp; char ch; do { newnode=getnode(); if(poly==NULL) { poly=newnode; }
  • 104. else { temp=poly; while(temp->next!=NULL) { temp=temp->next; } temp->next=newnode; } printf("Do you want to continue:n press y for yes and n for NO"); ch=getche(); if(ch=='n') break; }while(1); return poly; }
  • 105. void display(node *poly) { node *temp; temp=poly; printf("n"); while(temp->next!=NULL) { printf("%dx^%d+",temp->coef,temp->exp); temp=temp->next; } printf("%dx^%d",temp->coef,temp->exp); } node* createnode() { node *newnode; newnode=(node*)malloc(sizeof(node)); newnode->coef=newnode->exp=0; newnode->next=NULL; return newnode; }
  • 106. void insert(node *newnode) { node *temp; if(poly3==NULL) { poly3=newnode; } else { temp=poly3; while(temp->next!=NULL) { temp=temp->next; } temp->next=newnode; } }
  • 107. void addpoly(node *p1,node *p2) { node *temp,*ptr1,*ptr2; ptr1=p1; ptr2=p2; while(ptr1!=NULL && ptr2!=NULL) { temp=createnode(); if(ptr1->exp==ptr2->exp) { temp->coef=ptr1->coef+ptr2->coef; temp->exp=ptr1->exp; insert(temp); } else { ptr1=ptr1->next; ptr2=ptr2->next; if(ptr1->exp>ptr2->exp) {
  • 108. temp->coef=ptr1->coef; temp->exp=ptr1->exp; } else { insert(temp); ptr1=ptr1->next; temp->coef=ptr2->coef; temp->exp=ptr2->exp; insert(temp); ptr2=ptr2->next; } } } while(ptr1!=NULL) { temp=createnode(); temp->coef=ptr1->coef; temp->exp=ptr1->exp; insert(temp); ptr1=ptr1->next; }