0% found this document useful (0 votes)
8 views51 pages

WINSEM2024-25 CBS1003 ETH VL2024250505129 2025-01-07 Reference-Material-I

The document provides an overview of data structures, including linear and non-linear types, with a focus on lists, stacks, queues, and linked lists. It details operations such as insertion, deletion, and traversal for these structures, along with their implementations in both array and linked forms. Additionally, it discusses memory management functions and the advantages and disadvantages of singly and doubly linked lists.

Uploaded by

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

WINSEM2024-25 CBS1003 ETH VL2024250505129 2025-01-07 Reference-Material-I

The document provides an overview of data structures, including linear and non-linear types, with a focus on lists, stacks, queues, and linked lists. It details operations such as insertion, deletion, and traversal for these structures, along with their implementations in both array and linked forms. Additionally, it discusses memory management functions and the advantages and disadvantages of singly and doubly linked lists.

Uploaded by

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

Data Structure

- Structured way of representing data


- Implemented using a data type
- Data type with some rules for accessing data
elements
Types of DS

- Linear - elements inside it have predecessor


successor relationship
- Non Linear
Different data structures

List, Stack, queue, trees and graphs


Linear DS

List

- Linear DS

- Used to store names and other information one after


the other

- Elements can be added anywhere in the list

- Types: Ordered and Unordered list

- Ordered – elements must be stored in order

- Unordered – elements can be stored in random


order
Operations:

Insert, delete, search and display


Insertion into an unsorted list

- Take input from the user

- Store it the first free location of the list

- increment the count of number of elements

- do the processing until the list is full


Deletion of an element from an unsorted list

- Take the element to be deleted

- Start from the first element and start searching for


the element to be deleted.

- Once the element is found, swap the element with


the element at the last location

- decrement the number of items


Sorted List

Insertion into a sorted list

- Take the element to be inserted from the user

- find the position to insert

- Shift the elements down and insert the new element

- Increment the total number of elements.


Deletion of an element from sorted list

- Take the element to be deleted from the user

- find where the element to be deleted is present

- Move the elements up overwriting the element to be


deleted.

- Decrement the total number of elements.


Stack
- Follows Last In First Out principle (LIFO)
- element added to the top of the stack
- Basic operations

- Push – to add an element


- Pop – to remove an element
Push

Array Implementation of a Stack


int a[10];
int top=-1;

push(int ele)
{ top++; a[top]=ele; }

pop()
{ top--; }

top()
{
printf(“%d”, a[top]);
}

Queue
- Linear DS
- First In First Out principle
- enqueue and dequeue operations
- Front and rear pointers
- Insertion at rear, deletion at front

enqueue:
Fixed front approach

- front remains fixed


- after each deletion all remaining element moves.

Array Implementation of queue – Fixed front approach


int a[10];
int f,r =-1;
enqueue(int ele)
{
if (r==-1)
{ r=f=0; a[r]=ele; }
else
{ r++; a[r]=ele; }
}

dequeue()
{
printf(“%d”, a[f]);
for (i=f+1;i<=r;i++)
{ a[i-1]=a[i]; }
}

display()
{ for (i=f+1;i<=r;i++)
{ printf(“%d”, a[i]); }
}

Floating front approach


- Front moves while deleting an element
- front moves beyond rear until the stack is full

Floating front queue – array implementation


int a[10]; int f,r =-1;
enqueue(int ele)
{
if (r==-1)
{ r=f=0; a[r]=ele; }
else
{ r=(r+1)%10; a[r]=ele; }
}

dequeue()
{
printf(“%d”, a[f]); f++;
}

display()
{
for (i=f,t=0;t<c;i%10++,t++)
{ printf(“%d”, a[i]); }
}

Applications of stack
- Function calls uses stack
- Expression evaluation

Application of queue

- Priority queue
Infix expression
a+b*c+(d*e+f)*g
Expression Evaluation

6 5 2 3 + 8 *+ 3 + *
Linked list

- a typical node in a linked representation

struct node{
int data;
struct node *link;
}*engine;
Inserting a node into a linked list

insert(int n)
{
struct node *temp;
if(engine==NULL)
{
engine=(struct node *)malloc(sizeof(struct node));
engine-> data = n;
engine-> link = NULL;
}
else
{
last = engine;
while (last-> link != NULL)
last = last-> link;
last-> link = (struct node *)malloc(sizeof(struct node));
last = last-> link;
last-> data = n;
last-> link= NULL;
}

}
Inserting a newly created node after the specified node
newinsert ( int node_no, int value )
{
struct node *temp, * temp1; int i;

if ( node_no == 0)
{
temp = ( struct node * )malloc ( sizeof ( struct node ));
temp -> data = value;
temp -> link = engine;
engine = temp ;
}
else
{
temp = engine ;
i = 1;
while ( i < node_no )
{
i = i+1;
temp = temp-> link ;
}
temp1 = ( struct node * )malloc ( sizeof(struct node));

temp1 -> data = value ;


temp1 -> link = temp -> link;
temp -> link = temp1;
}
}
Deleting the element from the list

delete(int node_no, int value )


{
struct node *temp, * temp1; int i;

if ( node_no == 0)
{
engine=enginelink;
}
else
{
temp = engine ;
i = 1;
while ( i < node_no )
{
i = i+1;
temp = temp-> link ;
}
temp1 = templink;

temp -> link = temp1 -> link;


free(temp1);
}
}
Printing the elements of the list

void printlist ( )
{
struct node *t;
t=engine;
printf("The data values in the list are\n");
while (t!= NULL)
{
printf("%d\t",t-> data);
t = t-> link;
}
}
Main function

void main ()
{
int n;
int x;
printf("Enter the nodes to be created \n");
scanf("%d",&n);
while ( n- > 0 )
{
printf( "Enter the data values to be placed in a node\
n");
scanf("%d",&x);
insert ( x );
}
printf(" The list before deletion is\n");
printlist ();
printf(" \n Enter the node no after which the insertion is
to be done\n");
scanf ( " %d",&n);
printf("Enter the value of the node\n");
scanf("%d",&x);
newinsert(n,x);
printf("The list after insertion is \n");
printlist();
}
Linked stack

- Add every new element to the beginning

push(int x)
{

if (engine==NULL)
{
engine=(struct node *)malloc(sizeof(struct node*));

enginedata=x;
enginelink=NULL;
}

else
{
t=(struct node *)malloc(sizeof(struct node*));
tdata=x;
tlink=engine;
engine=t ;
}
}
pop()
{
if (engine !=NULL)
{ printf(“%d”,enginedata);
engine=enginelink;
}
else
{printf (“stack empty”);}
}
Linked queue

struct node *f,*r;


f=r=NULL;
enqueue(int x)
{

if (r==NULL)
{
r=(struct node *)malloc(sizeof(struct node*));

rdata=x;
rlink=NULL;
f=r;
}

else
{
rlink=(struct node *)malloc(sizeof(struct node*));
r=rlink ;
rdata=x;
rlink=NULL;
}
}
dequeue()
{
if (f!=NULL)
{
printf(“%d”,fdata);
f=flink;
}
else
{
r=NULL;
}
}
Linked priority queue

struct node *f,*r;


f=r=NULL;
prio_enqueue(int x)
{

if (r==NULL)
{
r=(struct node *)malloc(sizeof(struct node*));

rdata=x;
rlink=NULL;
f=r;
}

else
{
rlink=(struct node *)malloc(sizeof(struct node*));
r=rlink ;
rdata=x;
rlink=NULL;
}
}
prio_dequeue()
{

struct node *t,*min;


int x;
t=min=f;

while(t!=NULL)
{
if(tdata<mindata)
{
min=t;
}
t=tlink;
}
x=mindata;
mindata=fdata;
fdata=x;

if (f!=NULL)
{
printf(“%d”,fdata);
f=flink;
}
else
{
r=NULL;
}
}
Circular list, stack, queue

-
Memory Mangement Functions
Malloc()

char *p;
p=(char *)malloc(sizeof(char));
Calloc()

char *p;
p=(char *) calloc(n, sizeof(char));
Realloc()

p= realloc(ptr, size);

Creates a new block based on size mentioned.


Free()
- to release memory to the heap
int *p= (int *)malloc(sizeof(int));
free(p);
Disadvantages of singly linked list
The following are problems with singly linked lists:
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 whose link points
to the node to be deleted.
3. If the link in any node gets corrupted, the remaining
nodes of the list become unusable.
DOUBLY LINKED LISTS
Node of a doubly linked list

struct dnode
{
int data;
struct dnode *left,*right;
};
Inserting an element into DLL

struct dnode *insert(struct dnode *p,


struct dnode **q, int n)
{
struct dnode *temp;

if(p==NULL)
{
p=(struct dnode *)malloc(sizeof(struct dnode));

p->data = n;
p-> left = p->right =NULL;
*q =p;
}
else
{
temp = (struct dnode *)malloc(sizeof(struct dnode));
temp->data = n;
temp->left = (*q);
temp->right = NULL;
(*q)->right = temp;
(*q) = temp;
}
return (p);
}

void main()
{
int n;
int x;
struct dnode *start = NULL ;
struct dnode *end = NULL;
printf("Enter the nodes to be created \n");
scanf("%d",&n);
while ( n-- > 0 )
{
printf( "Enter the data values to be placed in a node\n");
scanf("%d",&x);
start = insert ( start, &end,x );
}
printf("The created list is\n");
printfor ( start );
printrev(end);
}
Deleting from a DLL

struct dnode * delete( struct dnode *p, int node_no, int


*val)
{
struct dnode *temp ,*prev=NULL;
int i;

if ( node_no <= 0 || node_no > 4)


{
printf("Error! the specified node does not exist\n");
exit(0);
}

if ( node_no == 1)
{
temp = p;
p = temp->right;
p->left = NULL;
*val = temp->data;
return(p);
}

else
{
temp = p ;
i = 1;
while ( i < node_no )
{
i = i+1;
prev = temp;
temp = temp-> right ;
}
prev->right = temp->right;
if(temp->right != NULL)
temp->right->left = prev;
*val = temp->data;
free(temp);
}
return (p);
}
Applications of doubly linked list
1. Applications that have an MRU list (a linked list of file names)

2. The cache in your browser that allows you to hit the BACK button (a linked list of
URLs)

3. Undo functionality in Photoshop or Word (a linked list of state)

4. A stack, hash table, and binary tree can be implemented using a doubly linked list.

5. A great way to represent a deck of cards in a game.

Before termination of process p1.


After termination of process p1.

You might also like