0% found this document useful (0 votes)
28 views67 pages

Linked List - DSA

The document describes the examination scheme and syllabus for a Data Structures course with C Programming. The examination includes class and lab continuous assessments, as well as a term end exam. The syllabus covers topics like linear and nonlinear data structures, algorithms, asymptotic analysis, arrays, linked lists, stacks, queues, and trees. It provides details on various concepts related to linked lists, including their representation and operations. Linked lists allow flexible insertion and deletion by linking nodes using pointers.

Uploaded by

Sneha Mehta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views67 pages

Linked List - DSA

The document describes the examination scheme and syllabus for a Data Structures course with C Programming. The examination includes class and lab continuous assessments, as well as a term end exam. The syllabus covers topics like linear and nonlinear data structures, algorithms, asymptotic analysis, arrays, linked lists, stacks, queues, and trees. It provides details on various concepts related to linked lists, including their representation and operations. Linked lists allow flexible insertion and deletion by linking nodes using pointers.

Uploaded by

Sneha Mehta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

Data Structures-I with C Programming

S. Y. B. Tech CSE Trimester – IV

SCHOOL OF COMPUTER ENGINEERING AND TECHNOLOGY

DATA STRUCTURES -I WITH C


1
PROGRAMMING
Data Structures-I with C-
Programming

Examination scheme:
Marks-50 [Class Continuous Assessment]
Marks-50 [lab Continuous Assessment] Marks-50[Term End Exam]

Class Continuous Assessment (CCA)- 50 Marks

Assignments 1 Active Learning 1 Mid-term Poster


Presentations
20% 30% 30% 20%
(10) (15) (15) (10)

Laboratory Continuous Assessment (LCA)-50 Marks

Practical Oral based Site Visit Mini Problem Any other


on practical Project based (Mock )
Learning
60% - - - - 40%
( 30) (20)

DATA STRUCTURES -I WITH C PROGRAMMING 2


Data Structures-I with C-
Programming
Examination scheme:
Marks-50 [Class Continuous Assessment]
Marks-50 [lab Continuous Assessment] Marks-50[Term End Exam]
Course Objectives:
1. To understand the need of data structure, concept of abstract data type.
2. To familiarize with the structural constraints and usage of the data.
3. To study the use of data structures in solving real life problems.
4. To study different searching and sorting techniques for solving complex data related
problems.
Course Outcomes: (After Completion of course Student should be able to:-)
1. To develop skills for writing and analyzing algorithms to solve domain problems.
2. To compare and contrast different linear data structures and identify appropriate
usage.
3. To analyze different searching and sorting algorithms so as to understand their
applications.
4. To demonstrate the use of sequential data structures.
DATA STRUCTURES -I WITH C PROGRAMMING 3
Syllabus
UNIT – I Introduction to Data Structures:
• Data, Data Objects, Abstract Data types (ADT) and Data Structure, Types of data structure(Linear
and Non linear, Static and dynamic)
• Introduction to algorithms, Algorithm design tools: Pseudo code and flowchart,
• Analysis of Algorithms- Space complexity, Time complexity, Asymptotic notations-Big-O, Theta
and Omega, finding complexity using step count method, Analysis of programming constructs-
Linear, Quadratic, Cubic, Logarithmic

UNIT – II Linear Data Structures:


• Array as an Abstract Data Type, Sequential Organization, Memory Representation and Address
Calculation.
• Representation of Polynomials using arrays, addition and evaluation of Polynomials, Representation
of sparse matrix, Addition, Simple Transpose and Fast transpose of sparse matrix
• Searching: Linear search, Binary search.
• Sorting: Types of sorting-Internal and External sorting, Sorting methods- Bubble sort, Insertion sort,
Selection sort, Merge Sort, Comparison and analysis of sorting methods.

DATA STRUCTURES -I WITH C PROGRAMMING 4


Syllabus
UNIT – III
• Stacks: Stack as an Abstract Data Type, Representation of Stack Using Sequential Organization,
Applications of Stack- Expression Conversion and Evaluation, Recursion.
• Queues: Queue as Abstract Data Type, Representation of Queue Using Sequential Organization,
Circular Queue, Advantages of Circular queues, Deque-Basic concept, types(Input restricted and
Output restricted), Application of Queue : Job scheduling.

UNIT – IV
• Linked List: Linked List as an Abstract Data Type, Representation of Linked List Using Sequential
Organization, Representation of Linked List Using Dynamic Organization, Operations on Linked
List, Polynomial operations using linked list.
• Circular Linked List, Doubly Linked List , Generalized Linked List (GLL)
• Case Study : Garbage Collection

DATA STRUCTURES -I WITH C PROGRAMMING 5


Linked List
Topics to be Covered
• Linked List as an ADT

• Representation of Linked List Using Sequential Organization

• Representation of Linked List Using Dynamic Organization

• Operations on Linked List

• Polynomial operations using linked list

• Circular Linked List, Doubly Linked List , Generalized Linked List (GLL)

Case Study : Garbage Collection

DATA STRUCTURES -I WITH C PROGRAMMING 6


Types of Data Structures

DATA STRUCTURE

LINEAR
NONLINEAR

ARRAY
LINKLIST STACK
QUEUE TREES & GRAPH

DATA STRUCTURES -I WITH C


7
PROGRAMMING
Linked List ADT
structure Linked List (item)
declare CREATE() -> Linked list
insert(item, linked list) -> linked list
delete(linked list) -> linked list
ISEMPS(linked list) -> boolean;
for all L ∈ Linked list, i ∈ item let
ISEMPS(CREATE) ::= true
ISEMPS(insert(i,L)) ::= false
delete(CREATE) ::= error
delete(insert(I,L)) ::= L
end Linked List

DATA STRUCTURES -I WITH C


8
PROGRAMMING
Introduction to linked list
• Representation of simple data structures using an array and a sequential
mapping are studied.

• These representations had the property that successive nodes of the data
object were stored fixed distance apart.

• If the element aij of a table was stored at location Lij, then a i, j+1 was at the
location L ij + c for some constant c;

(BAT, CAT, EAT, FAT, HAT, JAT, LAT, MAT, OAT, PAT, RAT, SAT, TAT, VAT,
WAT)

• Disadvantage of a sequential mapping for ordered lists


– insertion and deletion of arbitrary elements become expensive.(Add
word GAT will require shifting)

DATA STRUCTURES -I WITH C


9
PROGRAMMING
contd…
• Solution to this problem of data movement in sequential representations
is achieved by using linked representations .

• Unlike a sequential representation where successive items of a list are


located a fixed distance apart, in a linked representation these items may
be placed anywhere in memory.

• Another way of saying this is that in a sequential representation the order


of elements is the same as in the ordered list, while in a linked
representation these two sequences need not be the same.

DATA STRUCTURES -I WITH C


10
PROGRAMMING
contd…
• To access elements in the list in the correct order, with each element we
store the address or location of the next element in that list.

• Thus, associated with each data item in a linked representation is a pointer


to the next element.

• This pointer is often referred to as a link.

• In general, a node is a collection of data, DATA1, ..., DATAn and links


LINK1, ...,LINKm. Each item in a node is called a field. A field contains
either a data item or a link.

DATA STRUCTURES -I WITH C


11
PROGRAMMING
Linked lists
• Linked lists are appropriate when the number of data elements
to be represented in the data structure at once is unpredictable.

• Linked lists are dynamic, so the length of a list can increase or


decrease as necessary.

• Each node does not necessarily follow the previous one


physically in the memory.

• Linked lists can be maintained in sorted order by inserting or


deleting an element at the proper point in the list.

DATA STRUCTURES -I WITH C


12
PROGRAMMING
What is Linked List?
• A linked list is an ordered collection of finite homogeneous data
elements called nodes where the linear order is maintained by means
of links or pointers.

Pointer to the
first node

Info/Data Link Field/


field Address Field

DATA STRUCTURES -I WITH C


13
PROGRAMMING
Anatomy of a linked list
• A linked list consists of:
– A sequence of nodes

myList

a b c d

Each node contains a value


and a link (pointer or reference) to some other node
The last node contains a null link
The list may (or may not) have a header

DATA STRUCTURES -I WITH C


14
PROGRAMMING
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)

DATA STRUCTURES -I WITH C


15
PROGRAMMING
Realization of Linked List Using Arrays

• Let L ={Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec}

• L is an ordered set

• List is stored in Data -1D array

• Elements are not stored in the same order as in the set L.

• To maintain the sequence ,second array ,Link is added


• List starts at 10th Loc

DATA STRUCTURES -I WITH C


16
PROGRAMMING
Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec

Data Index Link


Jun 1 4
Sep 2 7
Feb 3 8
Jul 4 12
5
Dec 6 -1
Oct 7 14
Mar 8 9
Apr 9 11
Head
Jan 10 3
May 11 1
Aug 12 2
13
Nov 14 6
DATA STRUCTURES -I WITH C
17
PROGRAMMING
15
Dynamic memory management

• In which memory resources are allocated whenever they are


needed, and freed whenever they are not necessary anymore

• Linked lists, are defined precisely in such a way as to allow for


dynamically allocating and deallocating memory, depending
on the requirements of the application.

DATA STRUCTURES -I WITH C


18
PROGRAMMING
Pointers in C

• Declare Pointer P of type integer: Int *P;

• Allocate memory and assign its address to


P:
P = (int *) malloc ( sizeof ( int ) );
• Store value 10 in the allocated memory:
P 10
*P = 10;

Notes:
1. The new allocated memory is anonymous ( i.e. does not
have a name of its own
2. The only way we can access this location is via P.
DATA STRUCTURES -I WITH C
19
PROGRAMMING
Pointers in C – Dynamic Memory Allocation

• But what does this complicated line do?:


P = (int *) malloc ( sizeof ( int ) ); Read This First
!!!

int Sizeof( int ): 1


But, what is this void * 3 A function that takes a primitive
in front of malloc? data type like int, float, char, etc.
Well, we do not know and just return its size in bytes!
what type of address to
return until we use void * malloc( int ): 2
malloc in our programs!! A function that takes a number
So, that’s why we cast of bytes, and dynamically allocate
void * to int * in this memory of that size in bytes,
example, since P is and finally returns its address!
declared of type int* . (in this case to P)

DATA STRUCTURES -I WITH C


20
PROGRAMMING
Types of linked lists

• The number of pointers are maintained depending on the


requirements and usage

• Based on this ,linked list are classified into three categories.


– Singly linked lists
– Circular linked lists
– Doubly linked lists

DATA STRUCTURES -I WITH C


21
PROGRAMMING
Empty List
• Empty Linked list is a single pointer having the
value of NULL.

head = NULL;

head

DATA STRUCTURES -I WITH C


22
PROGRAMMING
Singly Linked List
A linked list in which each node contains only one link
field pointing to the next node in the list

--- 05 60 99

Head Node First data


Node

First node is called the header node where no data


element is stored, but the link field holds the address of
the node containing very first data element.

DATA STRUCTURES -I WITH C


23
PROGRAMMING
Doubly Linked List
•A doubly linked list is a linear data structure in which each
node can have any number of data fields and two link fields
which are used to point to the previous node and the next
node.

--- 05 60 99

Head Node First data


Node

DATA STRUCTURES -I WITH C


24
PROGRAMMING
Circular Linked List
A linked list where the last node points to the header node
is called a circular linked list.
There are no NULL links.

--- 05 60 99

Head Node First data


Node

DATA STRUCTURES -I WITH C


25
PROGRAMMING
Basic Operations on a list

• Creating a List
• List Traversal
• Inserting an element in a list
• Deleting an element from a list
• Searching a list
• Reversing a list
• Merging two linked lists into a larger list
DATA STRUCTURES -I WITH C
26
PROGRAMMING
Data Structure of Node

struct node
{
int data; data Next
(Address of
struct node *next; next node)
};
struct node *head;

DATA STRUCTURES -I WITH C


27
PROGRAMMING
Creation of SLL
head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;

//Creation of Link List

Algorithm create(*H)
{
temp=H;
repeat untill choice =‘y’
{
allocate memory to curr;
accept curr->data;
curr->next=NULL;
temp->next=curr;
temp=curr; //temp=temp->next
Read choice;
}
}
DATA STRUCTURES -I WITH C
28
PROGRAMMING
Display Link List Finding length of Link List

Algorithm display(*H) Algorithm len(*H)


{ {
if H->next ==NULL curr=H->next;
Print “ list is empty “ while(curr!=NULL)
else {
{ i++;
//print head node values curr=curr->next;
curr=H->next; }
while(curr!=NULL) return(i);
{ }
Print curr,curr->data,curr->next;
curr=curr->next;
}
}
DATA STRUCTURES -I WITH C
29
PROGRAMMING
Insertion in a linked list
a b
… …
4. 3.
curr
2.Accept
nnode Data

1.Allocate memory

1.Allocate memory for nnode ;


2.Accept data for nnode;
3.nnode->next = curr->next;
4.curr->next = nnode;

DATA STRUCTURES -I WITH C


30
PROGRAMMING
Insert a new node by position
Algorithm Insertbypos(*H)
{
i=1 ; curr=H;
//allocate memory for nnode node;
read nnode->data and pos; //accept data & position to be inserted:
k=len();
If(pos>k+1)
//Print “Data can't be inserted”;
else
{
while(curr!=NULL && i<pos)
{
i++;
curr=curr->next;
}
nnode->next=curr->next;
curr->next=nnode;
} DATA STRUCTURES -I WITH C
31
PROGRAMMING
Deletion from a linked list

… a x b

deletedNode
current

Node *deletedNode = current->next;


current->next = current->next->next;
delete deletedNode;

DATA STRUCTURES -I WITH C


32
PROGRAMMING
Delete a node by position
Algorithm delpos(*H) while(ctr<pos && curr!=NULL)
{ {
prev=H; ctr=1; ctr++;
curr=H->next; prev=curr;
read pos; curr=curr->next;
//Accept position of data to be deleted: }
temp=curr;
k=len(); prev->next=curr->next;
if (k<pos) curr->next=NULL;
//display Data can't be deleted; free(temp);
else }
{ }

DATA STRUCTURES -I WITH C


33
PROGRAMMING
Reverse of Linked List(Using
Pointers)
Algorithm reverse(*H)
{
prev=NULL;
curr=head->next;
while(curr!=NULL)
{
future=curr->next;
curr->next=prev;
prev=curr;
curr=future;
}
head->next=prev;
}
DATA STRUCTURES -I WITH C
34
PROGRAMMING
Sorting of SLL by using pointers
Algorithm sort(*H) if(curr->data > temp->data
{ {
len=len(H); prev->next=temp;
curr->next=temp->next;
for i=1 to len-1
temp->next=curr;
{
prev=temp;
prev=H; }
curr=H->next; else
for j=0 to <l-i {
{ prev=curr;
temp=curr->next; curr=curr->next;
}
} //end for inner for
} //end for outer for
} //end Algorithm
DATA STRUCTURES -I WITH C
35
PROGRAMMING
Merging of SLL by using pointers
Algorithm merge(*H1,*H2) else
{ {
curr1=H1->next; temp->next=curr2;
curr2=H2->next; temp=curr2;
if(curr1->data<curr2->data) curr2=curr2->next;
{ }
temp=head1; }
flag=1; if(curr1==NULL)
} temp->next=curr2;
else if(curr2==NULL)
{ temp->next=curr1;
temp=head2; if(flag==1)
flag=0; display(head1);
} else
while(curr1!=NULL && curr2!=NULL) display(head2);
{ }
if(curr1->data<curr2->data)
{
temp->next=curr1;
temp=curr1;
curr1=curr1->next;
}
DATA STRUCTURES -I WITH C
36
PROGRAMMING
Arrays Vs Linked Lists
Arrays Linked list

Fixed size: Resizing is expensive Dynamic size

Insertions and Deletions are inefficient: Insertions and Deletions are efficient: No
Elements are usually shifted shifting

Random access i.e., efficient indexing No random access


 Not suitable for operations requiring
accessing elements by index such as sorting

No memory waste if the array is full or almost Since memory is allocated dynamically(acc. to
full; otherwise may result in much memory our need) there is no waste of memory.
waste.

Sequential access is faster [Reason: Elements in Sequential access is slow [Reason: Elements not
contiguous memory locations] in contiguous memory locations]

DATA STRUCTURES -I WITH C


37
PROGRAMMING
Doubly-Linked Lists
 It is a way of going both directions in a linked list, forward and reverse.
 Many applications require a quick access to the predecessor node of
some node in list.
A node in a doubly-linked list store two references:
•A next link; that points to the next node in the list, and
•A prev link; that points to the previous node in the list.
Advantages:
Convenient to traverse the list backwards.
Simplifies insertion and deletion because you no longer have to refer to the
previous node.
Disadvantage:
Increase in space requirements.
prev next

elem node
DATA STRUCTURES -I WITH C
38
PROGRAMMING
Doubly Link List creation
Algorithm Create(*H)
{
struct node temp=H;
{ repeat till choice =y
char data[20]; {
node *next,*prev; //allocate memory for new node
}; temp->next=curr;
curr->prev=temp;
curr->next=NULL;
temp=curr;
}
Read choice;
}

DATA STRUCTURES -I WITH C


39
PROGRAMMING
Insertion
head current

newNode = new Node(x);


newNode->prev = current; newNode
newNode->next = current->next;
newNode->prev->next = newNode;
newNode->next->prev = newNode;
current = newNode;

DATA STRUCTURES -I WITH C


40
PROGRAMMING
Deletion
head current

oldNode = current;
oldNode->prev->next = oldNode->next;
oldNode->next->prev = oldNode->prev;
delete oldNode;
current = head;

DATA STRUCTURES -I WITH C


41
PROGRAMMING
Circular Linked Lists

DATA STRUCTURES -I WITH C


42
PROGRAMMING
Allocation of memory for head node using constructor

head=(struct node *)malloc(sizeof(struct node));


head->next=head;

//Creation of Link List


Algorithm create(*H)
{ temp=H;
repeat untill choice =‘y’ {
allocate memory to curr;
accept curr->data;
curr->next=H;
temp->next=curr;
temp=curr; //temp=temp->next
Read choice;
}
} DATA STRUCTURES -I WITH C
43
PROGRAMMING
Advantages over Singly-linked Lists
• Quick update operations:
such as: insertions, deletions at both ends (head
and tail), and also at the middle of the list.
• A node in a doubly-linked list store two
references:
• A next link; that points to the next node in the list,
and
• A prev link; that points to the previous node in the
list.

DATA STRUCTURES -I WITH C


44
PROGRAMMING
DLLs compared to SLLs
• Advantages: • Disadvantages:
– Can be traversed in either – Requires more space
direction (may be – List manipulations are
essential for some slower (because more
applications) links must be changed)
– Some operations, such as – Greater chance of having
deletion and inserting bugs (because more links
before a node, become must be manipulated)
easier

DATA STRUCTURES -I WITH C


45
PROGRAMMING
• Polynomial
•Array Implementation:
• p1(x) = 8x3 + 3x2 + 2x + 6
• p2(x) = 23x4 + 18x - 3

p1(x) p2(x)

6 2 3 8 -3 18 0 0 23
0 2 0 2 4

Index
represents
exponents
DATA STRUCTURES -I WITH C
46
PROGRAMMING
• Polynomial (continued)

•This is why arrays aren’t good to represent


polynomials:

• p3(x) = 16x21 - 3x5 + 2x + 6

6 2 0 0 -3 0 ………… 0 16

WASTE OF SPACE!

DATA STRUCTURES -I WITH C


47
PROGRAMMING
• Polynomial (continued)

• Advantages of using an Array:

• only good for non-sparse polynomials.


• ease of storage and retrieval.

• Disadvantages of using an Array:

• have to allocate array size ahead of time.


• huge array size required for sparse polynomials.
Waste of space and runtime.

DATA STRUCTURES -I WITH C


48
PROGRAMMING
• Polynomial (continued)

• Linked list Implementation:

• p1(x) = 23x9 + 18x7 + 41x6 + 163x4 + 3


• p2(x) = 4x6 + 10x4 + 12x + 8

P1 23 9 18 7 41 6 163 4 3 0

TAIL (contains pointer)

P2 4 6 10 4 12 1 8 0

NODE (contains coefficient & exponent)


DATA STRUCTURES -I WITH C
49
PROGRAMMING
Revisit Polynomials

coeff exp
a.first 3 14 2 8 1 0 0

a  3 x14  2 x 8  1

b.first 8 14 -3 10 10 6 **

b  8 x14  3 x10  10 x 6
DATA STRUCTURES -I WITH C
50
PROGRAMMING
Node structure of Polynomial
struct polyNode
{
// All members in “struct” are public
int coef; // coefficient
int exp; // exponent
struct polyNode *next;
};

DATA STRUCTURES -I WITH C


51
PROGRAMMING
Addition of Two Polynomials (1)
• It is an easy way to represent a polynomial by
a linked list.
• Example of adding two polynomials a and b

a.first 3 14 2 8 1 0 0

b.first 8 14 -3 10 10 6 0

q
c.first 11 14 0 (i) p->exp == q->exp

DATA STRUCTURES -I WITH C


52
PROGRAMMING
Addition of Two Polynomials (2)

a.first 3 14 2 8 1 0 0

b.first 8 14 -3 10 10 6 0

c.first 11 14 -3 10 0

(ii) p->exp < q->exp

DATA STRUCTURES -I WITH C


53
PROGRAMMING
Addition of Two Polynomials (3)

a.first 3 14 2 8 1 0 0

b.first 8 14 -3 10 10 6 0

q
c.first 11 14 -3 10 2 8 0

(iii) p->exp > q->exp

DATA STRUCTURES -I WITH C


4-54
PROGRAMMING
• Polynomial (continued)

• Adding polynomials using a Linked list representation: (storing the result in p3)
To do this, we have to break the process down to cases:

• Case 1: exponent of p1 > exponent of p2


• Copy node of p1 to end of p3.
[go to next node]

• Case 2: exponent of p1 < exponent of p2


• Copy node of p2 to end of p3.
[go to next node]

•Case 3: exponent of p1 = exponent of p2


• Create a new node in p3 with the same exponent and with the sum of the
coefficients of p1 and p2.

DATA STRUCTURES -I WITH C


55
PROGRAMMING
Addition of Polynomial
• Allocate the memory space for head;
• Head->exp=-1

Algorithm add(*H1,*H2)
{
Allocate a memory for H3;
head3->exp=-1;
t3=H3;
t1=H1->next;
t2=H2->next;
while(t1->exp!=-1||t2->exp!=-1)
{
if(t1->exp==t2->exp)
{
Allocate the memory for temp;
Add t1 coeff and t2 coeff in t3 coeff
copy one of the exponent in t3 exp
t3->next=temp;
temp->next=head3;
t3=temp;
Move t1 to next node ;
Move t2 to next node
}
DATA STRUCTURES -I WITH C
56
PROGRAMMING
Addition of Polynomial
else
if exponent of p1 < exponent of p2
Copy node of p2 to end of p3.
else //exponent of p1 > exponent of p2
Copy node of p1 to end of p3

}//end of while

} //end algorithm

DATA STRUCTURES -I WITH C


57
PROGRAMMING
Evaluation of Polynomial
Algorithm eval(*H, x)
{
cur=H->next;
while not end of list
{
using value of x find the sum of terms;
move cur to next node ;
}
print sum;
}
DATA STRUCTURES -I WITH C
58
PROGRAMMING
Generalized Lists
• A generalized list, A, is a finite sequence of n ≥ 0 elements,
a0, a1, a2, …, an-1, where ai, is either an atom or a list. The
elements ai,0 ≤ i ≤ n – 1, that are not atoms are said to be
the sublists of A.

• A list A is written as A = (a0, …, an-1 ), and the length of


the list is n.

• A list name is represented by a capital letter and an atom is


represented by a lowercase letter.

• a0 is the head of list A and the rest (a1, …, an-1) is the tail
of list A.

DATA STRUCTURES -I WITH C


59
PROGRAMMING
Examples of Generalized Lists
• A = ( ): the null, or empty, list; its length is zero.

• B = (a, (b, c)): a list of length two; its first element is the atom a,
and its second element is the linear list (b, c).

• C = (B, B, ( )): A list of length three whose first two elements


are the list B, and the third element is the null list.

• D = (a, D): is a recursive list of length two; D corresponds to the


infinite list D = (a, (a, (a, …))).

• head(B) = ‘a’ and tail(B) = (b, c), head(tail(C))=B and tail(tail(C))


= ( ).

• Lists may be shared by other lists.

• Lists may be recursive.


DATA STRUCTURES -I WITH C
60
PROGRAMMING
• It is a little surprising that every generalized
list can be represented using the node
structure
Tag=0/1 Data Link
• Tag=0 means data
• Tag = 1 means pointer to the sublist

DATA STRUCTURES -I WITH C


61
PROGRAMMING
Representations of Generalized Lists

A=() Empty list


B=(a, (b, c))
B 0 a 1 0

0 b 0 c 0

C 1 1 1 0 0 C=(B, B, ( ))

D 0 a 1 0 D=(a, D)
**
DATA STRUCTURES -I WITH C
62
PROGRAMMING
General Polynomial
p ( x, y, z )  x10 y 3 z 2  2 x 8 y 3 z 2  3 x 8 y 2 z 2  x 4 y 4 z  6 x 3 y 4 z  2 yz
• P(x, y, z)=
(( x10  2 x 8 ) y 3  3 x 8 y 2 ) z 2  (( x 4  6 x 3 ) y 4  2 y ) z
• Rewritten as Cz2 + Dz, where C and D are
polynomials.
• Again, in C, it is of the form Ey3 + Fy2, where E and
F are polynomials.
• In general, every polynomial consists of a variable
plus coefficient-exponent pairs. Each coefficient
may be a constant or a polynomial.

DATA STRUCTURES -I WITH C


63
PROGRAMMING
• Suppose we need to devise a data representation for them and
consider one typical example, the polynomial P(x,y,z) =

• One can easily think of a sequential representation for P, say


using nodes with four fields: COEF, EXPX, EXPY, and EXPZ.
• But this would mean that polynomials in a different number of
variables would need a different number of fields, adding
another conceptual inelegance to other difficulties.
• These nodes would have to vary in size depending on the
number of variables, causing difficulties in storage
management.
• The idea of using a general list structure with fixed size nodes
arises naturally if we consider re-writing
• P(x,y,z) as
DATA STRUCTURES -I WITH C
64
PROGRAMMING
Linked List-based Stack
Implementation
typedef struct pnode
{
pnode * next;
PUSH pnode
int item;
}
23 45 67 78 NULL

TOP
Stack implementation using Linked list includes
POP
 Inserting Element from top
 Deleting Element from top

DATA STRUCTURES -I WITH C


65
PROGRAMMING
Garbage Collection
• After series of memory allocation and deallocation, there
are blocks of free memory scattered throughout the
available heap space

• To be able to reuse this memory, the memory allocator


will usually link the freed blocks together in a free list by
writing pointers to the next free block.

• Components of os ,memory management module,


maintain this list.

DATA STRUCTURES -I WITH C


66
PROGRAMMING
Garbage collection contd..
• Os periodically collects all the free blocks and inserts into
the free pool.

• This is called as garbage collection.

• When CPU is idle, the garbage collection starts.

• It is invisible to programmer.

DATA STRUCTURES -I WITH C


67
PROGRAMMING

You might also like