160 likes | 380 Views
Department of Technical Education Andhra Pradesh Name of : A Santhosh Kumar Designation : Lecturer Branch : Computer Engineering Institute : VMR Polytechnic, Rampur, Warangal Semester : III Subject : Data Structures through ‘C’.
E N D
Department of Technical Education Andhra Pradesh Name of : A Santhosh Kumar Designation : Lecturer Branch : Computer Engineering Institute : VMR Polytechnic, Rampur, Warangal Semester : III Subject : Data Structures through ‘C’. Subject Code : 9CM305 Topic : Linear Data Structures Duration : 50 Mts Sub Topic : Creating a Single Linked List Teaching Aids to be used : Power Point Presentation, Animations Revised by : Bapuji naik 9CM305 .8
Objectives After the Completion of this period, you would be able to know: • What is a Singly Linked List (SLL)? • How to create a Singly Linked List 9CM305 .8
Recap In the last class, you have come to know • What is a linked list? • Advantages of linked lists over arrays • Purpose of a dummy header 9CM305 .8
Singly Linked List • A Singly Linked List is a linked list in which each node has a link pointer to its successor. What is a Singly Linked List? 9CM305 .8
SLL - More terminology • A node’s successor is the next node in the sequence • The last node has no successor • A node’s predecessor is the previous node in the sequence • The first node has no predecessor • A list’s length is the number of elements in it • A list may be empty (contain no elements) 9CM305 .8
Common Operations on a Linked List • Traverse • Insert • Count • Delete • Search • Search and replace • Sort • Reverse 9CM305 .8
temp header 18 786 24 X Operations - Traversing a SLL (animation) 9CM305 .8
Common Operations (contd..) • Traverse – visiting nodes in a linked list • Insert – to add a node in the linked list • Count – number of nodes in the linked list • Delete – remove a node from the linked list 9CM305 .8
Common Operations (contd..) • Search – locate a particular node in the liked list • Search and replace – Identify the node asked and replace with another given node • Sort – arrange all nodes of a liked list in an order • Reverse – to turn around the linked list 9CM305 .8
Program – Declaration Part Singly Linked list - Creation # include <stdio.h.> typedef struct node { in info; struct node *link; } NODE; void append (NODE *) ; NODE *header =NULL; NODE *last=NULL; 9CM305 .8
Singly Linked List - Creation Program – main program main () { NODE *temp; int data; char ch; do { temp = (NODE*)malloc(sizeof(NODE)); printf(“enter the values into new node”); scanf(“%d”,& data); temp info = data; temp link = NULL; append( temp ); printf(“To append another node press Y”); scanf(“%c”, &ch); }while(ch ==‘Y’); } 9CM305 .8
Here is a singly-linked list (SLL): The header points to the first node in the list (or contains the null link if the list is empty) Each node contains a value and a link to its successor (the last node has no successor) a b c d Singly-linked lists – creation (animation) Header X 9CM305 .8
A singly linked list - Creation Discussion Point With the help of above animation, discuss the algorithm to create a singly linked list 9CM305 .8
Singly Linked List - Creation Program – function part void append (t) { NODE *t; if (header == NULL) { header = t; last=header; } else { last->link= t; last=last ->link; } } 9CM305 .8
Summary We have discussed - How a singly linked list looks? - Basic operations on a linked list - Program for creation of a singly Linked List 9CM305 .8
Frequently asked questions • How to declare the structure of a node for a singly linked list? • Define a singly linked list • List various operations on a linked list • Explain about a singly linked list • Write a program to create a singly linked list 9CM305 .8