module iii linear data structures and their linked storage … · 2018-10-16 · module iii linear...
TRANSCRIPT
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
Module III
Linear Data Structures and their Linked Storage Representation
3.1 Linked List
Linked list is the collection of inter connected nodes with a head node representing the first node
and a tail node representing the last node of it. A node can be viewed as a block consisting of
two major fields: a data field and a pointer field. The pointer field is used to interconnect nodes
of a list. Below diagram depicts the linked list setup.
3.2 Linked list types
There are few variants of linked lists. Below diagram shows a brief classification of Linked
Lists.
Regarding these variants of LL, explanations and examples are given in subsequent sections of
the notes.
3.2 Linked List Representation
A simple representation of singly linked list would look like the below:
struct node {
int data;
struct node *next; };
Tail
NULL
Linked List
Singly LL
Singly Circular LL
Circular LL
Doubly LL
Doubly Circular LL
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
struct node *newnode;
With the above declaration we can create a node of the pattern below:
data *next
newnode
Initially, the pointer field of the first node in singly linked list would contain NULL value.
Instruction that makes it is: newnode->next = NULL;
Note: Structure variables’ fields of self referential type are accessed through -> (arrow
operator)
Now, let us try to understand few conventions w.r.t linked lists. Consider below given list.
head (1000h) (1008h) (1016h) tail (1024h)
In this list, head node contains data = 10 and tail node contains data = 40. This list has 4 nodes
that are interconnected. Head node is connected to the next node since the pointer value contains
the address of next node. Tail node is not linked to any node since its pointer value is NULL.
struct node *head, *tail, *newnode; // variables declared of type struct node
head = (struct node *) malloc (1 * sizeof(struct node));
This instruction creates one node of type struct. Memory gets allocated using DMA function
malloc(). Created node in the above instruction is referred to as “head”. It can be any other name
as well.
head->data = 10; // assigns head node’s data field with 10
head->next = NULL; //assigning pointer value to NULL
head; tail
tail = head; // assigning the reference of head to tail. This means both head and tail are referring
to the same node.
Now, let us add a new node to this list.
newnode = (struct node *) malloc (1 * sizeof(struct node));
newnode->data = 20; newnode->next = NULL;
10 1008h 20 1016h 30 1024h 40 NULL
10 NULL
10 NULL 10 NULL
Head; tail newnode
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
The above nodes can be connected using the following instructions:
head->next = newnode; // assigning head node’s pointer value with address of newnode
head; tail (1000h) newnode (1008h)
Now, if we execute the instruction, tail = newnode; tail & newnode both refer to the node with
value 20.
In this way, linked lists can be created.
3.3 Operations on Linked Lists
Major operations performed on linked lists are inserting a node, deleting a node, searching a
node and traversing the list to display all of its nodes.
a) Inserting a node: Node can be inserted at the beginning, at the end or anywhere in the
linked list
head (1000h) (1008h) (1016h) tail (1024h)
Consider the above list. Suppose we wish to insert a node at the beginning, following set of
instruction are to be executed.
newnode = (struct node *) malloc (1 * sizeof(struct node));
newnode->data = 50; newnode->next = head;
head = newnode;
(1000h) (1008h) (1016h) tail (1024h)
head; newnode
Suppose we wish to insert a node at the end, following set of instruction are to be executed.
head (1000h) (1008h) (1016h) tail (1024h)
newnode = (struct node *) malloc (1 * sizeof(struct node));
newnode->data = 80; newnode->next = NULL; (1032h)
10 1008h 20 NULL
10 1008h 20 1016h 30 1024h 40 NULL
50 1000h
10 1008h 20 1016h 30 1024h 40 NULL
10 1008h 20 1016h 30 1024h 40 1032h
50 1000h
80 NULL
80 NULL
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
Take a temporary variable of type struct node, traverse the list until you reach the last node (i.e
tail )
while(temp->next !=NULL)
{
temp=temp->next;
}
temp->next = newnode;
tail = newnode;
Now let us see how to insert a node at a particular position in the list. For this, we need to ask
user to specify after which node in the list he would want to insert. Call it as “key” node. This
can be realized as below:
temp
head (1000h) (1008h) (1016h) tail (1024h)
Key = 30
Newnode (1040h) (1032h)
Take a temporary variable of type struct node, traverse the list until you reach the desired node
(i.e 30)
while(temp->data !=key)
{
temp=temp->next; newnode (1040h)
}
newnode->next = temp->next;
temp->next = newnode;
b) Deleting a node: A node in the list can be deleted using the standard function free ( ). Let
us see an example to understand delete operations on linked list.
head (1000h) temp2 (1008h) temp(1016h) tail (1024h)
Let us try to delete node with 30 data value.
Take two temporary variables and position them to key node and the previous node of it.
10 1008h 20 1016h 30 1040h 40 1032h
80 NULL
100 NULL
100 1032h
10 1008h 20 1024h 30 NULL 40 NULL
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
Now, key =30.
While(temp->data!=key)
{
temp=temp->next;
}
While(temp2->next!=key)
{
temp2=temp2->next;
}
temp2->next = temp->next;
temp->next = NULL;
free(temp);
Below C program creates a singly linked list and performs few operations like insert, delete,
display, search, insert and delete at specific positions in the list.
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head, *tail,*temp, *temp2;
void create();
void delet();
void dele_spec();
void display();
void spec_ins();
int search(int);
int num,con=1,ch,key,f;
void main()
{
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
clrscr();
head=NULL;
printf("1.Create\n2.Delete\n3.Delete Specific\n4.Insert After Specific node\n5.Display\n6.
Search\n7.Exit\n");
while(1)
{
printf("Enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter number\n");
scanf("%d",&num);
create();
break;
case 2: delet();
break;
case 3: dele_spec();
break;
case 4: spec_ins();
break;
case 5: display();
break;
case 6: printf("Enter the node data to be searched\n");
scanf("%d",&key);
f=search(key);
if(f==1) printf("NODE FOUND\n");
else printf("NODE NOT FOUND\n");
break;
case 7: exit(0);
}
}
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
}
void create()
{
if(head==NULL)
{
head = (struct node*)malloc(1*sizeof(struct node));
head->data=num;
head->next=NULL;
tail=head;
}
else
{
temp= (struct node*)malloc(1*sizeof(struct node));
temp->data=num;
tail->next=temp;
temp->next=NULL;
tail=temp;
}
}
void delet()
{
if(head==NULL)
{
printf("List is empty\n");
}
else
{
temp=head;
temp = temp->next;
free(head);
head=temp;
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
}
}
void dele_spec()
{
int key;
printf("Enter the node to be deleted\n");
scanf("%d",&key);
temp=head;
temp2=head;
while(temp->data!=key)
{
temp=temp->next;
}
while(temp2->next!=temp)
{
temp2=temp2->next;
}
if(temp->next==NULL)
{
temp2->next=NULL;
free(temp);
tail=temp2;
}
else
{
temp2->next=temp->next;
free(temp);
}
}
void spec_ins()
{
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
int key;
printf("Enter the node info next to which to be inserted\n");
scanf("%d",&key);
printf("Enter data for newnode\n");
scanf("%d",&num);
temp2= (struct node*)malloc(1*sizeof(struct node));
temp2->data=num;
temp=head;
while(temp->data!=key)
{
temp=temp->next;
}
temp2->next=temp->next;
temp->next=temp2;
}
void display()
{
if(head==NULL)
{
printf("List empty\n");
}
else
{
temp=head;
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->next;
}
}
}
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
int search(int key)
{
int flag=0;
temp=head;
while(temp!=NULL)
{
if(temp->data==key)
{
flag=1;
return flag;
}
else
temp=temp->next;
}
return flag;
}
Advantages of Linked Lists
They are a dynamic in nature which allocates the memory when required.
Insertion and deletion operations can be easily implemented.
Stacks and queues can be easily executed.
Linked List reduces the access time.
Disadvantages of Linked Lists
The memory is wasted as pointers require extra memory for storage.
No element can be accessed randomly; it has to access each node sequentially.
Reverse Traversing is difficult in linked list.
Applications of Linked Lists
Linked lists are used to implement stacks, queues, graphs, etc.
Linked lists let you insert elements at the beginning and end of the list.
In Linked Lists we don’t need to know the size in advance.
3.4 Types of Linked Lists
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
Singly Linked List : Singly linked lists contain nodes which have a data part as well as an
address part i.e. next, which points to the next node in sequence of nodes. The operations we can
perform on singly linked lists are insertion, deletion and traversal.
Doubly Linked List : In a doubly linked list, each node contains two links the first link points to
the previous node and the next link points to the next node in the sequence.
Circular Linked List :
Singly Circular LL: In the circular linked list the last node of the list contains the address
of the first node and forms a circular chain.
Doubly Circular LL: In the doubly circular linked list the last node’s next ptr field will
have the address of start (head) node and the start (head) node’s previous ptr field will have the
address of last (tail) node. Below diagram shows its pictorial representation.
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
3.5 Header Linked List
Header
Header linked list is similar to Singly linked List but the difference is that in this type of list,
there exists a special node called “header”, which either contains information regarding total
nodes in the list or the address of the last node in the whole list.
C Program to implement header linked list is shown below (For execution purpose)
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head, *header,*last,*temp, *newnode;
void create();
void delet();
void display();
int num,count=0,ch;
void main()
{
clrscr();
head=NULL;
3
Head
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
printf("1.Create\n2.Delete\n3.Display\n4.Exit\n");
while(1)
{
printf("Enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: if(head==NULL)
{
header = (struct node*)malloc(1*sizeof(struct node));
header->data=count;
}
printf("Enter number\n");
scanf("%d",&num);
create();
break;
case 2: delet();
break;
case 3: display();
break;
case 4: exit(0);
}
}
}
void create()
{
if (head == NULL){
head = (struct node*)malloc(1*sizeof(struct node));
head->data=num;
head->next = NULL;
header->next = head;
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
tail =head;
count++;
header->next = count;
}
else{
newnode = (struct node*)malloc(1*sizeof(struct node));
newnode->data=num;
newnode->next=NULL;
tail->next=newnode;
tail=newnode;
count++;
header->data=count;
}
void delet()
{
if(head->data==0)
{
printf("List is empty\n");
}
else
{
temp=head;
while(temp->next!=last)
{
temp=temp->next;
}
free(tail);
tail =temp;
tail->next=NULL;
}
count--;
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
header->data=count;
}
void display()
{
if(head==NULL)
{
printf("List empty\n");
}
else
{
temp=head;
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->ptr;
}
}
printf("\nHeader Information:%d nodes\n",header->data);
}
C Program to implement header linked list is shown below (For Exam purpose)
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head,*header,*tail,*temp,*newnode;
void create(int);
void delet();
void display();
int num,count=0,ch;
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
void main()
{
clrscr();
header=(struct node *)malloc(1*sizeof(struct node));
header->data=count;
header->next=NULL;
create(10);
create(20);
create(30);
delet();
display();
}
void create(int num)
{
if(head==NULL)
{
head=(struct node*)malloc(1*sizeof(struct node));
head->data=num;
head->next=NULL;
tail=head;
count++;
}
else
{
newnode = (struct node*)malloc(1*sizeof(struct node));
newnode->data=num;
newnode->next=NULL;
tail->next=newnode;
tail=newnode;
count++;
header->data=count;
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
}
}
void delet()
{
if(header==NULL)
{
printf("List is empty\n");
}
else
{
temp=head;
while(temp->next!=tail)
{
temp=temp->next;
}
free(tail);
tail =temp;
tail->next=NULL;
}
count--;
header->data=count;
}
void display()
{
if(head==NULL)
{
printf("List empty\n");
}
else
{
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
temp=head;
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->next;
}
}
printf("\nHeader Information:%d\n",header->data);
}
3.5 Linked implementation of Stack
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int Data;
struct Node *next;
}*top;
void popStack()
{
struct Node *temp, *var=top;
if(var==top)
{
top = top->next;
free(var);
}
else
printf("\nStack Empty");
}
void push(int value)
{
struct Node *temp;
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (top == NULL)
{
top=temp;
top->next=NULL;
}
else
{
temp->next=top;
top=temp;
}
}
void display()
{
struct Node *var=top;
if(var!=NULL)
{
printf("\nElements are as:\n");
while(var!=NULL)
{
printf("\t%d\n",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nStack is Empty");
}
int main(int argc, char *argv[])
{
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
int i=0;
top=NULL;
printf(" \n1. Push to stack");
printf(" \n2. Pop from Stack");
printf(" \n3. Display data of Stack");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
{
int value;
printf("\nEnter a valueber to push into Stack: ");
scanf("%d",&value);
push(value);
display();
break;
}
case 2:
{
popStack();
display();
break;
}
case 3:
{
display();
break;
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
}
case 4:
{
struct Node *temp;
while(top!=NULL)
{
temp = top->next;
free(top);
top=temp;
}
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}
}
}
3.6 Merging of two lists
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void insert_list1();
void insert_list2();
void display();
void merge_list();
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
struct node *list1, *list2,*list3,*temp1,*temp2,*temp3,*temp4;
void main()
{
int ch;
clrscr();
printf("1 for insert in first linklist\n");
printf("2 for insert in second linklist\n");
printf("3 for display first & second linklist\n");
printf("4 for merge linklist\n");
printf("5 for exit\n");
while(1)
{
printf("\nEnter ur choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_list1();
break;
case 2: insert_list2();
break;
case 3: display();
break;
case 4: merge_list();
break;
case 5: exit(0);
}
}
}
void merge_list()
{
temp1=list1;
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
temp2=list2;
while(temp1!=NULL || temp2!=NULL)
{
temp3=(struct node*)malloc(1*sizeof(struct node));
if(list3==NULL)
{
list3=temp3;
temp4=temp3;
}
if(temp1->data<temp2->data)
{
temp3->data=temp1->data;
temp4->next=temp3;
temp4=temp3;
temp1=temp1->next;
}
else
{
temp3->data=temp2->data;
temp4->next=temp3;
temp4=temp3;
temp2=temp2->next;
}
if(temp2==NULL)
{
while(temp1!=NULL)
{
temp3=(struct node*)malloc(1*sizeof(struct node));
temp3->data=temp1->data;
temp4->next=temp3;
temp4=temp3;
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
temp1=temp1->next;
}
break;
}
if(temp1==NULL)
{
while(temp2!=NULL)
{
temp3=(struct node*)malloc(1*sizeof(struct node));
temp3->data=temp2->data;
temp4->next=temp3;
temp4=temp3;
temp2=temp2->next;
}
break;
}
}
temp3->next=NULL;
}
void insert_list1()
{
struct node *new;
if(list1==NULL)
{
list1=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&list1->data);
list1->next=NULL;
temp1=list1;
}
else
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
{
new=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&new->data);
new->next=NULL;
temp1->next=new;
temp1=temp1->next;
}
}
void insert_list2()
{
struct node *new;
if(list2==NULL)
{
list2=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&list2->data);
list2->next=NULL;
temp2=list2;
}
else
{
new=(struct node *)malloc(sizeof(struct node));
printf("Enter the info");
scanf("%d",&new->data);
new->next=NULL;
temp2->next=new;
temp2=temp2->next;
}
}
Data Structures and Applications (17CS33)
© Mr. Nischaykumar Hegde, CSE, VCET, Puttur
void display()
{
temp1=list1;
temp2=list2;
printf("\nThe Information of First linklist:");
while(temp1!=NULL)
{
printf("%d ",temp1->data);
temp1=temp1->next;
}
printf("\nThe Information of Second linklist:");
while(temp2!=NULL)
{
printf("%d ",temp2->data);
temp2=temp2->next;
}
temp3=list3;
printf("\nMerged List:");
while(temp3!=NULL)
{
printf("%d ",temp3->data);
temp3=temp3->next;
}
}