0% found this document useful (0 votes)
142 views

Data Structures: Aad Cse Srm-Ap 1

The document discusses different data structures including linear data structures like arrays, stacks and queues as well as non-linear data structures like trees and graphs. It describes how arrays, stacks, queues and linked lists are implemented and the basic operations that can be performed on each including insertion, deletion, traversal and sorting. Linear data structures like linked lists and arrays are compared in terms of their representation of data.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views

Data Structures: Aad Cse Srm-Ap 1

The document discusses different data structures including linear data structures like arrays, stacks and queues as well as non-linear data structures like trees and graphs. It describes how arrays, stacks, queues and linked lists are implemented and the basic operations that can be performed on each including insertion, deletion, traversal and sorting. Linear data structures like linked lists and arrays are compared in terms of their representation of data.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

DATA STRUCTURES

AAD CSE SRM-AP 1


Data Structure
A data structure is a process of
organizing the data elements in the
memory of a computer.

Data Structure

Linear Non-Linear

- - Trees
Array
- - Graphs
Stack
- Queue 2
AAD CSE SRM-AP
- Linked Lists
Linear Data Structures

AAD CSE SRM-AP 3


Array
1.Array :
An array is a collection of homogeneous type of data
elements.
2. Array Interfaces / Functionalities of Array
 Traversing
 Search
 Insertion
 Deletion
 Sorting
 Merging

AAD CSE SRM-AP 4


Inserting the data element
into an Array

AAD CSE SRM-AP 5


  if(pos>size)
  {
#include<iostream>
    cout<<"\nThis is out of range.\n";
 
  }
using namespace std;
  else
 
  {
int main()
    cout<<"\nEnter number to be inserted :: ";
{
    cin>>no;
    int i,a[50],no,pos,size;
    --pos;
    cout<<"Enter array size( Max:50 ) :: ";
 
    cin>>size;
  for(i=size;i>=pos;i--)
        cout<<"\nEnter array elements :: \n";
  {
 
    a[i+1]=a[i];
        for(i=0; i<size; i++)
  }
        {
  a[pos]=no;
            cout<<"\nEnter arr["<<i<<"] Element :: ";
 
                cin>>a[i];
  cout<<"\nNew Array is :: \n\n";
        }
 
 
  for(i=0;i<size+1;i++)
  cout<<"\nStored Data in Array :: \n\n";
  {
 
    cout<<" "<<a[i]<<" ";
  for(i=0;i<size;i++)
  }
  {
   }
  cout<<" "<<a[i]<<" ";
  cout<<"\n";
  }
   return 0;   }
   cout<<"\n\nEnter position to insert number :: ";
  cin>>pos;
AAD CSE SRM-AP 6
 
Deleting the data element
from an array

AAD CSE SRM-AP 7


#include<iostream>
if(count==0)
#include<conio>
{
void main()
cout<<"Element not found..!!";
{
}
clrscr();
else
int arr[50], size, i, del, count=0;
{
cout<<"Enter array size : ";
cout<<"Element deleted successfully..!!\n";
cin>>size;
cout<<"Now the new array is :\n";
cout<<"Enter array elements : ";
for(i=0; i<(size-1); i++)
for(i=0; i<size; i++)
{
{
cout<<arr[i]<<" ";
cin>>arr[i];
}
}
}
cout<<"Enter element to be delete : ";
getch();
cin>>del;
for(i=0; i<size; i++)
}
{
if(arr[i]==del)
{
for(int j=i; j<(size-1); j++)
{
arr[j]=arr[j+1];
}
count++;
break;
}
} AAD CSE SRM-AP 8
Stack
A stack is an Abstract Data Type (ADT),
commonly used in most programming
languages. It is named stack as it behaves
like a real-world stack, for example – a
deck of cards or a pile of plates, etc.

AAD CSE SRM-AP 9


Stack Representation

• Can be implemented by means of Array, Structure,


Pointers and Linked List.
• Stack can either be a fixed size or dynamic.
AAD CSE SRM-AP 10
Operations in Stack

AAD CSE SRM-AP 11


Stack using Array
Push Operation Pop Operation

PUSH POP

top top
top top

AAD CSE SRM-AP 12


Stack using Array
push

top ++top top

top top top--


pop

AAD CSE SRM-AP 13


Stack Algorithms
Algorithm push(S[],item, max, top)
Algorithm size() if top = max-1 then
return top +1 print “ stack is full”
else
top  top + 1
S[top]  max+1
Algorithm pop(S[],top,item)
if top=-1 then
print “ stack is underflow”
else
top top - 1
return S[top+1] AAD CSE SRM-AP 14
Queue
Queue is an abstract data structure, somewhat
similar to Stacks. Unlike stacks, a queue is
open at both its ends. One end is always used
to insert data (enqueue) and the other is used
to remove data (dequeue).

AAD CSE SRM-AP 15


Queue Representation

As in stacks, a queue can also be implemented


using Arrays, Linked-lists, Pointers and
Structures.
AAD CSE SRM-AP 16
Operations in Queue

AAD CSE SRM-AP 17


Algorithm enqueue(o)
if rear = N-1 then
print “queue full error”
else
if(front =-1) Algorithm dequeue()
front=0 if front=rear then
r (f + sz) mod N print “ Queue is empty”
Q[r]  o else
sz (sz + 1) o Q[f]
f  (f + 1) mod N
Algorithm isfull()
sz (sz - 1)
return sz=N if (front==rear)
Algorithm size() front=rear=-1
return sz return o
Algorithm isEmpty()
return (sz ==0) AAD CSE SRM-AP 18
Linked list
Linear collection of self-referential class
objects, called nodes Connected by pointer
links
Accessed via a pointer to the first node of the
list
Link pointer in the last node is set to null to
mark the list’s end
Use a linked list instead of an array when
 You have an unpredictable number of data

elements
 You want to insert and delete quickly.
AAD CSE SRM-AP 19
Singly Linked List
A singly linked list is a concrete data structure
consisting of a sequence of nodes
Each node stores next
 element

 link to the next node

Last element points to NULL.


It does not waste memory space. elem node

It can grow or shrink in size during execution of a


program.

AAD CSE SRM-AP 20


A B C D
Linked List
Keeping track of a linked list:
 Must know the pointer to the first element of the list

(called start, head, etc.).

Basic operations commonly include:


 Construction: Allocate and initialize a list object

(usually empty)
 Empty: Check if list is empty

 Insert: Add an item to the list at any point

 Delete: Remove an item from the list at any point

Traverse: Go through the list or a part of it, accessing


and processing the elements in the order they are stored
AAD CSE SRM-AP 21
Linked Representation
Data structure for a linked list:

 Each node contains a value


 and a link (pointer or reference) to some
other node
 The last node contains a null link
 The list may (or may not) have a header
AAD CSE SRM-AP 22
Singly Linked Lists and Arrays

AAD CSE SRM-AP 23


Basic Linked List Operations
List Traversal (Display function)
Searching an element in a linked list
Insert a node (At begin, At end , In between)
Delete a node (At begin, At end , In between)

AAD CSE SRM-AP 24


Create a Linked List
Empty Linked list is a single pointer having the value of NULL.
head = NULL; head
Let’s assume that the node is given by the following type
declaration:
head
struct Node{
Data next
int data;
struct Node *next;
};
To start with, we have to create a node (the first node), and
make head point to it.
head= (struct Node*)malloc(sizeof(struct Node));

AAD CSE SRM-AP 25


Singly linked lists
Node Structure

struct node
2000
{
int data; new
struct node *link;
}*new, *ptr, *header, *ptr1;
data link

10 NULL
Creating a node

new = malloc (sizeof(struct node));


2000
new -> data = 10;
new -> link = NULL;
Inserting a node at the beginning
Create a node that is to be inserted
2500
new
data link Algorithm:
1.Create a new node.
10 NULL 2.if (header = = NULL)
2500 3. header = new;
4.else
5.{
If the list is empty 6. new -> link = header;
7. header = new;
NULL 8.}
header header

If the list is not empty

1200
2500 header
new
5 1300 5 1330 4 1400 8 NULL
10 NULL 1200 1300 1330 1400
2500
header Inserting a node at the end
1500

10 1800 20 1200 30 1400 40 NULL


2000

1400
1500 1800 1200

ptr 50 NULL

1400
1800
1500
1200
2000

Algorithm:
new
1. new=malloc(sizeof(struct node));
2. ptr = header; 2000
3. while(ptr -> link!= NULL)
4. ptr = ptr -> link;
5. ptr -> link = new;
Inserting a node at the given position
header ptr

1500 1800
1500 Insert position : 3

10 1800 20 1200
2000 30 1400 40 NULL
1400
1500 1800 1200

50 NULL
1200
new Algorithm:
1. new=malloc(sizeof(struct node));
2000
2000 2. ptr = header;
3. for(i=1;i < pos-1;i++)
4. ptr = ptr -> link;
5. new -> link = ptr -> link;
6. ptr -> link = new;
Deleting a node at the beginning
header ptr

1500
1800 1500

10 1800 20 1200 30 1400 40 NULL


1400
1500 1800 1200

Algorithm:
1. if (header = = NULL)
2. print “List is Empty”;
3. else
4. {
5. ptr = header;
6. header = header -> link;
7. free(ptr);
8. }
header
Deleting a node at the end
ptr1 ptr1 ptr1
1500

10 1800 20 1200 30 NULL


1400 40 NULL

1400
1500 1800 1200

ptr

1800
1200
1400
1500

Algorithm:
1. ptr = header;
2. while(ptr -> link != NULL)
3. ptr1=ptr;
4. ptr = ptr -> link;
5. ptr1 -> link = NULL;
6. free(ptr);
Deleting a node at the given position
ptr1
header ptr
1200
1500 1800
1500 Insert position : 3

10 1800 20 1400
1200 30 1400 40 NULL
1400
1500 1800 1200

Algorithm:
1. ptr = header ;
2. for(i=1;i<pos-1;i++)
3. ptr = ptr -> link;
4. ptr1 = ptr -> link;
5. ptr -> link = ptr1-> link;
6. free(ptr1);
Traversing an elements of a list
header ptr
1500
1500

10 1800 20 1200 30 1400 40 NULL

1500 1800 1200 1400

Algorithm:
1. if(header = = NULL)
2. print “List is empty”;
3. else
4. for (ptr = header ; ptr != NULL ; ptr = ptr -> link)
5. print “ptr->data”;
#include<iostream>
#include<stdlib.h> Creating the linked List

struct Node {
int data; struct Node *create_list()
{
struct Node *next; int k, n;
}; struct Node *p, *Head;
printf("\n How many elements to enter?");
struct Node *create_list(); scanf("%d", &n);
void display(struct Node *); for (k=0; k<n; k++)
{
void main() if (k == 0) {
{ Head = (struct Node*)malloc(sizeof(struct Node));
struct Node *head; p = Head;
}
head=create_list(); else {
display(head); p->next = (struct Node*)malloc(sizeof(struct Node));
p = p->next;
} }
printf("\n Enter an %dth element",k);
scanf("%d",&p->data);
}
void display (struct Node *head)
{ p->next = NULL;
return(Head);
struct Node *p; }
for(p = head; p!= NULL; p = p->next)
{
printf ("\nNode data %d", p->data);

}
printf ("\n");
} 34
COMPLEXITY OF VARIOUS OPERATIONS
IN ARRAYS AND LINKED LIST

AAD CSE SRM-AP 35


Stack using Linked List

AAD CSE SRM-AP 36


AAD CSE SRM-AP 37
Stack Summary
Stack Operation Complexity

Array List
Fixed-Size Singly-Linked

Pop() O(1) O(1)

Push(o) O(1) O(1)

Top() O(1) O(1)

Size(), isEmpty() O(1) O(1)

2/24/22 06:13 AM
Vectors 38
Queue implementation using
Linked List
• Basic idea:
• Create a linked list to which items would be added to one
end and deleted from the other end.
• Two pointers will be maintained:
• One pointing to the beginning of the list (point from
where elements will be deleted).
• Another pointing to the end of the list (point where new Rear
elements will be inserted).

Front DELETION INSERTION


39
AAD CSE SRM-AP 39
ENQUEUE

front rear

DEQUEUE
front rear

AAD CSE SRM-AP 40


Queue Summary
Queue Operation Complexity
Array List
Fixed-Size Singly-Linked

dequeue() O(1) O(1)

enqueue(o) O(1) O(1)

front() O(1) O(1)

Size(), isEmpty() O(1) O(1)

2/24/22 06:13 AM
Vectors 41

You might also like