ADS Unit III 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 68

Notes On Algorithms and Data Structures

Algorithms and Data Structures: Unit-III


Fundamentals, Types: Singly, Doubly, Circular , Linked Stacks and Queues, Examples on linked list, Circular linked list, doubly
linked list and Dynamic storage Management: Garbage Collection, Compaction, and Applications of Linked List: Operations on
Polynomials, Generalized Linked List..

1. Pointers in C Programming
Each memory cell (or Byte) in the computer is associated with a unique number called an address that can be used to
access that location. The picture below represents several bytes of a computer's memory. In the picture, addresses FFD1
thru FFDD are shown.

Memory
cell

F F F F F F F F F F F F F
F F F F F F F F F F F F F
D D D D D D D D D D D D D Address
1 2 3 4 5 6 7 8 9 A B C D
Whenever a variable is defined in a program, somewhere in the memory sufficient numbers of memory cell (byte) are
assigned to it to store the value of the variable. During the lifetime of the variable, the address is not changed but the
value may change. Consider the statement
int max = 456;
Let us assume that addresses of two bytes (since data type of max is int) of memory assigned to max be FFD3 and
FFD4.

max Variable Name

456 Value or data

FFD3 Address

There are two types of variables in C.


1. Variable that stores a data
2. Variable that stores address
Variable that stores address is known as pointer variable or simply a pointer. In C a pointer is a variable that points to or
references a memory location in which data is stored. The term pointee is used for the thing that the pointer points to. A
pointer can access and change the pointee via the reference.

ip FFD3 456 max ip 456 max


FFD3 Pointer Pointee

Note: the data type of all pointer variables is fix (as is stored address and address is an integer value) but they points to
different data type. A pointer is definitely NOT an integer.

void main()
{
int *ip;
float *fp;
double *dp;
printf("ip=%d\tfp=%d\tdp=%d\n",sizeof(ip),sizeof(fp),sizeof(dp));
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

Output:
ip=4 fp=4 dp=4

Note: To differentiate data and address we are using hex numbers to represent address and decimal numbers to represent
data.

There are three important steps to deal with pointer:


1. How to declare pointer
2. How to get address and assign address to pointer
3. How to access data stored in memory location whose address is in pointer.

1.2 Declaration
A pointer is a derived data type in C. Pointer must be declared before it is use. Declarations begins with data type
followed by indirection or dereference operator (*) followed by pointer variable name.
Syntax:
Data_type * pointer_variable;
The reason for associating data type to pointer is to know to what type of data it points to.
Example:
int *ip;
Here pointer ip points to integer data. The dereference operator looks up the value that exists at an address. Thus,
“pointer variable” really means “variable of a pointer type”.
The pointer itself is not of that type, it is of type pointer to that type. A given pointer only points to one particular
type, not to all possible types.
In pointer declaration, each pointer variable must be prefix by dereference operator
int *ip, *iq, *ir;
float *fa, *fb;
The convention is to place the * closer to pointer variable rather than the data type.
int* ip;
is same as
int *ip;
Also declaration
int* ip, a, b;
does not mean that ip, a and b are pointer variables rather its actual meaning is ip is pointer variable and a & b are
non-pointer variables (int *ip, a, b).
Compiler always associate the * with pointer variable rather then data type thus allowing us to declare pointer variables
and non-pointer variables in a single declaration.
int *ip, a, x, *iq;
in above declaration ip & iq are pointer variables and a & b are non-pointer variables.

1.3 Assigning Address to Pointer


void main()
{
int *ip;
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

float *fp;

printf("&ip=%X ip=%X\n", &ip,ip);


ip
printf("&fp=%X fp=%X\n", &fp,fp); ? ?
} Contains Points to
Output: Garbage unknown location
&ip=FFF4 ip=8F53 fp
? ?
&fp=FFF2 fp=3C6

The addresses shown in output for in ip and fp are some random (garbage) addresses.
When a pointer is declared but not initialized to any address it always contains some random address. It is necessary that
pointer must point to some meaningful memory location prior to its use. The pointer when initialized is said to reference
another variable, or to contain of a memory location. Allocating a pointer and allocating a pointee for it to point to be two
separate steps.

Note: Don’t try to assign manually some integer value to pointer as address is an integer value. It is always better to use
some mechanism to get an address of a memory location.
There are two ways to get address of memory location
1. By using address operator (&) to get the address of a variable
2. By requesting operating system to get a block of memory

1.3.1 Assignment using Address Operator (&)


The actual location of a variable in the memory is system dependent and therefore, the address of variable is not known to
us immediately. The & operator preceding a variable returns the address of the operand (i.e. variable) associated with it.
void main()
{
int x=55;
float y= 34.78;

printf("&x=%X x=%d\n", &x,x);


printf("&y=%X y=%g\n", &y,y);

}
Output:
&x=FFF4 x=55
&y=FFF0 y=34.78

Pointer variable can be initialised by using address of variable which is already declared.
void main()
{
int x=55;
float y= 34.78;

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

int *ip;
float *fp;

/* assigning address to pointer */


ip=&x;
fp=&y;

printf("&x=%X x=%d\n", &x,x);


printf("&y=%X y=%g\n", &y,y);

printf("&ip=%X ip=%X *ip=%d\n", &ip,ip,*ip);


printf("&fp=%X fp=%X *fp=%g\n", &fp,fp,*fp);
}
ip
FFF4 55
Output: &x x
&x=FFF4 x=55
&y=FFF0 y=34.78 fp
FFF0 34.78
&ip=FFEE ip=FFF4 *ip=55 &y y
&fp=FFEC fp=FFF0 *fp=34.78

It may be observed that the hexadecimal addresses shown in output are in decreasing order, supporting the fact that all
automatic variables are created in the program’s stack area and the stack always grows from a higher address to lower
one.
Pointers of different types are not the same. There are no implicit conversions from one to the other. We must associate a
pointer to a particular data type. Let us consider following code
1: int *ip;
2: float x=12.78;
3: ip=&x;
At the time of compilation this code gives an error in line no 3 that says the compiler can not convert (float *) to
(int *). Here code is trying to coerce ip to point to some float data but ip is a pointer to integer data. Basically ip
is a pointer variable hence it can take any address but in real sense, “pointer variable” is a “variable of a pointer type” thus
implicit assignment of address of data type other than data type of pointer results into error. One can use cast operator for
explicit assignment.
Example: ip=(int *)&x.
C is not too fussy about assigning values of different type (not strongly type checked).

1.3.2 Assignment using memory allocation functions


The process of allocating memory at run time is known as dynamic memory allocation. C inherently does not have facility
for dynamic memory management but it has memory management functions, which can be used to allocate and free
memory during the program execution. The following functions are used in c for the purpose of memory management.
Function Task
malloc Allocates memory requests size of bytes and returns a pointer to the
1st byte of allocated space
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

calloc Allocates space for an array of elements initializes them to zero and
returns a pointer to the memory
free Frees previously allocated space
realloc Modifies the size of previously allocated space.

A block of memory may be allocated using the function malloc. It takes the following form:
#include <stdlib.h>

void *malloc(size_t size);

The malloc function reserves a block of memory of specified size and returns a pointer of type void. This means that
we can assign it to any type of pointer. The malloc() doesn't care how the memory allocated will be used, malloc()
just returns a pointer to a series of bytes. One can't do anything with a void pointer except acknowledge its existence
until we convert (type cast) it to another variable type.
C has the ability to convert one type of variable to another type by using cast operator. The syntax is simple, put the
variable type you want to convert to inside parentheses.
A size is an unsigned integer value. C has a sizeof operator, which computes the size, in bytes, of a variable or data
type. It is advisable to use sizeof operator to specify the size of variable or data type because size of variable and data
type is compiler and machine dependent.

#include<stdio.h>
#include<stdlib.h>
void main()
{
int *ip;
float *fp, *fq;

ip=(int *)malloc(sizeof(int));
fp=(float *)malloc(sizeof(float));
fq=(float *)malloc(8));

printf("sizeof(ip):%d\tsizeof(*ip):%d\n",sizeof(ip),sizeof(*ip));
printf("sizeof(fp):%d\tsizeof(*fp):%d\n",sizeof(fp),sizeof(*fp));
printf("sizeof(fq):%d\tsizeof(*fq):%d\n",sizeof(fq),sizeof(*fq));
}
Output:
sizeof(ip):2 sizeof(*ip):2
sizeof(fp):2 sizeof(*fp):4
sizeof(fq):2 sizeof(*fq):4

The cast operators (int *) or (float *) give integer or float pointer to memory. The size of a pointer is how many
bytes it takes to store the address, not the size of the memory block the pointer is pointing to. Hence size of fp and fq is
2 bytes and size of memory block pointer by fp and fq is 4 bytes even though the block pointed by fq is of 8 bytes.

According to the conceptual view the program instructions and global & static variables are kept in a permanent storage
area and local area variables are stored in stacks. The memory space that is located between these two regions is available
for dynamic allocation during the execution of the program. The free memory region is called the heap.

#include<stdio.h>
#include<stdlib.h>
int x;
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

void main()
{
int a;
int *ip;
ip=(int *)malloc(sizeof(int));

printf("global: &x :%X\n", &x);


printf("pointer: ip:%X\n", ip);
printf("local: &a :%X\n", &a);
}

Output:
global: &x :00AA
pointer: ip:0586
local: &a :FFF4

0000
Program
Instruction
Permanent Data like
(Data
global and static
Segment)
variables

Heap

Stack

FFFF

Figure: Variables in memory

The size of heap keeps changing when program is executed due to creation and death of variables that are local for
functions and blocks. Therefore, it is possible to encounter memory overflow during dynamic allocation process. In such
situations, the memory allocation functions mentioned above will return a null pointer. A null pointer points nowhere
hence it is not a pointer one can use. Therefore, whenever malloc is used it is vital to check the returned pointer before
using it.

int *ip = (int *)malloc(100 * sizeof(int));


if(ip == NULL)
{
printf("ERROR: out of memory\n");
exit or return
}

Compile time storage of a variable is allocated and released by the system in accordance with its storage class. With the
dynamic runtime allocation, it is a responsibility of programmer to release the space when it is not required. The release of
storage space becomes important when the storage is limited. When block of memory is no longer needed and also there is
no intension to reuse that block, then such block may be released for future use. Dynamically allocated memory is de-
allocated with the free function. The syntax of free function is
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

void free(void *block);

#include<stdio.h>
#include<stdlib.h>
void main()
{
int *ip = (int *)malloc(sizeof(int));

if(ip == NULL)
{
printf("ERROR: out of memory\n");
exit(1);
}
*ip=678;
printf("*ip:%d\n", *ip);
free(ip);
}

Output:
ip:0598 *ip:678

Memory allocated with malloc lasts as long as programmer wants it to. It does not disappear automatically when a
function ends.

1.3.3 Pointer Assignment


The assignment operation (=) between two pointers makes them point to the same pointee. Pointer assignment does not
touch the pointees. It just changes one pointer to have the same reference as another pointer.
#include<stdio.h>
void main()
{
int a=56, *ip, *iq;

ip = &a;
iq = ip;

printf("&a: %X\n", &a);


printf("Address in ip: %X and iq: %X\n", ip,iq);
printf("*ip: %d\t and *iq: %d\n", *ip, *iq);
}
ip FFF4
Output: 56 a
&a: FFF4
Address in ip: FFF4 and iq: FFF4 iq FFF4 FFF4
*ip: 56 and *iq: 56

Statement iq = ip do pointer assignment. After pointer assignment, the two pointers are said to be "sharing" the
pointee. That two or more entities can cooperatively share a single memory structure is a key advantage of pointers.
Sharing can be used to provide efficient communication between parts of a program.

One can use pointers with the assignment operators if the following conditions are met:
 The left-hand operand is a pointer and the right-hand operand is a null pointer constant.
 One operand is a pointer to an object or incomplete type; the other is a pointer to void (whether qualified or not).
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

 Both of the operands are pointers to compatible types (whether qualified or not).
In the last two cases, the type pointed to by the left-hand side must have at least the same qualifiers as the type pointed to
by the right-hand side (possibly more).

1.4 Pointer Dereference


Dereferencing is an operation performed to access and manipulates data contained in memory location pointed by a
pointer. The unary operator * is dereference or indirection operator that is prefix to a pointer variable or pointer
expression. This operator returns an l-value equivalent to the value at the pointer address. Any operation performed on
dereference pointer directly affect the value of variable or value stored in memory location.

#include<stdio.h>
void main()
{
int a=56, *ip=&a, b=6;

*ip=*ip + 5;
printf("value of 'a' after addition:%d\n", a);
ip= &b;
*ip=7 * *ip;
printf("value of 'b' after multiplication:%d\n", b);
}
Output:
value of 'a' after addition:61
value of 'b' after multiplication:42

A single pointer variable can be used to point different variables or memory locations. Binary operator * is used for
multiplication and unary operator * is used for indirection. Indirection allows the contents of a variable to be accessed and
manipulated without using the name of the variable.

Note: A dereference operation on an un-initialized pointer gives a serious runtime error. Each pointer must be assigned a
pointee before it can support dereference operations.

Like other variables, pointer variables can be used in arithmetic or logical expressions.
#include<stdio.h>
void main()
{
int a=5, b=7, *ip1=&a, *ip2=&b;
int y, sum, z;

y=*ip1**ip2;
sum=sum+*ip1;
z= 5* - *ip2 / *ip1;
printf("y = %d\t sum = %d\t z = %d\n", y, sum, z);
printf(" a<b = %d\n", *ip1 < *ip2);
}
Output:
y = 35 sum = 5 z = -7
a<b = 1

Multiple dereference operation starts at the pointer and follows its address link to access its pointee.

#include<stdio.h>
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

void main()
{
int a=56;
int *pa = &a;
int **pb = &pa;
int ***pc = &pb;

printf("address of ---> a: %X\t pa: %X\t pb: %X\n",&a,&pa,&pb);


printf("address in ---> pa: %X\t pb: %X\t pc: %X\n",pa,pb,pc);
printf("pc:%X\t*pc:%X\t**pc:%X\t ***pc:%d\n",pc,*pc, **pc,***pc);
}
Output:
address of ---> a: FFF4 pa: FFF2 pb: FFF0
address in ---> pa: FFF4 pb: FFF2 pc: FFF0
pc:FFF0 *pc: FFF2 **pc:FFF4 ***pc:56

*pc **pc ***pc


pc FFF0 pb FFF2 pa FFF4 a 56
FFF0 FFF2 FFF4

2. Ordered List
The data objects linear lists, stacks, queues, polynomials, and matrices are ordered lists. Polynomials are ordered by
exponent while matrices are ordered by rows and columns. There are two types of representation for ordered lists.
1. Sequential representation
2. Linked representation

In Sequential representation sequential mapping can be used to represent ordered lists. Sequential representation has the
property that successive nodes of data object are stored at a fixed distance apart. Sequential scheme is supported by static
allocation strategy. Arrays are probably the most common data structure used to represent sequential scheme. The linear
relationship between the data elements of an array is reflected by the physical position of the data in memory, not by any
information contained in the data element themselves. This makes it easy to compute the address of an element in an
array. In most languages, arrays are convenient to declare. The advantages of sequential representation are
1. Easy to implement (e.g. create & Traverse)
2. Easy to access any element in list (random access)
and disadvantages are
1. Wastage of memory space
2. There is lot of data movements in order to perform insertion, deletion, sorting or merging operations.
3. Merging require an additional space

Also when problem demands for several ordered lists (like multiple stacks & queues) of varying sizes, sequential
representation again proved to be inadequate, by storing each list in a different array of maximum size. Storage may be
wasted.

Linked representation is an alternate representation for ordered lists that will overcome the disadvantages of sequential
representation. Unlike a sequential representation where successive elements of a list are located at fix distance apart, in a
linked representation these elements may be placed anywhere in a memory. Another way of saying this is that in
sequential representation order of element is same as is ordered list, while in linked representation these two sequences
need not be the same. In linked list a list in stored in memory have each element in list containing a field called link or
pointer, which contain the address of next element in the list

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

2.1. Linked Representation


Let us consider an order list [3, 6, 8, 11, 15]. When this ordered list is stored in an array using sequential representation,
the order of element is mapped with the index of an array.

index 0 1 2 3 4
element 3 6 8 11 15

Here the entire array is allocated as one block of memory. Each element in the list gets its own space in the array. Any
element can be accessed directly using its index. The address of any element is computed by using fast address arithmetic.
Now if we want to store an ordered list in an array using linked representation, following information is required
1. Which is the first element? (or marking of the first element)
2. Current element (i.e. element in processing) has information about next element
3. Which is the last element? (or marking of the last element)
The elements of an ordered list are stored in the array at any place and index of an array does not indicate their order in
the list. Index only signifies position of element in an array. An extra field (i.e. link) must be associated with each element
to keep information about next element. The NULL value in link field of an element indicated that this element is the last
element in an ordered list. Also the information about first element is stored in some variable.
first
index 0 1 2 3 4
element 6 15 3 8 11
link 3 N 0 4 1

In this representation the link field of current element always stores the index of next element in an ordered list. This
linked representation has their own strengths and weaknesses, but they happened to be strong where sequential
representation is weak.
Let us insert value 9 in the given ordered list. The new ordered list is [3, 6, 8, 9, 11, 13]. To implement this new ordered
list in linked representation; append 9 in the array and link field of element 9 will take value from link field of element 8
(i.e. previous element to element 9) & link field of element 8 will be set to index of element 9.
first

index 0 1 2 3 4 5
element 6 15 3 8 11 9
link 3 N 0 5 1 4
Just by setting the two link fields and without moving single element of the list it is easy to insert new element in the
ordered list that is represented in linked form. Similarly it is easy to delete any element from the ordered list too.

3. Linked Lists
Linked lists are among the simplest and most common data structures. They are linear data structure. They can be used to
implement several other common abstract data types, including ordered list, stacks, queues, polynomial etc.
A linked list allocates space for each element separately in its own block of memory called a "linked list element" or
"node" when necessary. The list gets is overall structure by using pointers to connect all its nodes together like the links in
a chain. Each node contains two fields: "data" field to store an element of a list and “link" field is a pointer used to link
one node to another node. So link is always pointer to node.

Node data link

15 20

struct node
{
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

int data;
struct node *link;
};

Variable link is a pointer to the same structure in which it is declared leads to self-referencing pointer (recursively defined
struct). By using dynamic memory allocation (like a call to malloc()), memory from heap is allocated to each node in a
linked list.
first=(struct node*)malloc(sizeof(struct node));

The start of a linked list is stored in a "head" or “first” pointer which points to the first node. The first node contains a
pointer to the second node. The second node contains a pointer to the third node, and so on. The last node in a linked list
has its link field set to NULL to mark the end of a list.

Example: create a singly linked list of two nodes.


Step 1: declare two variables to hold the address of two nodes
struct node *first, *second;
Step 2: allocate memory to first node and put some data in that node
first=(struct node*) malloc(sizeof(struct node));
first->data=5;
firs 5
t
Step 3: allocate memory to second node and put some data in that node
second=(struct node*) malloc(sizeof(struct node));
second->data=8;

second 8

Step 4: connect first node to second node


first->link=second;
second
first
5 8

Step 5: make second node as the last node of a list


second->link=NULL;

second
first
5 8 N

first 5 8 N

Note: Visiting and Processing data in each node of a list at least once is known as traversal of the list. In linked list
information about the first node is available and also the last node is marked by NULL in the link field. Hence traversal
starts from the first node, it processes data in the first node and then it reaches to the second node by using link field of
first node. In this way after processing a current node pointer moves to the next node and this movement continues till one
reaches to the last node.

Program: To create a singly linked list of two nodes.


#include<stdio.h>
#include<stdlib.h>
struct node

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

{
int data;
struct node *link;
};

void main()
{
struct node *first, *second, *temp;

/* allocate memory to first node and put some data in that node */
first=(struct node*)malloc(sizeof(struct node));
first->data=5;

/* allocate memory to second node and put some data in that node */
second=(struct node*)malloc(sizeof(struct node));
second->data=8;

/* make a linked list by connecting nodes and putting end marker */


first->link=second;
second->link=NULL;

/* --traversing in the linked list-- */


temp=first;
while(temp != NULL)
{
printf("%d\n", temp->data);
temp=temp->link;
}
}

Program: To create a singly linked list of three nodes.


first 5 8 2 N
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};

void main()
{
struct node *first, *second, *third, *temp;

/* allocate memory to first node and put some data in that node */
first=(struct node*)malloc(sizeof(struct node));
first->data=5;

/* allocate memory to second node and put some data in that node */
second=(struct node*)malloc(sizeof(struct node));
second->data=8;

/* allocate memory to third node and put some data in that node */
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

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


third->data=2;

first->link=second; /* connect first node to second node */


second->link=third; /* connect second node to third node */
third->link=NULL; /* make third as the last node of a list */

/* --traversing in the linked list-- */


temp=first;
while(temp != NULL)
{
printf("%d\n", temp->data);
temp=temp->link;
}
}

3.1 Creation of Singly Linked List


In order to create a singly linked list of n (where n>=0) nodes, there are two cases
1. n = 0, list with no elements (nodes) i.e. empty list.
2. n > 0, list with some elements (nodes)
We need some representation of the empty list (the list with zero nodes). The most common representation chosen for the
empty list is the NULL head pointer (i.e. head is set to NULL). The empty list case is the one common weird "boundary
case" for a linked list code. When working on linked list code, it's a good habit to remember to check the empty list case
to verify that it works too. Sometimes the empty list case works the same as all the cases, but sometimes it requires some
special case code. Figure shows flow chart to create singly linked list of n node.
n
== 0 >0

Create empty list: Create first node:


 Mark head as NULL  Create a node, put data into it
to make empty list;  Make this node a head (or first) node of the list

Create n-1 nodes iteratively and connect them properly:


Use loop that run (n-1) time to do following things
 Create a new node, put data into it
 Then the link field of previously created node takes the
address of this new node

Mark last node:


 To mark end of list put NULL in the link field of the last node.

Figure. flow chart to create singly linked list of n node

Program: To create a singly linked list of n nodes by using loop.


#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

void main()
{
struct node *head, *curr, *next, *temp;
int i, n;

printf("Program to create singly linked list on n nodes\n");


printf("Enter value of n:");
scanf("%d",&n);
if(n<=0)
head=NULL;
else
{ /* --create first (head) node-- */
head=(struct node*)malloc(sizeof(struct node));
printf("Enter data for 1 node:");
scanf("%d",&head->data);
curr=head;

for(i=1; i<n; i++)


{ /* --Create rest of the n-1 nodes-- */
next=(struct node*) malloc(sizeof(struct node));
printf("enter data for %d node:",i+1);
scanf("%d", &next->data);
curr->link=next;
curr=next;
}
curr->link=NULL;
}
/* --traversing in the linked list-- */
printf("Traversing in the linked list:\n");
if(head == NULL)
printf("list is empty\n");
else
{
temp=head;
while(temp != NULL)
{
printf("%d\n", temp->data);
temp=temp->link;
}
}
getch();
}

The typical run of above program to create a singly linked list of n (=3)three nodes is shown below. Variable head always
holds the address of first node in the list. Variable curr always keeps address of a last node added (tail pointer) in the list.
Since new node is always added after the last node in the list hence one always need address of last node. Variable next
gets address of newly created node.

Example: create a singly linked list of two nodes.


1. Initially: head NULL
2. Create first node: head=(struct node*)malloc(sizeof(struct node));
scanf("%d",&head->data);

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

head 5

3. curr always points to a node that is appended (added at the end) in the list: curr=head;
head 5
curr

4. Iteration with i=1, create a second node next=(struct node*)malloc(sizeof(struct node));


scanf("%d", &next->data);
head 5 next 8
curr

Connect first node to second node: curr->link=next;


next
head 5 8
curr

Let curr points to the last node (appended in list): curr=next;

head next
5 8
curr

5. Iteration with i=2, create a third node: next=(struct node*)malloc(sizeof(struct node));


scanf("%d", &next->data);
head 5 8 next 2
curr

Connect second node to third node: curr->link=next;


head next
5 8 2
curr

Let curr points to the last node (appended in list): curr=next;

head 5 next
8 2
curr

6. To mark the end of the list, the link field of last node is set to NULL: curr->link=NULL;

head 5 next
8 2 N
curr

7. The singly linked list of three nodes (n=3) look like:

head 5 8 2 N

Note: The best way to design and think about code of linked list is to use a drawing to see how the pointer operations are
setting up memory. Take this opportunity to practice looking at memory drawings and using them to think about pointer
intensive code. One will be able to understand many of the later, more complex functions only by making memory
drawings like this on your own.

There are two ways to write function to create singly linked list (SLL) of n nodes.
a. Keeping Head Pointer (info about first node) local to calling function and use parameter passing technique
struct node * create(int n)
{

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

struct node *head, *curr, *next, *temp;


int i;
if(n<=0)
head=NULL;
else
{ /* --create first (head) node-- */
head=(struct node*)malloc(sizeof(struct node));
printf("Enter data for 1 node:");
scanf("%d",&head->data);
curr=head;

for(i=1; i<n; i++)


{ /* --Create rest of the n-1 nodes-- */
next=(struct node*)malloc(sizeof(struct node));
printf("enter data for %d node:",i+1);
scanf("%d", &next->data);
curr->link=next;
curr=next;
}
curr->link=NULL;
}
return(head);
}

Code shown below is a recursive function to create SLL of n nodes.


struct node * create(int n)
{
struct node *curr;
int i;
if(n<=0)
return(NULL);
else
{
curr=(struct node*)malloc(sizeof(struct node));
printf("Enter data for node:");
scanf("%d",&curr->data);
curr->link=create(n-1);
return(curr);
}
}

b. Keeping Head Pointer (info about first node) as global variable


#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *head=NULL;

void create(int n)
{
int i;
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

struct node *curr, *next;

if(n <= 0 )
head=NULL;
else
{
curr=(struct node*)malloc(sizeof(struct node));
printf("enter data for 1 node:");
scanf("%d", &curr->data);
head=curr;
for(i=1; i<n; i++)
{ /* --Create rest of the n-1 nodes-- */
next=(struct node*)malloc(sizeof(struct node));
printf("enter data for %d node:",i+1);
scanf("%d", &next->data);
curr->link=next;
curr=next;
}
curr->link=NULL;
}
}
The time complexity of creation of singly linked list of n nodes is O(n). Same is also applicable to the traversal.

3.2 Traversal in a Singly Linked List


There are two possibilities to traverse in a SLL, either list is empty, or it contains nodes. Figure shows flow chart for
traversal in a Singly Linked List.

head
Is NULL Is not NULL

List is Empty Traverse in non empty list:


 Print message to inform  Start traversal from first node
list is empty

Iteratively traverse in the list till you reach the end of the list
 Print the data of the current node
 Move to next node by using address stored in link field of
current node

Figure. Flow chart for traversal in a Singly Linked List.

Following is a code for traversal in a Singly Linked List.


void traverse(struct node *head)
{
struct node *curr;

printf("Traversing in the linked list:\n");


if(head == NULL)
printf("list is empty\n");
else
{
curr=head;
while(curr != NULL)
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

{
printf("%d\n", curr->data);
curr=curr->link;
}
}
}

The typical run of above program, traverse in the singly linked list of n (=3) is shown below. Traversal start from the first
node, it goes through all the nodes and end after processing the last node. Variable temp keeps the track of node in a
migration process.

Example: Traverse a singly linked list of three nodes.


1. Given SLL of three node
head 5 8 2 N

2. Let curr=head , temp points to first node

head 5 8 2 N
curr

3. Enter in the while loop as temp is not NULL and process the node pointed by temp (i.e. first node)
printf("%d\n", curr ->data);
move to second node by using link field of first node
curr = curr ->link;

head 5 8 2 N
curr

4. While loop continue as temp is not NULL and process the node pointed by temp (i.e. second node)
printf("%d\n", curr ->data);
move to third node by using link field of second node
curr = curr ->link;

head 5 8 2 N
curr

5. While loop continue as temp is not NULL and process the node pointed by temp (i.e. third node)
printf("%d\n", curr ->data);
move to next node by using link field of third node
curr = curr ->link;

head 5 8 2 N

curr NULL

6. Since curr is now NULL hence while loop get terminated after processing all the three nodes in the SLL

Shown below is a recursive function to traverse in a SLL of n nodes


void traverse(struct node *curr)
{
if(curr != NULL)
{
printf("%d\n", curr->data);
traverse(curr->link);
}
}
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

Traversal function with Head Pointer (info about first node) as global variable is shown below
void traverse()
{
struct node *curr;

printf("Traversing in the linked list:\n");


if(head == NULL)
printf("list is empty\n");
else
{
curr =head;
while(curr != NULL)
{
printf("%d\n", curr ->data);
curr = curr ->link;
}
}
}

Exercise:
1. Write a function to count number of nodes in a singly linked list.
Solution:
int count()
{
int c=0;
struct node *temp=head;

while(temp != NULL)
{
c=c+1;
temp=temp->link;
}
return(c);
}
2. Write a recursive function to count number of nodes in a singly linked list.
Solution:
int count(struct node *temp)
{
if (temp == NULL)
return(0);
else
return(1+count(temp->link));
}
3. Suppose list is a linked list in memory consisting of numerical values. Write a function find average of the values
in the list.
Solution:
float Average()
{
struct node *temp;
int sum=0, c=0;

temp=head;
while(temp != NULL)
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

{
c=c+1;
sum=sum + temp->data;
temp=temp->link;
}
return((float)sum/c);
}
a. Finding the product PROD of the element.
Hint: initialize prod=1 and in loop prod = prod * first->data

4. Suppose list is a linked list in memory consisting of numerical values. Write a function to find Maximum value in
the list.
Solution:
int Max()
{
struct node *temp=head;
int big;

big=head->data;
temp=head->link;
while(temp != NULL)
{
if (temp->data > big)
big=temp->data;
temp=temp->link;
}
return(big);
}

5. Write a function to create an exact copy of a given singly linked list.


Solution:
struct node * copy(struct node *list)
{
struct node *temp;

if (list == NULL)
return NULL;
else
{
temp=(struct node *)malloc(sizeof(struct node));
temp->data=list->data;
temp->link=copy(list->link);
return temp;
}
}
6. Write a function to invert links of singly linked list.
Solution:
 Given SLL with initial position of curr and prev

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

curr

head 5 8 2 9 N
prev  NULL

 Enter into while loop and process first node and set link field of first node to NULL to make is last node

prev curr next

head N 5 8 2 9 N

 In loop, process second node, let link field of second node takes the address of first node

prev curr next

head N 5 8 2 9 N

 In loop, process third node, let link field of third node takes the address of second node

prev curr

head N 5 8 2 9 N
next  NULL
 In loop, process forth node , let link field of forth node takes the address of third node

prev

head N 5 8 2 9
curr, next  NULL

 Now make last node to become first node

N 5 8 2 9 head

Following is a code to invert the links of a singly linked list.


/*Code: To invert the links of a singly linked list */
void invert ()
{
struct node * curr, * prev, * next ;

curr=head ;
prev=NULL;
while(curr != NULL)
{
next=curr->link;
curr->link=prev;
prev=curr;
curr=next;
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

}
head = prev;
}

7. Write a recursive function to invert the links of a singly linked list.


8. A binary number is stored in linked list, with each node store one bit of a number. Write a function to find the
decimal equivalent of the binary number.
9. Write a Boolean valued function to check whether two singly linked lists are equal or not.
10. Suppose list is a linked list in memory consisting of numerical values. Write a function to count odd numbers and
even numbers in the list.
11. An ordered list L is maintained in an array A. Write a function to create linked list for ordered list L.
12. Write a function to create SLL that stores binary equivalent of a given decimal number with each node having one
bit only.
13. Write a function to print data stored in middle node by traversing a singly linked list (n>2) only once.
14. Write a function to print data stored in last but one node by traversing a singly linked list (n>2) only once
15. Write a function to swap 2 adjacent elements by adjusting only the links (not the data) while traversing a
singly linked list (n>2) only once.
16. Write a program for printing the following in a given linked list:
a. maximum
b. minimum
c. maximum – minimum
17. A node of a SLL Stored record about student (RollNo, Name, Marks in %). Write functions to read data about
students in a class and print Name & RollNo of those students who have secured more than 60% marks.
18. A node of a SLL stored either customer ID (5-digit number) or customer name and phone number. Write function
to read information about customers and print them.
19. A bag (also called multiset) is a generalization of a set, where elements are allowed to appear more than once. For
example, the bag (a, a, b) consists of two copies of a and one copy of b. However, a bag is still unordered, so the
bags (a, a, b) and (a, b, a) are equivalent. Implement the following features for bag:
 add (v: G; n: INTEGER), which adds n copies of v to the bag.
 remove (v: G; n: INTEGER), which removes as many copies of v as possible, up to n. For example,
removing one copy of a from the bag (a, a, b) will result in a bag (a, b), while removing two copies of c
from the same bag will not change it.
 subtract (other: LINKED BAG [G]), which removes all elements of other from the current bag. For
example, taking the bag (a, a, b) and subtracting (a, b, c) from it will yield the bag (a).
20.

3.3 Insertion operation in singly linked list


In order to insert some data in a linked list, first create a node and put data in the new node.
temp=(struct node *) malloc(sizeof(struct node));
scanf("%d",&temp->data);

There are four possible cases


Case 1: Insert node in an empty list
If a list is empty then the newly created node, temp is inserted in a list and it becomes the first node as well as the last
node in the list.
if (head ==NULL)
{
head=temp; /* let temp becomes the first node */
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

temp->link = NULL; /* also set temp as the last node */


}
head NULL
head 5 N
temp 5

Case 2: Insert node at first position in a list


When newly created node temp is inserted at the first position, the first node in the original list becomes the second node.
For this first connect the temp to list and then make temp as the first node of the list.
if (pos == 1)
{
temp->link=head; /* connect temp to original list */
head = temp; /* let temp becomes the first node */
}
head
5 8 2 N
temp 4
 Connect temp to list and make it a head node
5 8 2 N
head 4

Case 3: Insert node somewhere in the middle of a list


In order to insert a node at mth position in a link list of n nodes (n ≥ m), starting from the first node traverse up to the (m-
1)th node in a list.
curr=head; curr=head;
th
c=1; /* traverse up to the (m-1) node */
th
/* traverse up to the (m-1) node */ for(c=1; c < pos-1; c++)
while (c < pos-1) curr=curr->link;
{
c++;
curr=curr->link;
}

Now pointer to (m-1)th node is curr and newly created node temp will be inserted after curr so that it becomes a mth node
in the list. The link field of temp will take address of the mth node (available in link field of curr) of the original list
temp->link=curr->link;
and link field of (m-1)th node (i.e. curr) will take address of new node temp.
curr->link=temp;

Figure shows typical run of, Insert node at 3rd position in a link list of 3.

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

 Insert node at 3rd position.


head
5 8 2 N

temp 4
 Traverse up to 2nd node, starting from head node
head 5 8 2 N
curr
temp 4
 Insert node temp in between 2nd and 3rd node
head 5 8 2 N
curr

temp 4

Figure. Typical run of, Insert node at 3rd position in a link list of 3

Case 4: Insert node at the end of a list


To insert node at the end of a list, first traverse upto the last node in the list.
curr=head;
/* traverse up to the last node in the list */
while (curr->link !=NULL)
curr=curr->link;
Now mark temp as last node
temp->link=NULL;
and attach it to the end of the list. Since curr points to the last node in the list hence temp is attached after node curr.
curr->link=temp;

Figure shows typical run of, Insert node at the end of a link list of 3 nodes.
Insert node at end of the a list
head
5 8 2 N
temp 4
Traverse up to last node, starting from head
head 5 8 2 N
curr temp 4
 Mark temp as the last node and connect it to last node of original list
head 5 8 2 N
curr
temp 4 N

Figure. Typical run of, Insert node at the end of a link list of 3 nodes

In the following program cases for insert node in the middle and at the end of the list are combined together. Also if value
of pos is greater than the actual number of nodes in the list (i.e. m > n) then new node temp is inserted at the end of the
list.

Code: To insert node in a singly linked list at any position


By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

void insert(unsigned int pos)


{
struct node *temp, *curr;
int c=0;

printf("Enter data to be insertion:");


temp=(struct node *) malloc(sizeof(struct node));
scanf("%d",&temp->data);
if (head ==NULL) /* --list is empty-- */
{
head=temp;
temp->link = NULL;
}
else
{
if (pos == 1) /* --insert node at first position-- */
{
temp->link=head;
head = temp;
}
else
{
c=1;
curr=head;
th th
/* traverse up to the (m-1) node if you want to insert node at m position */
while (c < pos-1 && curr->link !=NULL)
{
c++;
curr=curr->link;
}
if (curr->link == NULL) /* --insert node at the end-- */
{
temp->link=NULL;
curr->link=temp;
}
else /* --insert node in middle-- */
{
temp->link=curr->link;
curr->link=temp;
}
}
}
}

Since insertion of a node in SLL does not require any movement (or shifting) of nodes. It only does traversing up to the
appropriate position or node in a list. The best case is, insert node in an empty list or insert node at first position in a list
that takes O(1) time. The worst case is, insert node at the end of a list that needs traversal from the first node upto the last
node, hence worst case time complexity for insertion operation on SLL is O(n).

Exercise:
1. Singly linked list of n nodes is given. Write a procedure to move first node to end of the list.
Solution:
void Firsttolast()

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

{
struct node *temp, *fnode;

fnode= head;
temp=fnode->link;
head=temp;
while (temp->link !=NULL)
temp=temp->link;
temp->link=fnode;
fnode->link=NULL;
}

2. A singly linked list stores integer data in ascending order. P is a pointer to node, which stores data 20. Insert a
node contain data 18 into singly linked list such that it will be sorted after insertion.
p
15 20

Code:
temp=(struct node *) malloc(sizeof(struct node));
temp->data = p->data;
temp->link = p->link;
p->data = 18;
p->link = temp;

3. Create linked list of n node such that the elements in list are arranged in ascending order.
Solution:
void InsSorted( int val)
{
struct node *curr, *temp, *prev;

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


temp->data=val;
if (head == NULL) /* empty list */
{
temp->link=NULL;
head=temp;
}
else /* non-empty list */
{
curr=head;
while(curr != NULL && curr->data <val)
{
prev=curr;
curr=curr->link;
}
if (curr == head) /* insert as a first node */
{
temp->link=head;
head=temp;
}
else if (curr == NULL) /* insert at the end */

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

{
prev->link=temp;
temp->link=NULL;
}
else /* insert in a middle */
{
prev->link=temp;
temp->link=curr;
}
}
}

4. Let x is a pointer to some arbitrary node in the singly linked list. Write a function to insert node after x in the list

3.4 Deletion operation in singly linked list


In order to delete some node from a list there are three possible cases
Case 1: Delete node from an empty list
If list is empty then no node is available for deletion hence deletion is not possible.
if (head ==NULL)
printf(" List is empty, deletion not possible\n");
Case 2: Delete first node of a linked list
Deletion of first node from non-empty list, makes the second node to become a first node (if second node exists) or makes
the list empty if there is no second node.
if (pos == 1)
{
temp=head;
head=first->link;
free(temp);
}

Figure shows deletion of first node from singly linked list.

head 5 8 2 N

 Let temp points to first node (temp=head;)


 head 5 8 2 N
temp
 Make second node the head node (becomes the first node) head=first-
>link;
temp 5 8 2 N
head
 Delete node pointed by temp (first node of original list)
free(temp);
temp 5 8 2 N
head

Figure. Deletion of first node from singly linked list

Case 3: Delete some node in the middle or at the end of a linked list

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

In order to delete node at mth position in a link list of n nodes (n ≥ m), starting from first node traverse up to (m-1)th node
in the list.
curr=head;
c=1;
th
/* traverse up to the (m-1) node */
while (c < pos-1)
{
c++;
curr=curr->link;
}
Now pointer to (m-1)th node is curr. Let the link field of (m-1)th node will take address of (m+1)th node, which stored in
link field of mth node (i.e in the link field of temp).
temp=curr->link;
curr->link=temp->link;
free (temp);
If temp is a last node in the list then, then link field of curr is set to NULL and curr becomes the last node. Figure 3.6
shows typical run for deletion of 3rd node in a link list of 4 nodes.

 Delete 3rd node in linked list of 4 nodes


head
5 8 2 4 N

 Traverse up to 2nd node, starting from head node


head
5 8 2 4 N
curr temp
 Link field of 2nd node will take address of 4th node and delete 3rd node

head
5 8 2 4 N
curr temp
Figure 3.6. Typical run for deletion of 3rd node in a link list of 4 nodes

In the following program if value of pos is greater than the actual number of nodes in the list (i.e. m > n) then program
simply prints a message "ERROR: No node on this position\n".
Code: To delete node from any position in a singly linked list
void del(unsigned int pos)
{
struct node *temp, *curr;
int c=0;

if (head ==NULL) /* --list is empty-- */


printf(" List is empty, deletion not possible\n");
else
{
if (pos == 1) /* --delete node at first position-- */
{
temp=head;
head=head->link;
free(temp);
}
else
{ /* --traversing in list to get required node-- */

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

c=1;
curr=head;
while (c < pos-1 && curr->link !=NULL)
{
c++;
curr=curr->link;
}
if (curr->link == NULL)
printf("ERROR: No node on this position\n");
else /* --delete node in between or last node-- */
{
temp=curr->link;
curr->link=temp->link;
free (temp);
}
}
}
}

Since deletion of a node from SLL does not require any movement (or shifting) of nodes. It only does traversing up to the
appropriate position or node in a list. The best case is, delete node from an empty list or delete node at first position in a
list that takes O(1) time. The worst case is, delete last node of a list that needs traversal from first node upto last node,
hence worst-case time complexity for deletion operation on SLL is O(n).

The worst-case time complexity of insertion or deletion operation on singly linked list or array is O(n). The most
important advantage of linked list over array is these operations do not require any movement of data in a list.

Exercise:
1. Write a program to delete the node with the largest data value from a link list.
Solution:
void DelBig()
{
struct node *big, *bprev, *curr, *prev,*temp;

if (head == NULL) /* --list is empty-- */


printf(" List is empty\n");
else
{ /* --traversing in list to get required node-- */
big=head;
prev=head;
curr=prev->link;
while(curr != NULL)
{ if(curr->data > big->data)
{
big=curr;
bprev= prev;
}
prev=curr;
curr=curr->link;
}
if (big == head) /* --delete node at first position-- */
{
temp=head;
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

head=temp->link;
free(big);
}
else /* --delete node in between-- */
{
bprev->link=big->link;
free(big);
}
}
}
2. Write a function to delete the node N from a singly linked list. This delete function should call another function
that finds the location of node N and its preceding node.
3. Write an algorithm to implement a linked list and perform insertion and deletion in following way:
i. Insert element at the beginning of a list
ii. Delete last element from the list
4. Write a function which will delete and display the biggest and smallest from the given linked list.
5. Let p be a pointer to the first node in a singly linked list and x an arbitrary node in the list. Write a function to
delete a node x from the list.
6. Delete the node pointed by curr without using any extra pointer variable and provided the address of first node is
not given.
curr
5 8
Code:
while(curr-link!=null)
{
curr->data=curr->link->data;
curr=curr-link;
}
free(curr)

3.5 Merging of singly linked lists


Merging of two linked lists involve mixing of elements of two lists based on some criterion to generate single list. Simple
merging is a concatenation of two lists (that is add elements of second list to the end of first list). The last node of first list
becomes a merge point. Traverse upto the merge point and then connect second list to first list.

Example: Merging (concatenation) of two singly linked lists


 Let first list is of 4 nodes and second list of 3 nodes
head
3 5 7 9 N
1
head2
4 6 8 N

 In order to merge them first traverse in the first list and reach to the last node
curr
head
3 5 7 9 N
1
head2
4 6 8 N

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

 Now set link field of last node of first list to the first node of second list
curr
head
3 5 7 9
1

head2
4 6 8 N

Code: To merge two singly linked lists (concatenation of two lists)


/* head and head2 are global variables pointing to two lists */
void concat()
{
struct node *curr;

if (head == NULL)
head = head2;
else
{
if (head2 != NULL)
{
curr = head;
while(curr->link != NULL)
curr = curr->link;
curr->link = head2;
}
}
}

Concatenation of two lists needs\only traversing in the first list. If first list has n nodes then time complexity of
concatenation operation on SLL is O(n). Unlike array there is no movement of data in SLL for concatenation operation.
Any merge operation on SLL does not require any movement of data that is the major advantage of SLL over array.

Exercise:
1. Let list1 & list2 are two singly linked lists having data in ascending order. Write a function to merge them such
that resultant list will contain data in ascending order.
Solution:
struct node * mergeSortL(struct node *list1, struct node *list2)
{
struct node *p1, *p2, *mlist,*pm;

if (list1 == NULL)
return(list2);
else
{
if (list2 == NULL)
return(list1);
else
{
p1=list1;
p2=list2;
/* --create head node-- */
mlist=(struct node*)malloc(sizeof(struct node));
pm=mlist;
while(p1 != NULL && p2 != NULL)
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

{
if(p1->data < p2->data)
{
pm->link=p1;
pm=p1;
p1=p1->link;
}
else
{
if(p1->data == p2->data)
p1=p1->link;
pm->link=p2;
pm=p2;
p2=p2->link;
}
}
if(p1 == NULL)
pm->link=p2;
else
pm->link=p1;

/* --delete head node-- */


pm=mlist;
mlist=mlist->link;
free(pm);
return(mlist);
}
}
}

Note: In above function merged list (mlist) contains nodes from list1 and list2 that results in disturbing the sequence of
nodes in both the lists. Following function merge the two sorted lists without disturbing the original lists. It creates
sequence of nodes and copy data in them either from list1 or list2.

struct node * mergeSortdL(struct node *list1, struct node *list2)


{
struct node *p1, *p2, *mlist,*pm, *temp;

if (list1 == NULL)
return(list2);
else
{
if (list2 == NULL)
return(list1);
else
{
p1=list1; /* --pointer moving in list1-- */
p2=list2; /* --pointer moving in list2-- */
/* --create head node-- */
mlist=(struct node*)malloc(sizeof(struct node));
pm=mlist; /* --pointer moving in merged list-- */
while(p1 != NULL && p2 != NULL)
{
if(p1->data < p2->data)
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

{
temp=(struct node *)malloc(sizeof(struct node));
temp->data=p1->data;
pm->link=temp;
pm=temp;
p1=p1->link;
}
else
{
if(p1->data == p2->data)
p1=p1->link;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=p2->data;
pm->link=temp;
pm=temp;
p2=p2->link;
}
}
while(p2 != NULL)
{
temp=(struct node *)malloc(sizeof(struct node));
temp->data=p2->data;
pm->link=temp;
pm=temp;
p2=p2->link;
}
while(p1 != NULL)
{
temp=(struct node *)malloc(sizeof(struct node));
temp->data=p1->data;
pm->link=temp;
pm=temp;
p1=p1->link;
}
pm->link=NULL; /* --mark it as a last node-- */
/* --delete head node-- */
pm=mlist;
mlist=mlist->link;
free(pm);
return(mlist);
}
}
}

2. List 1: Element x1 , x2, x3 ------ xm


List 2: Element y1, y2, y3 ---- yn
Merge them in 3rd list such that it contains element as
x1, y1, x2, y2, x3, y3 ------- xn, yn, xn+1, … xm if m>n or
y1, x1, y2, x2, y3, x3 ------- ym, xm, ym+1, … yn if n>=m .No additional nodes may be used.
3. Let list1 & list2 are two singly linked lists. Write function to create a third linked list which is the union of
elements (without repetition) of two linked lists.
4. Let list1 & list2 are two singly linked lists. Write function to create a third linked list which contains elements
common in both the lists.
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

5. Let list1 & list2 are two singly linked lists. Write function to create a third linked list which contains elements not
common in both the lists.

3.6. Splitting of a singly linked list


Example: Splitting of a singly linked list into two linked lists
 Given a singly linked list of 5 nodes
head
3 5 7 9 8 N
1
 In order to split this list in two equal parts, traverse upto the 3rd node
curr
head
3 5 7 9 8 N
1
 Now terminate first list at node number 3 and the second list start from the node number 4
curr
head
3 5 7 N 9 8 N
1
head2
th
 After split first list contains 3 nodes and second list contains 2 nodes. 4 node of original list becomes
the first node second list
head
3 5 7 N

head2
4 6 N

Code: To split a singly linked list into two list each containing n/2 nodes
struct node * split()
{
int n,c;
struct node *curr, *prev;

n=count(head);
if(n < 2)
return NULL;
else
{
curr=head;
c=1;
while( c < (n/2+n%2)) /* (n+1)/2 */
{
prev=curr;
curr=curr->link;
c++;
}
prev->link=NULL;
return(curr);
}
}

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

Split operation on SLL needs traversal in the list of n nodes. The time complexity of split operation is O(n). Again split
operation does not require any movement of data elements in a list.

Exercise:
1. Write a function to split a given linked list of integers into two lists l 1 & l2 where l1 contain nodes having odd
values and l2 contain nodes having even values.
Solution:
void splitEO(struct node *orglist, struct node **list1,
struct node **list2)
{
struct node *temp, *p1=NULL, *p2=NULL;

*list1=NULL;
*list2=NULL;
while(orglist != NULL)
{
temp=(struct node *)malloc(sizeof(struct node));
temp->data=orglist->data;
if((orglist->data % 2) == 0)
{
if(*list1 == NULL)
{ /* -- node is first node in list1-- */
*list1=temp;
p1=temp;
}
else
{
p1->link=temp;
p1=temp;
}
}
else
{ /* -- node is first node in list2-- */
if(*list2 == NULL)
{
*list2=temp;
p2=temp;
}
else
{
p2->link=temp;
p2=temp;
}
}
orglist=orglist->link;
}
if(p1 != NULL)

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

p1->link=NULL;
if(p2 != NULL)
p2->link=NULL;
}

4. Circular Singly Linked List


In a singly linked list, nodes are linked together by keeping address of a next node in the link field of a current node (or by
keeping address of a previous node in the link field of a current node). This linking mechanism permits to us to travel in
the forward direction (or backward direction). There is no way to come back to the a previously visited node. Some
applications need access to previously visited node (not frequently). Circular singly linked list allows to do this by
keeping address of first node in the link field of last node. In this list, all nodes are linked in a continuous circle, without
using null.

head 5 8 2

With a circular list, a pointer to the last node gives easy access also to the first node, by following one link. Thus, in
applications that require access to both ends of a list (e.g., in the implementation of a queue), a circular structure allows
one to handle the structure by a single pointer, instead of two. Circular linked list offer benefit from the ability to traverse
the full list by starting from any given node. Where as in SLL, traversal of full list is only possible if traversal starts from
first node only.
Figure shows flow chart depicts the creation of circular singly linked list.

n
== 0 >0

Create empty list: Create first node:


 Mark head as NULL  Create a node, put data into it
to make empty list;  Make this node a head (or first) node of the list

Create n-1 nodes iteratively and connect them properly:


Use loop that run (n-1) time to do following things
 Create a new node, put data into it
 Let the link field of previously created node takes the
address of this new node

Mark last node:


 Put address of first node (i.e head) the link field of last
node in order to make list circular one.

Figure. Flow chart depicts the creation of circular singly linked list.

Program: Implementation of Circular Singly Linked List


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
};
struct node *head;
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

void createCLL(int n)
{
struct node *curr, *next, *temp;
int i;

if(n<=0)
head=NULL;
else
{ /* --create first (head) node-- */
head=(struct node*)malloc(sizeof(struct node));
printf("Enter data for 1 node:");
scanf("%d",&head->data);
curr=head;
for(i=1; i<n; i++)
{ /* --Create rest of the n-1 nodes-- */
next=(struct node*)malloc(sizeof(struct node));
printf("enter data for %d node:",i+1);
scanf("%d", &next->data);
curr->link=next;
curr=next;
}
curr->link=head;
}
}

Example: create Circular Singly Linked List of 3.


 Create a first node, put data into it and marked it as a head node
head 5
curr

 Create a second node, put data into it and let link field of first node takes the address of second node
head next
5 8
curr

 Create a third node, put data into it and let link field of second node takes the address of third node
head next
5 8 2
curr

 In order to make SLL circular SLL, let the link field of last node takes the address of first node

head next
5 8 2
curr

void traverseCLL()
{
struct node *temp;

printf("Traversing in the linked list:\n");


if(head == NULL)
printf("list is empty\n");
else

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

{
temp=head;
do
{
printf("%d\n", temp->data);
temp=temp->link;
} while(temp != head)
}
}

void main()
{
int n,ch;

clrscr();
while(1)
{
printf("CIRCULAR SINGLY LINLKED LIST\n");
printf("1.Create\n2.Traverse\n3.Quit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter No of nodes:");
scanf("%d",&n);
createCLL(n);
break;
case 2:
if (head == NULL)
printf("List is EMPTY\n");
else
{
printf("Traversing in The List:\n");
traverseCLL();
}
getch();
break;
case 3:
return;
default:printf("Wrong choice:re-enter\n");
}
}
}

The time complexity of operations on circular linked list is same as SLL. The only advantage is visit to previous node
from current node is possible in Circular singly linked list

Exercise:
1. Write a function to concatenate two Circular singly linked lists.
Solution:
struct node * concatCLL(struct node *list1, struct node *list2)
{
struct node *temp;
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

if (list1 == NULL)
return(list2);
else
{
if (list2 != NULL)
{
temp=list1;
while(temp->link != list1)
temp=temp->link;
temp->link=list2;
temp=list2;
while(temp->link !=list2)
temp=temp->link;
temp->link=list1;
}
return(list1);
}
}
2. Let p is a pointer to some arbitrary node in Circular singly linked list. Write a code to reach its previous node.
Code:
temp=p;
while (temp->link != p)
temp=temp->link;
3. There are two circular singly linked list available A and B. Write a function to generate third circular linked list
called as C which is nothing but addition of nodes A and B. if one of A and B get exhausted the remaining
elements of other are to copied.
4. Write a function to reverse the direction of links in a circular singly linked list.
Solution:
void invertCLL()
{
struct node * curr, * prev, * next ;

if (head != NULL)
{
curr=head ;
prev=NULL;
do
{
next=curr->link;
curr->link=prev;
prev=curr;
curr=next;
}while(curr != head);
head->link=prev;
head = prev;
}
}
5. Write a function to search a “Key” in a circular linked list. If key is found the function should return a pointer
node when match is found else should return null pointer.

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

6. Implement data structure queue by using circular linked list.

5 8 2 head

Ans: Insertion takes place at head whereas deletion takes place from headlink

5. Linked Stack
A stack is an ordered list in which all insertions and deletions are made at one end,
called the top. This makes stack a single ended data structure where element inserted
last will be deleted first i.e. LIFO (Last In First Out). LIFO makes it is possible to
top remove items from a stack in reverse order of their insertion.
A stack is characterized by two fundamental operations, called push and pop. The
push operation adds a new item at the top of a stack. The pop operation removes an
item from the top of a stack.
Linked stack is an implementation stack using a linked list. In linked stack an
element is a node that contains information about element and the link field to
connect nodes in a stack to form a linked list. Top is a pointer that always points to
the node recently added to a stack (last node added). If a stack is empty then top is NULL.
struct node
{
int data;
struct node *link;
};
struct node *top=NULL;
Initially, stack is empty hence top is NULL.

Note: The stack was first proposed in 1946, in the computer design of Alan M. Turing (who used the terms "bury" and
"unbury") as a means of calling and returning from subroutines.

Figure shows push and pop operations on Linked Stack.

top 9

top 8 top 8
8

topNULL top 5 N 5 N 5 N 5 N
1. Initial
Stack 2. push (5) 3. push (8) 4. push (9) 5. pop

Figure. Push and pop operations on Linked Stack

push operation: it adds a new node in a stack. The link field of this new node takes address of the node at the top and
then set top to this newly added node. If this new node is the first node to be added in a stack then set its link field to
NULL to mark the bottom of a stack (i.e. last node in linked list).
void push(int val)
{
struct node *temp;

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

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


temp->data=val;
temp->link=top;
top=temp;
}

Re-usability of code: push operation on Linked stack is a very special case of insertion operation in singly linked list.
Push operation in linked stack always insert a node at first position so as to comply the property LIFO. Hence push is
equivalent to the case insert node at first position in singly linked list.

pop operation: it removes a node from a stack. The node pointed by top is deleted from stack by making its previous
node to become the node on top of a stack. If there is single node in a stack, then deletion of this node makes stack empty
by setting top to NULL.
void pop()
{
struct node *temp;

if(top == NULL)
printf("STACK is EMPTY\n");
else
{
temp=top;
top=top->link;
printf("Poped Value is:%d\n", temp->data);
free(temp);
}
}

Re-usability of code: Pop operation on Linked stack is a very special case of deletion operation in a singly linked list.
Pop operation on linked stack always delete node at first position (if stack is not empty) so as to comply the property
LIFO. Hence pop is equivalent to the case delete node from first position in the singly linked list.
Note: The result of an illegal attempt to pop an element from an empty stack is called underflow.
Traversal in linked stack: it is same as traversal in singly liked list. Traversal begins from the node at the top and goes
through all nodes and reaches to the bottom of a stack. Null in the link field of a node of a stack indicates the bottom of a
stack. If top is NULL then stack is empty.
void traverse()
{
struct node *temp;

if(top == NULL) top 9


printf("STACK is EMPTY\n");
else
{
temp=top; 8
printf("Traversing in STACk:\n");
while(temp != NULL)
{ 5 N
printf("%d\n", temp->data);
temp=temp->link;
}
}
}

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

As discussed above, push operation is a very special case of insertion operation (always insert node at first position) on
SLL while pop operation is a very special case of deletion operation (always delete node at first position) on SLL.
Insertion or deletion at first position in SLL does not need any traversal. Hence time complexity of push and pop
operation is O(1).

Program: Implementation of Linked Stack


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
};

struct node *top=NULL;

void main()
{
int ch,val;

clrscr();
while(1)
{
printf("Implementation of Linked Stack\n");
printf("1.Push\n2.Pop\n3.Traverse\n4.Quit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter data to be pushed:");
scanf("%d", &val);
push(val);
break;
case 2:
pop();
break;
case 3:
traverse();
break;
case 4: return;
default:printf("Wrong choice:re-enter\n");
}
}
}

Exercise:
1. Write a function that uses stack to determine if an input character string is of the form xCy, where x is a string
consisting of the letters ‘A’ and ‘B’ and where y is the reverse of x.
2. Consider a language that does not have arrays but does have stack as data type. That is one can declare
stack s;

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

and the push, pop, popandtest, and stacktop(return element at top but does not remove) are defined. Show how a
one-dimensional array can be implemented by using these operations on two stacks.

Note: First-in-first-out appears to be fairer than last-in-first-out. Queue is more difficult to implement than stack as
actions happen at both the ends.

6. Linked Queue
A queue is an ordered list in which all insertions take place at one end, the rear, while
all deletions take place at the other end, the front. This makes the queue a First-In-First-
Out (FIFO) data structure. In a FIFO data structure, the first item added to the queue
rear will be the first one to be removed
front A queue is characterized by two fundamental operations, called insertQ and deleteQ.
The insertQ operation adds a new item from the rear end while deleteQ operation
removes an item from the front end of a queue.
Linked queue is an implementation of queue using a linked list. The item of a linked
queue is a node that contains information about item and the link field is used to
connect nodes in a queue. front and rear are pointers. rear always points to the node recently added to a queue (last node
added) while front always points to the oldest node in a queue. In linked queue front always points to the first node in
linked list while rear points to the last node in the list if queue is not empty. If a queue is empty then rear and front are
NULL.

struct node
{
int data;
struct node *link;
};
struct node *front=NULL, *rear=NULL;

Figure shows insertion and deletion operations on Linked Queue.


1. Initially empty queue 5. Delete node from queue
front NULL NULL  rear front rear
5 9

2. Insert 8 in queue 6. Insert 6 in queue


front 8 rear rear
front 5 9 6

3. Insert 5 in queue 7. Delete node from queue


front 8 5 rear front 9 6 rear

4. Insert 9 in queue 8. Delete node from queue


front 8 5 9 rear front 6 rear

Figure. Insertion and deletion operation on Linked Queue.

insertQ operation: it add a new node in a queue. Insertion takes place from the rear end. The link field of this new node
takes address of the node pointed by rear and then set rear to this newly added node. If this new node is the first node
added in a queue then rear and front both point to this node.
void insertQ(int val)
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

{
struct node *temp;

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


temp->data=val;
if (rear == NULL) /* -- Queue is empty -- */
front=temp;
else
rear->link=temp;
rear=temp;
}

Re-usability of code: insert operation on Linked queue is a very special case of insertion operation in a singly linked list.
Insert operation on linked queue always insert a node at the end of a queue so as to comply the property FIFO. Hence
insert in linked queue is equivalent to the case insert node at end in the singly linked list.
deleteQ operation: it removes a node from a queue. Deletion takes place from the front end. The node pointed by front is
deleted from a queue by making its next node to become the first node of a queue. If there is single node in a queue, then
deletion of this node makes queue empty by setting both rear and front to NULL. The result of an illegal attempt to
remove an element from an empty queue is called underflow. It occurs when front (as well as rear) becomes NULL.

void deleteQ()
{
struct node *temp;

if (front == NULL)
printf("Queue is Empty\n");
else
{
temp=front;
if(front == rear) /* -- only one node in Queue -- */
front=rear=NULL;
else
front=front->link;
printf("Value deleted:%d\n", temp->data);
free(temp);
}
}
Re-usability of code: delete operation on a Linked queue is a very special case of the deletion operation in a singly linked
list. DeleteQ operation on linked queue always delete node at first position (if queue is not empty) so as to comply the
property FIFO. Hence deleteQ is equivalent to the case delete node from first position in a singly linked list.
Traversal in linked queue: In non-empty queue fronts always points to first node where as rear points to the last node in
a linked queue. Hence in non-empty linked queue traversal begins from the node pointed by front and goes through all
nodes and reaches to the node pointed by rear.

front 5 9 6 8 rear

void traverseQ()
{

struct node *temp;

if (front == NULL)
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

printf(" Queue is empty\n");


else
{
printf("elements in Queue are:\n");
temp=front;
do
{
printf("%d\n", temp->data);
temp=temp->link;
} while( temp != rear)
}
}

As discussed above, insert operation is a very special case of insertion operation (always insert node at the end) on SLL
while delete operation is a very special case of deletion operation (always delete node at first position) on SLL. If instead
of two pointers (front & rear) only single pointer is used to implement a linked queue then insertion operation has time
complexity O(n) and deletion operation takes O(1) time. Use of two pointers (front & rear) makes time complexity of both
the operations O(1).

Program: Implementation of Linked Queue


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
};

struct node *front=NULL, *rear=NULL;

void main()
{
int ch,val;

clrscr();
while(1)
{
printf("Implementation of Linked Queue\n");
printf("1.Insert\n2.Delete\n3.Traverse\n4.Quit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter data to be Inserted in Queue:");
scanf("%d", &val);
insertQ(val);
break;
case 2:
deleteQ();
break;
case 3:
traverseQ();
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

break;
case 4: return;
default:printf("Wrong choice:re-enter\n");
}
}
}

Exercise:
1. What is circular queue? Implement it using circular linked list.
2. Write a function to delete an element from linked queue and insert it into linked stack.
3. Devise a representation for a list where insertions and deletion can be made at either end such a data structure is
called deque. Write a function for insertion and deletion in deque.
4. If p is a pointer to circularly linked list. Show how this list may be used as a queue. Write function to add and
delete elements in queue using p taking into consideration boundary condition when queue is empty.
5. Consider hypothetical data object x2, is a linear list with restriction that while addition to list may be at either end
but deletions can be made from one end only. Design a linked list representation for x 2. Write addition and
deletion functions for x2 specifying initial and boundary conditions for your representation.
6. Implement queue using stacks
7. How would you implement a queue of stacks? A stack of queues? A queue of queues? Write functions to
implement the appropriate operations for each of these data structures.

7. Representation of Polynomial using singly linked list


Let P(x) = anxn+ an-1xn-1+…….+ a2n2+ a1x+ a0 be a polynomial of degree n (if an ≠ 0) in variable x. polynomial is a
collection of terms and each term has coefficient & exponent. In order to represent polynomial using linked list, each node
in linked list represent term of a polynomial. Hence each node contains coefficient, exponent (to represent term) and link
field (to connect nodes). Nodes may be arranged in increasing or decreasing order of exponent.
coef expo link
struct
term
{
float coef;
int expo;
struct term *link;
};

Let P(x) = 6.0x6 -7.5x4+8.0x+9.7 is a polynomial in variable x with degree 6 with 4 non-zero terms. P(x) is represented
using singly linked list as
poly 6.0 6 -7.5 4 8.0 1 9.7 0 N

7.1. Reading of polynomial


Reading a polynomial is similar to creating a linked list of n nodes, where n is number of terms in polynomial.
struct term * readPoly(int nterm)
{
struct term *head, *curr, *next;
int i;
float c;

if(nterm <= 0)
head=NULL;
else
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

{ printf("Enter terms of poly in decreasing order of their exponent\n");


/* --create first (head) term-- */
head=(struct term*)malloc(sizeof(struct term));
printf("Enter coeficient & exponent for 1 term:");
scanf("%f%d", &c, &head->expo);
head->coef=c;
curr=head;
for(i=1; i<nterm; i++)
{ /* --Create rest of the n-1 terms-- */
next=(struct term*)malloc(sizeof(struct term));
printf("enter coeficient and exponent for %d node:",i+1);
scanf("%f%d", &c, &next->expo);
next->coef=c;
curr->link=next;
curr=next;
}
curr->link=NULL;
}
return(head);
}

7.2. Printing of polynomial


Printing a polynomial is similar to traversing in a linked list of n nodes, where n is number of terms in polynomial.
void prnPoly(struct term *poly)
{
while(poly != NULL)
{
if(poly->coef > 0)
printf("+");
printf("%gx^", poly->coef);
printf("%d ", poly->expo);
poly=poly->link;
}
printf("\n");
}

7.3Addition of two polynomials


Following example shows addition of two polynomials.
p A ( x )  6.0 x 6  7.5 x 4  8.0 x  9.7
pB ( x )  5.2 x 5  1.5 x 4  2.3x 2

pC ( x )  p A ( x )  pB ( x )  6.0 x 6  5.2 x 5  6.0 x 4  2.3x 2  8.0 x  9.7
While performing addition, for terms with same exponent in both the polynomials we do addition of their coefficients. If this sum
comes out to be non-zero then term with this sum & exponent is added to a resultant polynomial. Terms with distinct (not common in
both polynomials) exponent are added to resultant polynomial as it is. When polynomials are represented using linked list, the nodes
in the list are arranged in increasing or decreasing order of their exponent. During addition operation each polynomial is traversed
using pointer. There are three situations
1. Terms available in both the polynomials for processing (addition)

2. Terms available in first polynomial and second polynomial is processed completely

3. Terms available in second polynomial and first polynomial is processed completely

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

Example: Addition of two polynomials


1. Two polynomials are polyA and polyB. Let pa & pb are pointers moving in polyA & polyB respectively.
After additions resultant polynomial is polyC. Create head node for polyC and let pc is a pointer always
pointing to last node added in polyC.

polyA 6.0 6 -7.5 4 8.0 1 9.7 0 N


pb
pa
polyB 5.2 5 1.5 4 2.3 2 N

polyC pc

2. Exponent of term pointed by pa is greater than exponent of term pointed by pb hence add term pointed by
pa to resultant polynomial polyC. Move pointer pa to next term in polyA and let pc points to the last term
added to polyC.

polyA 6.0 6 -7.5 4 8.0 1 9.7 0 N


pb
pa pa
polyB 5.2 5 1.5 4 2.3 2 N

polyC 6.0 6 pc

3. Exponent of term pointed by pb is greater than exponent of term pointed by pa hence add term pointed by
pb to resultant polynomial polyC. Move pointer pb to next term in polyB and let pc points to the last term
added to polyC.

polyA 6.0 6 -7.5 4 8.0 1 9.7 0 N


pb pb
pa
polyB 5.2 5 1.5 4 2.3 2 N

polyC 6.0 6 5.2 5 pc

4. Exponent of terms pointed by pa & pb are same hence also sum of their coefficients is non-zero. Add new
node in polyC that contains addition of coefficients and exponent. Move pointers pa & pb to next term in
polyA & polyB respectively and let pc points to last term added to polyC.

polyA 6.0 6 -7.5 4 8.0 1 9.7 0 N


pb pb
pa pa
polyB 5.2 5 1.5 4 2.3 2 N

polyC 6.0 6 5.2 5 -6.0 4 pc

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

5. Exponent of term pointed by pb is greater than exponent of term pointed by pa hence add term pointed by
pb to resultant polynomial polyC. Move pointer pb to next term in polyB and let pc points to the last term
added to polyC.

polyA 6.0 6 -7.5 4 8.0 1 9.7 0 N


pb
pa pb

polyB 5.2 5 1.5 4 2.3 2 N NULL

polyC 6.0 6 5.2 5 -6.0 4

2.3 2 pc

6. Now pb is NULL but pa is not NULL, it means there are un-processed terms in polyA where as polyB is
processed completely. Hence copy remaining terms of polyA to polyC.

polyA 6.0 6 -7.5 4 8.0 1 9.7 0 N

pa pb NULL

polyB 5.2 5 1.5 4 2.3 2 N NULL pa

polyC 6.0 6 5.2 5 -6.0 4

2.3 2 8.0 1 9.7 0 N pc

7. Remove the extra head node from the resultant polynomial polyC.

polyC 6.0 6 5.2 5 -6.0 4

2.3 2 8.0 1 9.7 0 N pc

polyC 6.0 6 5.2 5 -6.0 4

2.3 2 8.0 1 9.7 0 N

struct term * polyAdd(struct term *polyA, struct term *polyB)


{
struct term *pa, *pb, *polyC,*pc, *temp;
float c;

if (polyA == NULL)
return(polyB);
else
{
if (polyB == NULL)
return(polyA);

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

else
{
pa=polyA;
pb=polyB;
/* --create head node-- */
polyC=(struct term*)malloc(sizeof(struct term));
pc=polyC;
while(pa != NULL && pb != NULL)
{
if(pa->expo > pb->expo)
{
temp=(struct term *)malloc(sizeof(struct term));
temp->coef=pa->coef;
temp->expo=pa->expo;
pc->link=temp;
pc=temp;
pa=pa->link;
}
else
{
if(pa->expo < pb->expo)
{
temp=(struct term *)malloc(sizeof(struct term));
temp->coef=pa->coef;
temp->expo=pa->expo;
pc->link=temp;
pc=temp;
pb=pb->link;
}
else
{
c=pa->coef+pb->coef;
if(c != 0)
{
temp=(struct term *)malloc(sizeof(struct term));
temp->coef=c;
temp->expo=pa->expo;
pc->link=temp;
pc=temp;
}
pa=pa->link;
pb=pb->link;
}
}
}
while(pa != NULL)
{
temp=(struct term *)malloc(sizeof(struct term));
temp->coef=pa->coef;
temp->expo=pa->expo;
pc->link=temp;
pc=temp;
pa=pa->link;
}
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

while(pb != NULL)
{
temp=(struct term *)malloc(sizeof(struct term));
temp->coef=pb->coef;
temp->expo=pb->expo;
pc->link=temp;
pc=temp;
pb=pb->link;
}
pc->link=NULL; /* --mark it as a last node-- */
/* --delete head node-- */
pc=polyC;
polyC=polyC->link;
free(pc);
return(polyC);
}
}
}

Program: Representation of Polynomial using singly linked list


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct term
{
float coef;
int expo;
struct term *link;
};

void main()
{
struct term *polyA, *polyB, *polyC;
int n;

printf("Enter number of terms in polynomial A:");


scanf("%d", &n);
polyA=readPoly(n);

printf("Enter number of terms in polynomial B:");


scanf("%d", &n);
polyB=readPoly(n);

ployC=polyAdd(polyA, polyB);

printf("Polynomial A:\n");
prnPoly(polyA);

printf("Polynomial B:\n");
prnPoly(polyB);

printf("Polynomial C:\n");
prnPoly(polyC);
}

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

Exercise:
1. Given a polynomial of a single variable represented as a linked list. Write function to differentiate the polynomial. Do not
create another list.
2. Write function to subtract a polynomial from another polynomial.
3. Write a function to evaluate polynomial at x=x0.
double evalPoly(struct term *poly, float x0)
{
double res=0;
while(poly != NULL)
{
Res=res + poly->coef * pow (x0, poly->expo);
poly=poly->link;
}
return(res);
}

4. Write function to integrate polynomial.

8. Doubly Linked List


In singly linked list nodes are linked together by keeping address of next node in the link field of current node (or by keeping address
of previous node in the link field of current node). This linking mechanism permits to travel in forward direction only (or backward
direction only). There are some applications where moving in either direction is often necessary then doubly linked list allows to do
this by keeping address of previous as well as next node in the node. Each node has two link fields, namely llink and rlink where llink
stores address of previous node and rlink stores address of next node. The data field in node keeps information about item in the list.
llink data rlin
k
struct dnode
{
int data;
struct dnode *llink;
struct dnode *rlink;
};
struct dnode *head;

Below shown is a doubly linked list of 4 nodes.

head N 6 7 8 9 N

8.1. Creation and traversal in Doubly Linked List


Figure 3.10 shows flow chart of creation on doubly linked list of n nodes.

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

n
== 0 >0

Create empty list: Create first node:


 Mark head as NULL  Create a node, put data into it
to make empty list;  Make this node a head (or first) node of the list
 Since there is no node previous to it hence mark llink as
NULL
Create n-1 nodes iteratively and connect them properly:
Use loop that run (n-1) time to do following things
 Create a new node, put data into it
 Let the rlink field of previously created node takes the
address of this new node and llink of this node stores
address of previous node

Mark last node:


 Since there is no node next to last node created hence
set rlink of last to NULL
Figure 3.10. Flow chart of creation on doubly linked list of n nodes

void createDLL(int n)
{
struct dnode *curr, *next;
int i;

if(n<=0)
head=NULL;
else
{ /* --create first (head) node-- */
head=(struct dnode*)malloc(sizeof(struct dnode));
printf("Enter data for 1 node:");
scanf("%d",&head->data);
head->llink=NULL;
curr=head;
for(i=1; i<n; i++)
{ /* --Create rest of the n-1 nodes-- */
next=(struct dnode*)malloc(sizeof(struct dnode));
printf("enter data for %d node:",i+1);
scanf("%d", &next->data);
curr->rlink=next;
next->llink=curr;
curr=next;
}
curr->rlink=NULL;
}
}

void traverseDLL()
{

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

struct dnode *temp;

printf("Traversing in the Doubly linked list:\n");


if(head == NULL)
printf("list is empty\n");
else
{
temp=head;
while(temp->rlink != NULL)
{
printf("%d\t", temp->data);
temp=temp->rlink;
}
printf("%d\n", temp->data);
while(temp != NULL)
{
printf("%d\t",temp->data);
temp=temp->llink;
}
printf("\n");
}
}

8.2. Insert New node in a doubly linked list


In order to insert new node in a doubly linked list, first create a new node and add data to it.
temp=(struct dnode *) malloc(sizeof(struct dnode));
scanf("%d",&temp->data);
There are following possible cases:
Case 1: Insert node in the empty list
temp->llink=NULL;
temp->rlink=NULL;
head=temp;
head NULL temp 5 head N 5 N

Case 2: Insert node at the beginning


Insert node temp before head
temp->rlink=head;
temp->llink=NULL;
head->llink=temp;
head = temp;

temp 5

head N 6 7 8 N

temp N 5
6 7 8 N
head
head

Case 3: Insert node in the middle


By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

Insert node temp between the nodes prev and curr.


prev->rlink=temp;
temp->llink=prev;
curr->llink=temp;
temp->rlink=curr;

temp 5

head N 6 7 8 9 N

prev curr

temp 5

head N 6 7 8 9 N

prev curr

Case 4: Insert node at the end:


Insert node temp after last node prev
prev->rlink=temp;
temp->llink=prev;
temp->rlink=NULL;

temp 5

head N 6 7 8 N

prev

temp 5 N

head N 6 7 8

prev

void insDLL( unsigned int pos)


{
struct dnode *temp, *curr, *prev;
int c=0;

printf("Enter data to be insertion:");


temp=(struct dnode *) malloc(sizeof(struct dnode));
scanf("%d",&temp->data);
if (head ==NULL) /* --list is empty-- */
{
temp->llink=NULL;
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

temp->rlink=NULL;
head=temp;
}
else
{
if (pos == 1) /* --insert at first position-- */
{
temp->rlink=head;
temp->llink=NULL;
head->llink=temp;
head = temp;
}
else
{
c=1;
curr=head;
while (c < pos && curr != NULL)
{
c++;
prev=curr;
curr=curr->rlink;
}
if (curr == NULL) /* --insert node at the end-- */
{
prev->rlink=temp;
temp->llink=prev;
temp->rlink=NULL;
}
else /* --insert node in between-- */
{
prev->rlink=temp;
temp->llink=prev;
curr->llink=temp;
temp->rlink=curr;
}
}
}
}

8.3. Delete a node from a doubly linked list


There are following possible cases:
Case 1: Delete first node:
curr= head;
head=head->rlink;
head->llink=NULL;
free(curr);

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

head N 6 7 8 9 N

head
curr

head N 6 N 7 8 9 N

Case 2: Delete node in between or at the end:


delete node pointed by curr.
next=curr->rlink;
prev->rlink=next;
if (next != NULL)
next->llink=prev;
free(curr);

head N 6 7 8 9 N

prev curr

head N 6 7 8 9 N

prev curr next

void delDLL(unsigned int pos)


{
struct dnode *prev, *curr, *next;
int c=0;

if (head ==NULL) /* --list is empty-- */


printf(" List is empty\n");
else
{
if (pos == 1) /* --delete node at first position-- */
{
curr=head;
head= head->rlink;
If (head != NULL)
(head->llink=NULL;
free(curr);
}
else
{
c=1;
curr=head;
while (c < pos && curr !=NULL)
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

{
c++;
prev=curr;
curr=curr->rlink;
}
if (curr == NULL)
printf("ERROR: No node on this position\n");
else /* --delete node in between-- */
{

next=curr->rlink;
prev->rlink=next;
if (next != NULL) /* --node is not a last node-- */
next->llink=prev;
free(curr);
}
}
}
}

void main()
{
int n,ch,pos;

clrscr();
while(1)
{
printf("DOUBLY LINLKED LIST\n");
printf("1.Create\n2.Traverse\n3.Insert\n4.Delete\n5.Quit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter No of nodes:");
scanf("%d",&n);
createDLL(n);
break;
case 2:
if (head == NULL)
printf("List is EMPTY\n");
else
traverseDLL();
getch();
break;
case 3:
printf("Enter position:");
scanf("%d", &pos);
insDLL(, pos);
break;
case 4:
printf("Enter position:");
scanf("%d", &pos);
delDLL( pos);

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

break;
case 5:
return;
default:printf("Wrong choice:re-enter\n");
}
}
}

Exercise:
1. The concatenation of two link lists is to be performed in O(1) time. Which of the following implementations of a list shall be
used?
(a) Single link list (b) doubly link list
(c) Circular link list (d) array implementation of lists
Code:
end1=list1->llink;
end2=list2->llink;
list2->llink=end1;
end1->rlink=list2;
list1->llink=end2;
end2->rlink=list1;
2. Write a function to generate a doubly linked list from a given singly linked list.

3. Write a function to remove all occurrences of value m from a doubly linked list

8.4. Circular Doubly Linked List


In Doubly linked list it is possible to reach from first node to last node or last node to first node. Both these traversals need O(n) time
if number of nodes in the list is n. Some applications demand fast movement from first node to last node or last node to first node.
Circular doubly linked list offers that facility by keeping address of first node in last node and address of last node in first node. Now
there are no NULL fields.

head 6 7 8 9

Creation and
traversal in Circular Doubly Linked List
Figure shows flow chart of creation on doubly linked list of n nodes.

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

n
== 0 >0

Create empty list: Create first node:


 Mark head as NULL  Create a node, put data into it
to make empty list;  Make this node a head (or first) node of the list

Create n-1 nodes iteratively and connect them properly:


Use loop that run (n-1) time to do following things
 Create a new node, put data into it
 Let the rlink field of previously created node takes the
address of this new node and llink of this node stores
address of previous node

Mark last node:


 Since there is no node next to last node created hence
set rlink of last to point to first node in the list.
 Let llink of first node points to the last node.

Figure. Flow chart of creation on circular doubly linked list of n nodes

void createDLL(int n)
{
struct dnode *curr, *next;
int i;

if(n<=0)
head=NULL;
else
{ /* --create first (head) node-- */
head=(struct dnode*)malloc(sizeof(struct dnode));
printf("Enter data for 1 node:");
scanf("%d",&head->data);
curr=head;
for(i=1; i<n; i++)
{ /* --Create rest of the n-1 nodes-- */
next=(struct dnode*)malloc(sizeof(struct dnode));
printf("enter data for %d node:",i+1);
scanf("%d", &next->data);
curr->rlink=next;
next->llink=curr;
curr=next;
}
curr->rlink=head;
head->llink=curr;
}
}

void traverseDLL()
{
struct dnode *temp;

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

printf("Traversing in the Doubly linked list:\n");


if(head == NULL)
printf("list is empty\n");
else
{
temp=head;
while(temp->rlink != head)
{
printf("%d\t", temp->data);
temp=temp->rlink;
}
printf("%d\n", temp->data);
do {
printf("%d\t",temp->data);
temp=temp->llink;
} while(temp != head)
printf("\n");
}
}

9. Generalized List
A linear list was defined to be a finite sequence of n≥0 elements a 1, a2, …..an, which we write as A=( a1, a2, …..an). the elements of a
linear list are restricted to be atoms and thus the only structural property a linear list has is the one of position, i.e. a i precedes ai+1, 1≤ i
≤n. it is sometimes useful to relax this restriction on the elements of a list, permitting them to have a structure of their own.
A generalized list, A is a finite sequence if n≥0 elements, a1, a2, …..an, where ai are either atoms or lists. The elements ai, 1≤ i ≤n
which are not atoms are said to be the sublists of A.
A list A itself is written as A= ( a1, a2, …..an). A is the name of the list ( a1, a2, …..an) and n its length. If n ≥ 1, then a1 is head of A
while (a2, …..an) is the tail of A. this definition of generalized list is recursive one. Some examples of generalized lists are
(i) D=() the null or empty list; its length is zero
(ii) A = (a, (b, c)) a list with length two; its first element is atom ‘a’ and second element is a linear list
(iii) B = (A, A, ( )) a list of length three; whose first two elements are the list A and the third element the null list
(iv) C = (a, C) a recursive list of length two; C corresponds to infinite list C = (a, (a, (a, (……..)))
Two important consequences of definition are:
1. Lists may be shared by other lists (in example iii)
2. Lists may be recursive ( in example iv)

9.1. Representations and processing of generalized list


Every generalized list can be represented using the node structure

tag=0/1 data/dlink link

Tag is 0 if element is an atom and tag is 1 if element is a list. The representation of generalized list (i) to (iv) are:

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

D NULL

A 0 a 1

0 b 0 c N A = (a, (b, c))

B 1 1 1 N N B = (A, A, ( ))

C a C = (a, C)
0 1 N

struct gnode
{
struct gnode * link ;
int tag ;
union
{
int data ;
struct gnode * dlink ;
}
};

Before writing function to create generalized linked list lets us analyze the function to create singly linked list in which nodes are
added at run time. In fact it a linked list of n nodes but the value of n is decided by the number of nodes created at run time (value of n
is not predefined).
void createSLL()
{
struct node *curr, *temp;
char ch;

while (1)
{
printf("wants to add node in the list:");
ch=getch();
if (ch == 'n' || ch == 'N')
break;
else
{
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&temp->data);
temp->link=NULL;
if (head == NULL)
{
head = temp;
curr = temp;
}
else
{
curr->link=temp;
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

curr=temp;
}
}
}
}

Shown below is a recursive function to create singly linked list of n nodes.


struct node * createSLL()
{
struct node *curr;
char ch;

printf("wants to add node in the list:");


ch=getch();
if (ch == 'n' || ch == 'N')
return NULL;
else
{
curr=(struct node*)malloc(sizeof(struct node));
printf("Enter data for node:");
scanf("%d",&curr->data);
curr->link=createSLL();
return(curr);
}
}
It is observed that in the forward run of recursion only nodes are created and linked are set in the backward run of recursion.
Following is a program to create and traverse in a generalized linked list
Program: to implement Generalized Linked List
#include<stdio.h>
#include<stdlib.h>

struct gnode
{
int tag;
union
{
int data;
struct gnode *dlink;
};
struct gnode *link;
};

struct gnode * createGLL()


{
struct gnode *temp, *curr, *head;
char ch, ch1;

head = NULL;
while (1)
{
printf("wants to add element in the list:");
ch=getch();
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

if (ch == 'n' || ch == 'N')


break;
else
{
temp=(struct gnode*)malloc(sizeof(struct gnode));
temp->link=NULL;

printf("Is Element is List:");


ch1=getch();
if (ch1 == 'n' || ch1 == 'N')
{
// element is an atom
temp->tag=0;
printf("Enter data:");
scanf("%d",&temp->data);
}
else
{
// element is a list
temp->tag=1;
temp->dlink = createGLL();
}
if (head == NULL)
{
head = temp;
curr = temp;
}
else
{
curr->link=temp;
curr=temp;
}
}
}//while
return head;
}

void traverseGLL(struct gnode *h)


{
struct gnode *temp;

if(h != NULL);
{
temp=h;
printf("( ");
while(temp != NULL)
{
if(temp->tag == 0)
printf("%d\t", temp->data);
else
traverseGLL(temp->dlink);
temp=temp->link;
}
printf(")");
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

}
}

void main()
{
int n,ch,pos;
struct gnode *head;

while(1)
{
printf("GENERALIZED LINLKED LIST\n");
printf("1.Create\n2.Traverse\n3.Quit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
head = createGLL();
break;
case 2:
printf("Traversing in the generalised Singly linked list:\n");
traverseGLL(head);
printf("\n");
getch();
break;
case 3:
return;
default:printf("Wrong choice:reenter\n");
}
}
}

Exercise:
1. Write a function to create exact copy of a given generalized linked list.
Solution:
struct gnode * copyGLL (struct gnode * curr)
{
struct gnode * temp ;
if (curr != NULL)
{
temp = ( struct gnode *) malloc (sizeof (struct gnode));
temp->tag= curr->tag;
if (curr->tag == 1)
temp->dlink = copyGLL(curr->dlink);
else
temp->data= curr->data;
temp->link=copyGLL(curr->link);
return(temp);
}
else
return (NULL);
}
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

2. Write a function to check whether given two generalized lists are equal or not?
Solution:
check (struct gnode *f, struct gnode *s)
{
int result = 0 ;

if ( f == NULL & & s == NULL)


return ( 1);
else
if ( f == NULL && s != NULL )
return ( 0);
else
if ( f != NULL && s == NULL )
return (0);
else
if ( f->tag == s->tag )
if (f->tag == 0 )
if (f->data == s->data)
result = check(f->link, s->link);
else
result = check (f->dlink, s->dlink);
return(result);
}
3. Write a function to Compute depth of a generalized linked list
Solution:
depthGLL (struct gnode *curr )
{
int m,n ;
struct gnode *temp ;
if ( curr != NULL)
{
temp = curr ; m = 0 ;
while ( temp != NULL)
{
if(temp->tag == 1)
{
n = depthGLL(temp->dlink);
if ( m < n )
m=n;
}
temp=temp->link;
}
return ( m+1);
}
else
return (0) ;
}

By Dr. M. M. Raghuwanshi, YCCE,Nagpur


Notes On Algorithms and Data Structures

4. Write a function to count number of nodes in a generalized linked list


Solution:
countGLL(struct gnode *curr)
{
int catom =0, clink =0;

if (curr== NULL)
return (0);
else
{
if (curr->tag == 1)
clink=countGLL(curr->dlink);
else
catom=countGLL(curr->link);
}
return (1+catom+clink);
}

5. Write a function to count number of atoms and lists in a given generalized list

6. Write a function that takes an arbitrary list l with no shared sub-lists and invert it and all of its sub-lists. For example if l= (a,
(b, c)) then invert (l) = ((c, b), a).

9.2 Representations of polynomial in multiple variables using generalized list


Let us take applications of Generalized list where the lists being represented are neither shared nor recursive. Let us consider
application to represent polynomial in several variables. one need to define data representation for them.
Let us take example
P(x,y,z) = x9 y3 z2 + 2x8 y3 z2 + 3x8 y2 z2 + x4 y4 z + 6x3 y4 z + 2y z

One can easily think of sequential representation for P(x,y,z) using nodes with fields coef, expx, expy, and expz. But this would means
that polynomial in different number of variables would need a different number of fields that add another complexity to
representation.
The idea of using a generalized list structure with fixed size nodes arises naturally. Let us re-write P(x,y,z) as

 P(x,y,z) = (x9 y3 + 2x8 y3 + (3x8 y2 ) z2 + ( x4 y4 + 6x3 y4 + 2y ) z


 P(x,y,z) = Bz2 + cz

 P(x,y,z) = ( (x9 + 2x8 ) y3 + (3x8 y2 ) z2 + ( ( x4 + 6x3 ) y4 + 2y ) z


 B = Dy3 + Ey2 & C = Fy4 + Gy

Continuing in this way one can see that every polynomial consists of a variable plus coefficient-exponent pairs. Each coefficient is
itself a polynomial (in one less variable) if a single numerical coefficient is regarded as a polynomial in zero variables. One can see
from discussion that every polynomial, regardless of the number of variables in it, can be represented using nodes of the type defined
below

tag=0/1/2 dlink/var/coef expo link

For tag=0 then it is a down link


For tag=1 then field represent variable
For tag=2 then it is a coefficient

struct pgnode
By Dr. M. M. Raghuwanshi, YCCE,Nagpur
Notes On Algorithms and Data Structures

{
int tag ;
union
{
struct pgnode * dlink ;
char var ;
float coeff ;
};
int expo ;
struct pgnode *link ;
}

P(x,y,z) = ( (x9 + 2x8 ) y3 + (3x8 y2 ) z2 + ( ( x4 + 6x3 ) y4 + 2y ) z


P
A
1 z 0 0 2 0 1 N

1 y 0 0 3 0 2 N

1 y 0 0 4 2 2 1 N
1 x 0 2 1 9 2 2 8 N

1 x 0 2 1 4 2 6 3 N

1 x 0 2 3 8 N

Exercise:
1. Represent following polynomial using generalized list
P(x,y,z) = 4x7 y3 z3 + 2y z + 3x8 z3 + 6x5 y4 z + 9x6+8y5 - x9 y3 z3

By Dr. M. M. Raghuwanshi, YCCE,Nagpur

You might also like