Data Struture and Alghorithem
Data Struture and Alghorithem
Algorithms (CoSc2042 )
Data Structure and its applications
There are two broad types of data structure based on
their memory allocation:
• Static Data Structures
• Dynamic Data Structure
Static Data Structures
• data structures that are defined & allocated before
execution, thus the size cannot be changed during
time of execution.
• Example: Array implementation of ADTs.
Cont.
Dynamic Data Structure
• Dynamic Data Structure Are data structure that can grow and
shrink in size or permits discarding of unwanted memory
during execution time.
• Example: Linked list implementation of ADTs.
Structure
• Structure is a collection of data items and the data items can be
of different data type.
• The data item of structure is called member of the structure.
Declaration of structure
• Structure is defined using the struct keyword.
Cont.
Example :
struct student
{
string name;
int age;
string Dept;
};
• The struct keyword creates a new user defined data
type that is used to declare variable of an aggregated
data type.
Cont.
Accessing Members of Structure Variables
• The Dot operator (.): to access data members of
structure variables.
• The Arrow operator (->): to access data members of
pointer variables pointing to the structure.
• Example:
struct student stud;
struct student *studptr;
cout<<stud.name;
OR
Studptr=&stud;
cout<studptr->name;//access stud.name
Cont.
Self-Referential Structures
• Structures can hold pointers to instances of
themselves.
• Example:
struct student
{
char name[20];
int age;
char Dept[20];
struct student *next;
};
Linked List
• Linked List is self-referential structure.
• It is a collection of elements called nodes, each of
which stores two types of fields.
• Data items and a pointer to next node in the case
of singly linked list and pointer to previous node in
the case of doubly linked list.
• The data field: holds the actual elements on the
list.
• The pointer field: contains the address of the next
and/or previous node in the list.
Types of Linked List
Steps
• Allocate a new node.
• Set the node data values and make the next pointer
of the new node point to NULL.
• Make old last node’s next pointer point to the new
node.
• Update end to point to the new node.
Adding a node to the end of a singly
linked list
Steps
• Allocate a new node.
• Insert new element values.
• Make the next pointer of the new node point to old
head (start).
• Update head (start) to point to the new node.
Adding a node to the front of a singly linked list
void insert_front(int x)
{
node *temp=new node;
temp->data=x;
temp->next=NULL;
if(head==NULL)
head = temp;
else
{
temp->next = head;
head = temp;
}}
Adding a node to the right of a specific value
in a singly linked list
void delete_front()
{
node *temp;
if(head==NULL)
cout <<"No data inside\n";
else
{
temp = head;
head = head->next;
delete temp;
}}
Deleting a node from the end of a singly
linked list
void delete_end()
{
node *temp, *temp3;
if(head==NULL)
cout <<"No data inside\n";
else {
temp = head;
while(temp->next!=NULL) {
temp3 = temp;
temp = temp->next;
}
temp3->next = NULL;
delete temp;
}}
Deleting a node of specific data of a
singly linked list
void delete_any(int x) else
{ {
node *temp, *temp3; temp = head;
if(head==NULL) while(temp->data!=x)
cout <<"No data inside\n"; {
if(head->data==x) temp3 = temp;
{ temp = temp->next;
temp = head; }
head = head->next; temp3->next = temp->next;
delete temp; delete temp;
} }}
Display in a forward manner in a singly
linked list
void display()
{
node *temp;
if(head==NULL)
cout << "No data inside\n";
else{
temp = head;
while(temp!=NULL){
cout <<temp->data << endl;
temp = temp->next;
}}}
Doubly linked lists
• Each node points not only to Successor node (Next
node), but also to Predecessor node (Previous
node).
• There are two NULL: at the first and last nodes in
the linked list.
• Advantage: given a node, it is easy to visit its
predecessor (previous) node. It is convenient to
traverse linked lists Forwards and Backwards.
Doubly linked lists (Cont..)
Doubly linked lists (Cont..)
struct node
{
int data;
node *prev;
node *next;
};
node *head = NULL, *tail = NULL;
Adding a node to the end of a doubly
linked list
void insert_end(int x)
{
node* temp = new node;
temp->data = x;
temp->next = NULL;
temp->prev = NULL;
if (head == NULL)
head = tail = temp;
else {
tail->next = temp;
temp->prev = tail;
tail = temp; }}
Adding a node to the front of a doubly
linked list
void insert_front(int x)
{
node* temp = new node;
temp->data = x;
temp->next = NULL;
temp->prev = NULL;
if (head == NULL)
head = tail = temp;
else{
temp->next = head;
head->prev = temp;
head = temp;
}}
Adding a node to the left of a specific data
in a doubly linked list
void insert_left_y(int x, int y) else
{ {
node* temp = new node; node *temp2 = head,
*temp3;
temp->data = x;
while(temp2->data!=y)
temp->next = NULL;
{
temp->prev = NULL;
temp3 = temp2;
if (head == NULL)
temp2 = temp2->next;
head = tail = temp;
}
else if(head->data==y){
temp->next = temp3->next;
temp->next = head;
temp3->next = temp;
head->prev = temp;
temp->prev = temp3;
head = temp; }
temp2->prev = temp; }}
Adding a node to the right of a specific
data in a doubly linked list
void insert_right_y(int x, int y)
{
node* temp = new node; else
temp->data = x; {
temp->next = NULL; node *temp2 = head;
temp->prev = NULL; while(temp2->data!=y)
if (head == NULL) {
head = tail = temp; temp2 = temp2->next;
else if(head->data==y) }
{ if(temp2->next==NULL)
if(head->next==NULL) tail = temp;
temp->prev = temp2;
tail = temp;
temp->next = temp2->next;
temp->prev = head;
temp2->next->prev = temp;
temp->next = head->next; temp2->next = temp;
head->next->prev = temp; }}
head->next = temp; }
Deleting a node from the end of a doubly
linked list
void delete_end()
{
node *temp;
if(tail==NULL)
cout <<"No data inside\n";
else {
temp = tail;
tail = tail->prev;
tail->next = NULL;
delete temp;
}}
Deleting a node from the front of a doubly
linked list
void delete_front()
{
node *temp;
if(head==NULL)
cout <<"No data inside\n";
else
{
temp = head;
head = head->next;
head->prev = NULL;
delete temp;
}}
Deleting any node using the search data from
a doubly linked list
void delete_any(int y)
{
if(head==NULL)
cout <<"No data inside\n";
else {
node *temp = head, *temp2;
while(temp->data != y)
{
temp2 = temp;
temp = temp->next; }
temp2->next = temp->next;
temp->next->prev = temp2;
delete temp;
}}
Display the node from the doubly linked list
in a forward manner
void display_forward()
{
node *temp;
if(head==NULL)
cout <<"No data inside\n";
else{
temp = head;
while(temp!=NULL)
{
cout << temp->data << endl;
temp = temp->next;
}}}
Display the node from the doubly linked list
in a backward manner
void display_backward()
{
node *temp;
if(tail==NULL)
cout <<"No data inside\n";
else{
temp = tail;
while(temp!=NULL)
{
cout << temp->data << endl;
temp = temp->prev;
}}}
Circular linked lists
• Circular linked lists: The last node points to the first node of the list.
Cont..
struct node
{
int data;
node *next;
};
node *head = NULL;
Adding a node to the end of a Circular Singly
linked list
void insert_end(int x)
{
node *temp = new node;
temp->data = x;
temp->next = temp;
if(head==NULL)
head = temp;
else{
node *temp2 = head;
while(temp2->next!=head) {
temp2 = temp2->next;
}
temp->next = head;
temp2->next = temp;
}}
Adding a node to the front of a Circular
Singly linked list
void insert_front(int x)
{
node *temp = new node;
temp->data = x;
temp->next = temp;
if(head==NULL)
head = temp;
else{
node *temp2 = head;
while(temp2->next!=head) {
temp2 = temp2->next;
}
temp->next = head;
head = temp;
temp2->next = temp;
}}
Adding a node to the left of a specific data in
a Circular Singly linked list
void display()
{
node *temp;
if(head==NULL)
cout <<"No data inside\n";
else{
temp = head;
while(temp->next!=head)
{
cout << temp->data << endl;
temp = temp->next;
}
cout<<temp->data<<endl;
}}
Questions
1. Write a C++ program to create and display a Singly Linked List with 4 nodes.
Then insert new nodes at the end, beginning, before the 3rd, and after the 3rd
node. Finally display all elements of the linked list in reverse order.
2. Write a C++ program to delete first node, last node, and specific node at the
middle of a given Singly Linked List.
3. Write a C++ program to create a singly linked list of n nodes and
count the number of nodes and displays it.
4. Write a C++ program to create and display a Doubly Linked List with 4 nodes.
Then insert new nodes at the end, beginning, before the 3rd, and after the 3rd
node. Finally display all elements of the linked list in reverse order.
5. Write a C++ program to delete first node, last node, and specific node at the
middle of a given Doubly Linked List.
6. Write a C++ program to create a doubly linked list of n nodes and
count the number of nodes and displays it.
Questions (cont.)
7. Write a C++ program to create and display a Circular singly Linked List with 4
nodes. Then insert new nodes at the end, beginning, before the 3rd, and after the 3rd
node. Finally display all elements of the linked list in reverse order.
8. Write a C++ program to delete first node, last node, and specific node at the middle of
a given Circular singly Linked List.
9. Write a C++ program to create a Circular singly linked list of n nodes and
count the number of nodes and displays it.
10. Write a C++ program to create and display a Circular doubly Linked List with 4
nodes. Then insert new nodes at the end, beginning, before the 3rd, and after the 3rd
node. Finally display all elements of the linked list in reverse order.
11. Write a C++ program to delete first node, last node, and specific node at the middle of
a given Circular doubly Linked List.
12. Write a C++ program to create a Circular doubly linked list of n nodes and
count the number of nodes and displays it.
THANK YOU!