data structures linked lists namiq sultan reference: vinu v das, principles of data structures using...
TRANSCRIPT
![Page 1: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/1.jpg)
Data Structures
Linked Lists
Namiq Sultan
Reference: Vinu V Das, Principles of Data Structures using C and C++
1
![Page 2: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/2.jpg)
Linked Lists
2
![Page 3: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/3.jpg)
Linked Lists
• START = NULL if there is no list (i.e.; NULL list or empty list).
3
![Page 4: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/4.jpg)
ADVANTAGES OF LINKED LIST
Linked list have many advantages and some of them are:
1. Linked list are dynamic data structure. That is, they can grow or shrink
during the execution of a program.
2. Efficient memory utilization: Memory is allocated whenever it is required.
And it is deallocated when it is not needed.
3. Insertion and deletion are easier and efficient.
4. Many complex applications can be easily carried out with linked list.
4
![Page 5: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/5.jpg)
OPERATIONS ON LINKED LISTS
The primitive operations performed on the linked list are as follows
1. Creation
2. Insertion
3. Deletion
4. Traversing
5. Searching
6. Concatenation
5
![Page 6: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/6.jpg)
OPERATIONS ON LINKED LISTS• Creation operation is used to create a linked list.
• Insertion operation is used to insert a new node at any specified location in the linked list. A new node may be inserted.
(a) At the beginning of the linked list
(b) At the end of the linked list
(c) At any specified position in between in a linked list
• Deletion operation is used to delete a node from the linked list. A node may be deleted from the
(a) Beginning of a linked list
(b) End of a linked list
(c) Specified location of the linked list
• Traversing is the process of going through all the nodes from one end to another end of a linked list.
• Concatenation is the process of appending the second list to the end of the first list.
6
![Page 7: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/7.jpg)
TYPES OF LINKED LIST1. Singly linked list
2. Doubly linked list
3. Circular linked list
7
![Page 8: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/8.jpg)
SINGLY LINKED LIST
8
![Page 9: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/9.jpg)
SINGLY LINKED LIST
9
![Page 10: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/10.jpg)
SINGLY LINKED LIST
10
![Page 11: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/11.jpg)
SINGLY LINKED LIST
11
![Page 12: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/12.jpg)
ALGORITHM FOR INSERTING A NODE
Suppose • START is the first position in linked list.• DATA be the element to be inserted in the new node. • POS is the position where the new node is to be inserted. • TEMP is a temporary pointer to hold the node address.
12
![Page 13: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/13.jpg)
ALGORITHM FOR INSERTING A NODE
Insert a Node at the beginning
1. Input DATA to be inserted
2. Create a NewNode
3. NewNode → DATA = DATA
4. If (SATRT equal to NULL)
(a) NewNode → Next = NULL
5. Else
(a) NewNode → Next = START
6. START = NewNode
7. Exit
13
![Page 14: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/14.jpg)
ALGORITHM FOR INSERTING A NODEInsert a Node at the end
1. Input DATA to be inserted
2. Create a NewNode
3. NewNode → DATA = DATA
4. NewNode → Next = NULL
8. If (SATRT equal to NULL)
(a) START = NewNode
9. Else
(a) TEMP = START
(b) While (TEMP → Next not equal to NULL)
(i) TEMP = TEMP → Next
10. TEMP → Next = NewNode
11. Exit
14
![Page 15: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/15.jpg)
ALGORITHM FOR INSERTING A NODEInsert a Node at any specified position (POS=1 means insert after first node. POS=0 not allowed)
1. Input DATA and POS to be inserted
2. initialise TEMP = START; and k = 1
3. Repeat the step 3 while( k is less than POS)
(a) TEMP = TEMP Next
(b) If (TEMP is equal to NULL)
(i) Display “Node in the list less than the position”
(ii) Exit
(c) k = k + 1
4. Create a New Node
5. NewNode DATA = DATA
6. NewNode Next = TEMP → Next
7. TEMP Next = NewNode
8. Exit 15
![Page 16: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/16.jpg)
ALGORITHM FOR DELETING A NODE
Suppose • START is the first position in linked list. Let • DATA be the element to be deleted. • TEMP, HOLD is a temporary pointer to hold the node address.
16
![Page 17: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/17.jpg)
ALGORITHM FOR DELETING A NODE
1. Input the DATA to be deleted
2. if ((START → DATA) is equal to DATA) // Delete first node
(a) TEMP = START
(b) START = START Next
(c) Set free the node TEMP, which is deleted
(d) Exit
3. HOLD = START
4. while ((HOLD Next Next) not equal to NULL)) // Between first & last
(a) if ((HOLD NEXT DATA) equal to DATA)
(i) TEMP = HOLD Next
(ii) HOLD Next = TEMP Next
(iii) Set free the node TEMP, which is deleted
(iv) Exit
(b) HOLD = HOLD Next17
![Page 18: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/18.jpg)
ALGORITHM FOR DELETING A NODE
5. if ((HOLD next DATA) = = DATA) // last node
(a) TEMP = HOLD → Next
(b) Set free the node TEMP, which is deleted
(c) HOLD Next = NULL
(d) Exit
6. Disply “DATA not found”
7. Exit
18
![Page 19: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/19.jpg)
ALGORITHM FOR SEARCHING A NODE1. Input the DATA to be searched
2. Initialize TEMP = START; POS = 1;
3. Repeat the step 4, 5 and 6 until (TEMP is equal to NULL)
4. If (TEMP DATA is equal to DATA)
(a) Display “The data is found at POS”
(b) Exit
5. TEMP = TEMP Next
6. POS = POS+1
7. If (TEMP is equal to NULL)
(a) Display “The data is not found in the list”
8. Exit
19
![Page 20: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/20.jpg)
ALGORITHM FOR DISPLAY ALL NODES1. If (START is equal to NULL)
(a) Display “The list is Empty”
(b) Exit
2. Initialize TEMP = START
3. Repeat the step 4 and 5 until (TEMP = = NULL )
4. Display “TEMP DATA”
5. TEMP = TEMP Next
6. Exit
20
![Page 21: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/21.jpg)
Programming Linked Listclass LinkedList{
//Structure declaration for the node
struct node{
int info;
node *next;
};
//private structure variable declared
node *start;
public:
LinkedList()//Constructor defined
{
start = NULL;
}
//public fucntion declared
void Create_List(int);
void AddAtBeg(int);
void AddAfter(int, int);
void Delete();
void Count();
void Search(int);
void Display();
};
21
![Page 22: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/22.jpg)
Programming Linked List//This function will create a new linked list of elements
void LinkedList::Create_List(int data)
{
node *q, *newNode;
//New node is created with new operator
newNode = new node;
newNode->info = data;
newNode->next = NULL;
if (start == NULL) //If list is empty
start = newNode;
else { //Element inserted at the end */
q = start;
while(q->next != NULL)
q = q->next;
q->next = newNode;
}
}/*End of create_list()*/ 22
![Page 23: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/23.jpg)
Programming Linked List
//following function will add new element at the beginning
void LinkedList::AddAtBeg(int data)
{
node *newNode;
newNode = new node;
newNode->info = data;
newNode->next = start;
start = newNode;}/*End of addatbeg()*/
23
![Page 24: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/24.jpg)
Programming Linked List//This function will add new element at any specified position
void LinkedList::AddAfter(int data, int pos)
{ node *newNode, *q;
int i;
q = start;
//Finding the position in the linked list to insert
for(i = 0; i<pos-1; i++){
q = q->next;
if(q == NULL) {
cout<<"\n\nThere are less than "<<pos<<" elements" << endl;
return;
}
}/*End of for*/
newNode = new node;
newNode->next = q->next;
newNode->info = data;
q->next = newNode;
}/*End of addafter()*/24
![Page 25: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/25.jpg)
Programming Linked Listvoid LinkedList::Delete()
{
node *tmp, *q;
int data;
if(start == NULL) {
cout<<"\n\nList is empty"<<endl;
return;
}
cout<<"\n\nEnter the element for deletion : ";
cin>>data;
if(start->info == data){
tmp = start;
start = start->next; //First element deleted
delete(tmp);
return;
}
25
![Page 26: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/26.jpg)
Programming Linked List q = start;
while(q->next->next ! = NULL) {
if(q->next->info == data) { //Element deleted in between
tmp = q->next;
q->next = tmp->next;
delete(tmp);
return;
}
q = q->next;
}/*End of while */
if(q->next->info == data) { //Last element deleted
tmp = q->next;
delete(tmp);
q->next = NULL;
return;
}
cout<<"\n\nElement "<<data<<" not found"<<endl;
}/*End of del()*/26
![Page 27: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/27.jpg)
Programming Linked Listvoid LinkedList::Display(){
// Fill in the code that displays the contents of the linked list
}/*End of display() */
void LinkedList::Count(){
// Fill in the code that counts the nodes of the linked list
}/*End of count() */
void LinkedList::Search(int data)
{
// Fill in the code that will find the position of a node that holds the ‘data’
}
27
![Page 28: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/28.jpg)
Programming Linked Listint main()
{
int choice, n, m, position, i;
LinkedList po;
while(1)
{
cout<<"1.Create List\n";
cout<<"2.Add at beginning\n";
cout<<"3.Add after \n";
cout<<"4.Delete\n";
cout<<"5.Display\n";
cout<<"6.Count\n";
cout<<“7.Search\n";
cout<<“8.Quit\n";
cout<<"\nEnter your choice:";
cin>>choice;
28
![Page 29: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/29.jpg)
Programming Linked List switch(choice) {
case 1:
cout<<"\n\nHow many nodes you want:";
cin>>n;
for(i = 0;i<n;i++)
{
cout<<"\nEnter the element:";
cin>>m;
po.Create_List(m);
}
break;
case 2:
cout<<"\n\nEnter the element:";
cin>>m;
po.AddAtBeg(m);
break;
29
![Page 30: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/30.jpg)
Programming Linked Listcase 3:
cout<<"\n\nEnter the element:";cin>>m;cout<<"\nEnter the position after the inserted element :";cin>>position;po.AddAfter(m,position);break;
case 4:po.Delete();break;
case 5:po.Display();break;
case 6:po.Count();break;
case 7:cout<<"\n\nEnter the element to be searched:";
30
![Page 31: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/31.jpg)
Programming Linked Listcin>>m;
po.Search(m);
break;
case 8:
exit(0);
default:
cout<<"\n\nWrong choice";
}/*End of switch */
}/*End of while */
}
31
![Page 32: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/32.jpg)
STACK USING LINKED LISTPUSH
32
![Page 33: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/33.jpg)
STACK USING LINKED LISTPOP
33
![Page 34: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/34.jpg)
STACK USING LINKED LISTALGORITHM FOR PUSH OPERATION
• TOP is NULL when the stack is empty.
• DATA is the data item to be pushed.
1. Input the DATA to be pushed
2. Creat a New Node
3. NewNode → DATA = DATA
4. NewNode → Next = TOP
5. TOP = NewNode
6. Exit
34
![Page 35: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/35.jpg)
STACK USING LINKED LIST• ALGORITHM FOR POP OPERATION
1. if (TOP is equal to NULL)
(a) Display “The stack is empty”
2. Else
(a) TEMP = TOP
(b) Display “The popped element TOP → DATA”
(c) TOP = TOP → Next
(d) TEMP → Next = NULL
(e) Free the TEMP node
3. Exit
35
![Page 36: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/36.jpg)
QUEUE USING LINKED LIST
ADD to queue
36
![Page 37: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/37.jpg)
QUEUE USING LINKED LIST
REMOVE from queue
37
![Page 38: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/38.jpg)
QUEUE USING LINKED LISTALGORITHM FOR PUSHING AN ELEMENT TO A QUEUE
1. Input the DATA element to be pushed
2. Create a New Node
3. NewNode → DATA = DATA
4. NewNode → Next = NULL
5. If(REAR not equal to NULL) // if it is not empty
(a) REAR → next = NewNode
(b) REAR = NewNode
6. Else
REAR = FRONT = NewNode
7. Exit
38
![Page 39: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/39.jpg)
QUEUE USING LINKED LISTALGORITHM FOR POPPING AN ELEMENT FROM A QUEUE
1. If (FRONT is equal to NULL)
(a) Display “The Queue is empty”
2. Else
(a) Display “The popped element is FRONT → DATA”
(b) If(FRONT is not equal to REAR)
(i ) FRONT = FRONT → Next
(c) Else
(i ) FRONT = NULL
(ii) REAR = NULL
3. Exit
39
![Page 40: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/40.jpg)
DOUBLY LINKED LISTEvery nodes in the doubly linked list has three fields:
LeftPointer, RightPointer and DATA.
40
![Page 41: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/41.jpg)
REPRESENTATION OF DOUBLY LINKED LIST
• A node in the doubly linked list can be represented with the following declarations:
struct Node
{
int DATA;
Node * next;
Node * prev;
};
Node * START;
• All operations performed on singly linked list can also be performed on doubly linked list.
41
![Page 42: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/42.jpg)
REPRESENTATION OF DOUBLY LINKED LIST
42
![Page 43: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/43.jpg)
CIRCULAR LINKED LIST• A circular linked list is one, which has no beginning and no end. A singly
linked list can be made a circular linked list by simply storing the address of the very first node in the linked field of the last node.
43
![Page 44: Data Structures Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++ 1](https://reader030.vdocument.in/reader030/viewer/2022032707/56649e555503460f94b4c73a/html5/thumbnails/44.jpg)
CIRCULAR DOUBLY LINKED LIST• A circular doubly linked list has both the successor pointer and predecessor
pointer in circular manner.
44