ncs 301 data structure using c · ncs 301 data structure using c 03-08-15 subject notes by hammad...
TRANSCRIPT
![Page 1: NCS 301 DATA STRUCTURE USING C · NCS 301 Data Structure Using C 03-08-15 Subject Notes by Hammad Lari 2 Continued…. 03-08-2015 Another way of storing a list is to have each element](https://reader033.vdocument.in/reader033/viewer/2022042620/5f37f36460eef019772f6042/html5/thumbnails/1.jpg)
NCS 301 Data Structure Using C 03-08-15
Subject Notes by Hammad Lari 1
NCS 301
DATA STRUCTURE USING C
Hammad Mashkoor Lari
Assistant Professor
Allenhouse Institute of Technology
www.ncs301ds.wordpress.com
Unit-1 Part-4 Linked Lists
Introduction
List refers to linear collection of data items.
Example:-shopping list
Data processing involves storing and processing data organized
into lists. One such way of storing is using arrays.
Advantages of arrays:-
Easy to compute the address of an element in an array(physical
relationship of data in memory).
Disadvantages:-
1. Relatively expensive to insert and delete elements
2. Array usually occupies a block of memory space one cannot
simply double or triple the size of array when required(arrays
are called dense lists) 03-08-2015
![Page 2: NCS 301 DATA STRUCTURE USING C · NCS 301 Data Structure Using C 03-08-15 Subject Notes by Hammad Lari 2 Continued…. 03-08-2015 Another way of storing a list is to have each element](https://reader033.vdocument.in/reader033/viewer/2022042620/5f37f36460eef019772f6042/html5/thumbnails/2.jpg)
NCS 301 Data Structure Using C 03-08-15
Subject Notes by Hammad Lari 2
Continued….
03-08-2015
Another way of storing a list is to have each element in list
contain a field called a link or pointer which contain the address
of next element in the list.
No adjacent space.
Easy to delete and insert elements
This is called linked lists
Linked lists
03-08-2015
A linked list or one way list is a linear collection of data elements called
nodes where linear order is given by means of pointers.
Each node is divided into 2 parts:-
1. First part contains information about element
2. Second part called link or nextpointer field contains address of next node.
Pointer of last node contains a special value called null pointer which is
invalid address(in practice 0 or negative number is used for null)
List also contains list pointer variable called start or name which contains
address of first node in list.
x
start Linked list with 3 nodes
![Page 3: NCS 301 DATA STRUCTURE USING C · NCS 301 Data Structure Using C 03-08-15 Subject Notes by Hammad Lari 2 Continued…. 03-08-2015 Another way of storing a list is to have each element](https://reader033.vdocument.in/reader033/viewer/2022042620/5f37f36460eef019772f6042/html5/thumbnails/3.jpg)
NCS 301 Data Structure Using C 03-08-15
Subject Notes by Hammad Lari 3
Representation of Linked Lists in memory Let LIST be a linked list. LIST requires two linear arrays (INFO & LINK).
List also requires a variable name such as START and nextpointer sentinel
NULL. We choose null=0.
Example:-
Refer page 5.4 from Data structures by Seymour Lipschutz(schaum)
Declaration for linear linked list in memory
Struct node
{
Int a ;
Struct node *next;
};
Typedef struct node NODE;
NODE *start;03-08-2015
Types of linked list1. Singly linked list-linear(we cannot access the predecessor of node from
current node)
2. Doubly ll-multiple links, accessing both predecessor and successor
3. Circular ll-no beginning and no end(sorting the address of very first node inlink field of last node)
4. Circular doubly ll-successor and predecessor pointer in circular manner.
Operations on Linked list
1. Creation
2. Insertion
3. Searching
4. Deletion
5. Traversing
6. Searching
7. Display 03-08-2015
![Page 4: NCS 301 DATA STRUCTURE USING C · NCS 301 Data Structure Using C 03-08-15 Subject Notes by Hammad Lari 2 Continued…. 03-08-2015 Another way of storing a list is to have each element](https://reader033.vdocument.in/reader033/viewer/2022042620/5f37f36460eef019772f6042/html5/thumbnails/4.jpg)
NCS 301 Data Structure Using C 03-08-15
Subject Notes by Hammad Lari 4
Singly Linked list In C a ll is created using structure ,pointers and dyanamic memory
allocation.
Struct node
{
Int num;
Struct node *ptr; //pointer to node
};
Typedef struct node NODE //type def making it adt
NODE *start; //pointer to node of ll
Start=(NODE*)malloc (sizeof(NODE));//dynamic memory allocation //when thisline is executed NODE is allocated and assigns HEAD as starting address ofNODE(head is the external pointer)
03-08-2015
num ptrstart
NODE
Singly Linked list
03-08-2015
num ptrstart
NODE
Now we can assign values
Start->number=30;
Start->ptr=‘\0’ //null pointer
Start->ptr=(NODE *) malloc(sizeof(NODE));
Start->ptr.num=100;
Start->ptr.ptr=‘\0’
![Page 5: NCS 301 DATA STRUCTURE USING C · NCS 301 Data Structure Using C 03-08-15 Subject Notes by Hammad Lari 2 Continued…. 03-08-2015 Another way of storing a list is to have each element](https://reader033.vdocument.in/reader033/viewer/2022042620/5f37f36460eef019772f6042/html5/thumbnails/5.jpg)
NCS 301 Data Structure Using C 03-08-15
Subject Notes by Hammad Lari 5
Inserting nodes in Singly linked list To insert an element following things should be done
1. Allocating a node
2. Assigning the data
3. Adjusting the pointers
Three instances of inserting
1. At the beginning
2. At the end
3. At specified position
03-08-2015
Insertion at the beginning
10
03-08-2015
302010
50403020
40 50
start
start
100
Node A
Node A
Before Insertion
After Insertion
New Nodeptr
![Page 6: NCS 301 DATA STRUCTURE USING C · NCS 301 Data Structure Using C 03-08-15 Subject Notes by Hammad Lari 2 Continued…. 03-08-2015 Another way of storing a list is to have each element](https://reader033.vdocument.in/reader033/viewer/2022042620/5f37f36460eef019772f6042/html5/thumbnails/6.jpg)
NCS 301 Data Structure Using C 03-08-15
Subject Notes by Hammad Lari 6
Algorithm-insertion at beginningInsert(start,item)
1. Initialize NODE *p
2. p=(NODE *)malloc(sizeof(NODE))
3. if(p==NULL)
print insertion was not possible and exit for program
4. else
1. If(start==null)
P->next=null;
2. Else
P->next=start;
3. Start=p;
03-08-2015
Insertion at the end
03-08-2015
start
start
100 x
Before Insertion
After Insertion
ptr
![Page 7: NCS 301 DATA STRUCTURE USING C · NCS 301 Data Structure Using C 03-08-15 Subject Notes by Hammad Lari 2 Continued…. 03-08-2015 Another way of storing a list is to have each element](https://reader033.vdocument.in/reader033/viewer/2022042620/5f37f36460eef019772f6042/html5/thumbnails/7.jpg)
NCS 301 Data Structure Using C 03-08-15
Subject Notes by Hammad Lari 7
Algorithm-insertion at the end
03-08-2015
Insert(start,item)
1. Initialize node *p,item
2. P=(NODE *)malloc(sizeof(NODE))
3. If(p==NULL)
Print NODE was not created and exit for program.
4. else
1. P->info=item
2. P->next=’\0’
3. If(start==NULL)
Start=p
4. Else
Temp=start
Repeat until (temp->next==’\0’)
Temp=temp->next
Temp->next=p
Insertion at the middle or specific location
03-08-2015
start
start
Node A Node B
Node A Node B
Before Insertion
After Insertion
![Page 8: NCS 301 DATA STRUCTURE USING C · NCS 301 Data Structure Using C 03-08-15 Subject Notes by Hammad Lari 2 Continued…. 03-08-2015 Another way of storing a list is to have each element](https://reader033.vdocument.in/reader033/viewer/2022042620/5f37f36460eef019772f6042/html5/thumbnails/8.jpg)
NCS 301 Data Structure Using C 03-08-15
Subject Notes by Hammad Lari 8
Algorithm-insertion at specific position
03-08-2015
1. Initialize NODE *p,temp,loc
2. p=(NODE *)malloc(sizeof(NODE))
3. If(p==NULL)
Print NODE is not created and exit from
program.
4. Else
1. p->info=item
2. if(start==NULL)
1. start=p
2. P->next=null
3. Else(initialize counter I and temp pointers)
1.temp=start
2.i=1
3.Repeat until i<loc
1.temp=temp->next
2.i++
4.p=temp->next
5.temp->next=p
Deleting first node
10
03-08-2015
3020
50403020
40 50
start
start
Before deletion
After deletion
ptr
![Page 9: NCS 301 DATA STRUCTURE USING C · NCS 301 Data Structure Using C 03-08-15 Subject Notes by Hammad Lari 2 Continued…. 03-08-2015 Another way of storing a list is to have each element](https://reader033.vdocument.in/reader033/viewer/2022042620/5f37f36460eef019772f6042/html5/thumbnails/9.jpg)
NCS 301 Data Structure Using C 03-08-15
Subject Notes by Hammad Lari 9
Algorithm-Delete from beginningdelete(start)
1. Check for underflow?
1. If start=null then
2. print no nodes
3. Exit
2. Set ptr=start
3. Set start=start->next
4. Print element deleted ptr->info
5. Free(ptr)
03-08-2015
Deleting last node
10
03-08-2015
3020
50 x403020
40 x
start
start
Before deletion
After deletion
ptr
10
loc
![Page 10: NCS 301 DATA STRUCTURE USING C · NCS 301 Data Structure Using C 03-08-15 Subject Notes by Hammad Lari 2 Continued…. 03-08-2015 Another way of storing a list is to have each element](https://reader033.vdocument.in/reader033/viewer/2022042620/5f37f36460eef019772f6042/html5/thumbnails/10.jpg)
NCS 301 Data Structure Using C 03-08-15
Subject Notes by Hammad Lari 10
Algorithm-deletion from enddelete(start)
1. Check for underflow?
1. If start=null then
2. print no nodes
3. Exit
2. If start->next=null then
1. Set ptr=start
2. Set start=null
3. Print element deleted is ptr->info
4. Free(ptr)
3. Set ptr=start
4. Repeat until ptr->next!=null
1. Set loc=ptr
2. Set ptr=ptr->next
5. Set loc->next=null
6. Free(ptr)03-08-2015
Deleting node from specific position
10
03-08-2015
4020
50 x403020
50 x
start
start
Before deletion
After deletion
10
![Page 11: NCS 301 DATA STRUCTURE USING C · NCS 301 Data Structure Using C 03-08-15 Subject Notes by Hammad Lari 2 Continued…. 03-08-2015 Another way of storing a list is to have each element](https://reader033.vdocument.in/reader033/viewer/2022042620/5f37f36460eef019772f6042/html5/thumbnails/11.jpg)
NCS 301 Data Structure Using C 03-08-15
Subject Notes by Hammad Lari 11
Algorithm-deletion from specific positiondelete(start)
1. Check for underflow?
1. If start=null then
2. print no nodes
3. Exit
2. Initialize counter, I and pointers
1. Node *temp, node *ptr;
2. Set i=0
3. Set ptr=start
3. Repeat steps 4 to 9 until i<=loc
4. Set temp=ptr
5. Set ptr=ptr->next
6. Set i=i+1
7. Print element deleted is
8. Set temp->next=ptr->new
9. Free(ptr) 03-08-2015
Circular Linked lists
03-08-2015
A circular linked list is a type of singly linked list in which the link field of the
last node contains the address of first node of the list.
List contains list pointer variable called start and last which contains address
of first node and last node in list.
x
start Linked list with 3 nodeslast
![Page 12: NCS 301 DATA STRUCTURE USING C · NCS 301 Data Structure Using C 03-08-15 Subject Notes by Hammad Lari 2 Continued…. 03-08-2015 Another way of storing a list is to have each element](https://reader033.vdocument.in/reader033/viewer/2022042620/5f37f36460eef019772f6042/html5/thumbnails/12.jpg)
NCS 301 Data Structure Using C 03-08-15
Subject Notes by Hammad Lari 12
Algorithm-insertion at beginning1. Initialize NODE *P
2. p=(NODE *)malloc(sizeof(NODE))
3. If(p==NULL)
1. Print insertion was not possible
4. Else
1. p->info=item
2. if(start==NULL)
1. Start=last=p
2. P->next=start
3. Else
1. p->next=start
2. start=p
3. last->next=p
03-08-2015
Algorithm-insertion at end1. Initialize NODE *p
2. p=(NODE*)malloc(sizeof(NODE))
3. If(p==NULL)
1. Print insertion was not possible.
4. Else
1. P->info=item
2. If(start==NULL)
1.start=last=p
2.p->next=start
3. Else
1.Last->next=p
2.Last=p
3.Last->next=start03-08-2015
![Page 13: NCS 301 DATA STRUCTURE USING C · NCS 301 Data Structure Using C 03-08-15 Subject Notes by Hammad Lari 2 Continued…. 03-08-2015 Another way of storing a list is to have each element](https://reader033.vdocument.in/reader033/viewer/2022042620/5f37f36460eef019772f6042/html5/thumbnails/13.jpg)
NCS 301 Data Structure Using C 03-08-15
Subject Notes by Hammad Lari 13
Algorithm-deletion from beginning1. Initialize NODE *p
2. if(start==NULL)
1. print 'UNDERFLOW'.
3. else
1. p=start
2. start=start->next
3. last->next=start
4. print p->info
5. free(p)
03-08-2015
Algorithm-deletion from end1. Initialize NODE *p,*temp.
2. if(start==NULL)
1. print 'UNDERFLOW'
3. else
1. p=start
2. repeat untill p->next !=start
1. temp=p
2. p=p->next
3. temp->next=start
4. last=temp
5. print p->info
6. free(p)
03-08-2015
![Page 14: NCS 301 DATA STRUCTURE USING C · NCS 301 Data Structure Using C 03-08-15 Subject Notes by Hammad Lari 2 Continued…. 03-08-2015 Another way of storing a list is to have each element](https://reader033.vdocument.in/reader033/viewer/2022042620/5f37f36460eef019772f6042/html5/thumbnails/14.jpg)
NCS 301 Data Structure Using C 03-08-15
Subject Notes by Hammad Lari 14
Doubly Linked lists
03-08-2015
Disadvantage of Singly linked list:-
1. Inability to traverse the list in the backward direction.
A doubly linked list provides bi directional traversing.
Each node in D.L has two fields.
List contains list pointer variable called start and last which contains address
of first node and last node in list.
x x
start Doubly Linked list with 3 nodeslast
prev data next
Structure –Doubly linked list
struct node
{
struct node *prev;
struct node *next;
char info;
}
typedef struct node NODE
NODE *start;
NODE *last;
03-08-2015
![Page 15: NCS 301 DATA STRUCTURE USING C · NCS 301 Data Structure Using C 03-08-15 Subject Notes by Hammad Lari 2 Continued…. 03-08-2015 Another way of storing a list is to have each element](https://reader033.vdocument.in/reader033/viewer/2022042620/5f37f36460eef019772f6042/html5/thumbnails/15.jpg)
NCS 301 Data Structure Using C 03-08-15
Subject Notes by Hammad Lari 15
Algorithm-insertion at beginning1. Initialize NODE *p
2. p=(NODE *)malloc(sizeof(NODE))
3. p->info=item
4. p->prev=NULL
5. if(start==NULL)
1. start=last=p
2. p->next=NULL
6. else
1. p->next=start
2. start->prev=p
3. start=p
03-08-2015
Algorithm-insertion at end1. Initialize NODE *p.
2. p=(NODE *)malloc(sizeof(NODE))
3. p->info= item
4. p->next=NULL
5. if(start==NULL)
1. start=last=p
2. p->prev=NULL
6. else
1. last->next=p
2. p->prev=last
3. last=p
03-08-2015
![Page 16: NCS 301 DATA STRUCTURE USING C · NCS 301 Data Structure Using C 03-08-15 Subject Notes by Hammad Lari 2 Continued…. 03-08-2015 Another way of storing a list is to have each element](https://reader033.vdocument.in/reader033/viewer/2022042620/5f37f36460eef019772f6042/html5/thumbnails/16.jpg)
NCS 301 Data Structure Using C 03-08-15
Subject Notes by Hammad Lari 16
Algorithm-deletion from beginning1. Initialize *p.
2. if(start==NULL)
1. print ''UNDERFLOW''
3. else
1. p=start
2. start=start->next
3. start->prev=NULL
4. print p->info
5. free(p)
03-08-2015
Algorithm-deletion from end1. Initialize *p.
2. if(start==NULL)
1. print "UNDERFLOW"
3. else
1. If(last->prev==null)
1. P=last
2. Last->last->prev
3. Last->next=null
2. print p->info
3. free(p)
03-08-2015
![Page 17: NCS 301 DATA STRUCTURE USING C · NCS 301 Data Structure Using C 03-08-15 Subject Notes by Hammad Lari 2 Continued…. 03-08-2015 Another way of storing a list is to have each element](https://reader033.vdocument.in/reader033/viewer/2022042620/5f37f36460eef019772f6042/html5/thumbnails/17.jpg)
NCS 301 Data Structure Using C 03-08-15
Subject Notes by Hammad Lari 17
Class Website
www.ncs301ds.wordpress.com
03-08-2015