DS-Module2 Ktustudents - in PDF
DS-Module2 Ktustudents - in PDF
All data types which are absolutely defined are called as concrete data types
Abstract data type can be constructed from known –or unknown data.
www.ktustudents.in
For example: Boolean, Integer, Floating point, String are examples of concrete data types as they
are very strictly defined to contain the specified data type values
Array can be an example of an abstract data type as an array can consist of a number of Booleans,
integers, Alphanumeric, Text, or even arrays (A program can (if properly programmed) seek out a
particular cell in an array, and return the data there, no matter what data type.)
Differences are:
The concrete data type is defined for certain inputs and outputs, whereas abstract is defined for
all kind of inputs and outputs.
A concrete data type is rarely reusable, whereas abstract data types are reusable repetitively
Arrays, lists and trees are concrete data types whereas stacks, queues and heaps are abstract
data types.
For implementing abstract data type, we need to choose a suitable concrete data type.
Arrays a kind of data structure that can store a fixed-size sequential collection of elements of
the same type. An array is used to store a collection of data, but it is often more useful to think of an
www.ktustudents.in
array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you
declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to
represent individual variables. A specific element in an array is accessed by an index.
All arrays consist of contiguous memory locations. The lowest address corresponds to the first element
and the highest address to the last element.
Declaring Arrays
To declare an array in C, a programmer specifies the type of the elements and the number of
elements required by an array as follows −
This is called a single-dimensional array. The arraySize must be an integer constant greater than zero
and type can be any valid C data type. For example, to declare a 10-element array called balance of
type double, use this statement −
double balance[10];
Initializing Arrays
You can initialize an array in C either one by one or using a single statement as follows −
The number of values between braces { } cannot be larger than the number of elements that we
declare for the array between square brackets [ ].
If you omit the size of the array, an array just big enough to hold the initialization is created.
Therefore, if you write −
www.ktustudents.in
double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0};
You will create exactly the same array as you did in the previous example. Following is an
example to assign a single element of the array −
balance[4] = 50.0;
The above statement assigns the 5th element in the array with a value of 50.0. All arrays have 0
as the index of their first element which is also called the base index and the last index of an array will
be total size of the array minus 1. Shown below is the pictorial representation of the array we discussed
above −
An element is accessed by indexing the array name. This is done by placing the index of the element
within square brackets after the name of the array. For example −
www.ktustudents.in
The above statement will take the 10th element from the array and assign the value to salary variable.
The following example Shows how to use all the three above mentioned concepts viz. declaration,
assignment, and accessing arrays −
#include <stdio.h>
int main ()
{
int n[ 10 ]; /* n is an array of 10 integers */
int i,j;
/* initialize elements of array n to 0 */
for ( i = 0; i < 10; i++ )
{
n[ i ] = i + 100; /* set element at location i to i + 100 */
}
{
printf("Element[%d] = %d\n", j, n[j] );
}
return 0;
}
When the above code is compiled and executed, it produces the following result −
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109
Write a C Program to insert an element into the beginning of an array
www.ktustudents.in
#include<stdio.h>
#include<conio.h>
int main()
{
int a[10],i,n,p;
printf(“Enter the number of elements\n”);
scanf(“%d”,&n);
printf(“Enter the Elements\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
if(n==10)
{
printf(“Array is full”);
}
else
{
printf(“Enter the element to be inserted”)
scanf(“%d”,&p);
for(i=n-1;i>=0;i--)
{
a[i+1]=a[i];
}
n=n+1
a[0]=p;
printf(“After insertion elements are:\n”);
for(i=0;i<n;i++)
{
printf(“%d\t”,a[i]);
}
}
getch();
}
Write a C Program to insert an element into the end of an array
www.ktustudents.in
#include<stdio.h>
#include<conio.h>
int main()
{
int a[10],i,n,p;
printf(“Enter the number of elements\n”);
scanf(“%d”,&n);
printf(“Enter the Elements\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
if(n==10)
{
printf(“Array is full”);
}
else
{
printf(“Enter the element to be inserted”)
scanf(“%d”,&p);
a[n+1]=p;
n=n+1;
printf(“After insertion elements are:\n”);
for(i=0;i<n;i++)
{
printf(“%d\t”,a[i]);
}
}
getch();
}
Write a C Program to insert an element into the particular position of an array
#include <stdio.h>
int main()
{
www.ktustudents.in
int array[100], position, c, n, value;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the location where you wish to insert an element\n");
scanf("%d", &position);
if(position>=100)
printf(“Array is full”)
else
{
printf("Enter the value to insert\n");
scanf("%d", &value);
array[c+1] = array[c];
array[position-1] = value;
n=n+1;
printf("Resultant array is\n");
www.ktustudents.in
scanf(“%d”,&n);
printf(“Enter the Elements\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
p=a[0];
for(i=1;i<n;i++)
{
a[i-1]=a[i];
}
n=n-1;
printf(“Deleted elements are %d”,p);
if(n==0)
printf(“Array is empty”);
else
{
printf(“After deletion elements are:\n”);
for(i=0;i<n;i++)
{
printf(“%d\t”,a[i]);
}
}
getch();
}
Write a C Program to delete an element from the end of an array
#include<stdio.h>
#include<conio.h>
int main()
{
int a[10],i,n;
printf(“Enter the number of elements\n”);
scanf(“%d”,&n);
printf(“Enter the Elements\n”);
www.ktustudents.in
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
p=a[n];
printf(“Deleted element is %d”,p);
n=n-1;
if(n==0)
printf(“Array is empty”);
else
{
printf(“After deletion elements are:\n”);
for(i=0;i<n;i++)
{
printf(“%d\t”,a[i]);
}
}
getch();
}
Write a C Program to delete an element from the particular position of an array
#include <stdio.h>
int main()
{
int array[100], position, c, n, value;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the location where you wish to delete an element\n");
scanf("%d", &position);
value=array[position-1];
for (c = position; c >n; c--)
array[c-1] = array[c];
www.ktustudents.in
n=n-1;
printf(“Deleted element is %d\n”,value);
if(==0)
printf(“Array is empty”);
else
{
printf("Resultant array is\n");
www.ktustudents.in
All elements in the first column is stored first, then second column and so on. Address of
a[i][j]=storing all elements in the first ( j-1) columns elements+ The number of elements in j-th column
up to i-th row =(j-1)*m+i, where m is the number of rows (Assume base address is 1) If the base
address is M Address of a[i][j]=M+(j-1)*m+i-1
2.2.2. VECTOR
Vector is an array with a dynamic size.
Instead of having a predefined size to the structure it increases, decreases its size as you
add/remove elements from/ to it.
Which together gives as some other advantages as adding elements at a specific index and
removing from a specific index.
Vector is not as fast as the array, but altogether efficient. Operations on a vector offer the same
big O as their counterparts on an array.
Like arrays, vector data is allocated in contiguous memory. This can be done either explicitly
or by adding more data.
In order to do this efficiently, the typical vector implementation grows by doubling its
allocated space (rather than incrementing it) and often has more space allocated to it at any one
time than it needs. This is because reallocating memory can sometimes be an expensive
operation.
APPLICATIONS
1. Stores Elements of Same Data Type
2. Used for Maintaining multiple variable names using single name
3. Can be Used for Sorting Elements
4. Can Perform Matrix Operation
5. Can be Used in CPU Scheduling
6. Can be Used in Recursive Function
7. Can be used to implement abstract data structures like stack, queue etc
If the memory is allocated before the execution of a program, it is fixed and cannot be changed.
We have to adopt an alternative strategy to allocated memory only when it is required. There is a
special data structure called linked list that provides a more flexible storage system and it does not
require the use of array.
Linked lists are special list of some data elements linked to one another. The logical ordering is
represented by having each element pointing to the next element. Each element is called a node, which
www.ktustudents.in
has two parts, INFO part which stores the information and LINK which points to the next element.
Advantages
Linked list have many advantages. Some of the very important advantages are:
Linked Lists are dynamic data structure: That is, they can grow or shrink during the execution
of a program.
Efficient memory utilization: Here, memory is not pre-allocated. Memory is allocated
whenever it is required. And it is deallocated when it is no longer needed.
Insertion and deletions are easier and efficient: Linked lists provide flexibility in inserting
data item at a specified position and deletion of a data item from the given position.
Many complex applications can be easily carried out with linked lists.
Disadvantages
More Memory: If the numbers of fields are more, then more memory space is needed.
Access to an arbitrary data item is little bit cumbersome and also time consuming.
Basic Operations
Insertion − add an element at the beginning of the list.
Display − displaying complete list.
Search − search an element using given key.
A linked list is a non-sequential collection of data items called nodes. These nodes in principles
are structures containing fields. Each node in a linked list has basically two fields.
1. DATA field
www.ktustudents.in
2. NEXT field
The DATA field contains an actual value to be stored and processed. And, the NEXT field
contains the address of the next data item in the linked list. The address used to access a
particular node is known as a pointer. Therefore, the elements in a linked list are ordered not by
their physical placement in memory but their logical links stored as part of the data within the
node itself.
Note that, the link field of the last node contains NULL rather than a valid address. It is
a NULL pointer and indicates the end of the list. External pointer(HEAD) is a pointer to the
very first node in the linked list, it enables us to access the entire linked list.
Basic Operations
Following are the basic operations supported by a list.
www.ktustudents.in
Display – This operation is used to print each and every node‟s information.
Traversing-It is a process of going through all the nodes of a linked list from one end to the
other end. If we start traversing from the very first node towards the last node, it is called
forward traversing. If the desired element is found, we signal operation “SUCCESSFUL”.
Otherwise, we signal it as “UNSUCCESSFUL”.
int data;
struct node *next;
}*start=NULL;
Step 3 : Create Node using Dynamic Memory Allocation
Now we are creating one node dynamically using malloc function.We don‟t have prior
knowledge about number of nodes , so we are calling malloc function to create
node at run time.
new_node=(struct node *)malloc(sizeof(struct node));
www.ktustudents.in
Step 5 : Creating Very First Node
If node created in the above step is very first node then we need to assign it as starting
node. If start is equal to null then we can identify node as first node .
start = NULL
First node has 3 names : new_node,current,start
if(start == NULL) {
start = new_node;
curr = new_node;
}
www.ktustudents.in
5. Linked field of the new_node pointed to start, then set start to new_node.
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *ptr;
};
int main()
{
typedef struct node NODE;
NODE *start=NULL, *temp;
int item;
printf("\n\n\tSingle Linked List ");
temp=(NODE*)malloc((sizeof(NODE)));
printf("Enter the data to be inserted: ");
www.ktustudents.in
scanf("%d", &temp->data);
if(start==NULL)
{
temp->ptr=NULL;
start=temp;
}
else
{
temp->ptr=start;
start=temp;
}
getch();
}
4. Start point to the new_node and set linked field of new_node to NULL
5. Node is to be inserted at Last Position so we need to traverse SLL upto Last Node.
6. Linked field of last node pointed to the new node.
Program
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *ptr;
};
int main()
{
typedef struct node NODE;
NODE *start=NULL, *temp, *p;
int item;
www.ktustudents.in
printf("\n\n\tSingle Linked List ");
printf("\n Insert into end");
temp=(NODE*)malloc((sizeof(NODE)));
printf("Enter the data to be inserted: ");
scanf("%d", &item);
item=temp->data;
if(start==NULL)
{
temp->ptr=NULL;
start=temp;
}
else
{
p=start;
while(p->ptr!=NULL)
{
p=p->ptr;
}
p->ptr=temp;
temp->ptr=NULL;
}
getch();
(a) Create a new node and insert the data into the data field
www.ktustudents.in
(b) p=start
(c) p=p->ptr
(d)new_node->ptr=p->ptr
Program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*ptr;
};
void main()
{
typedef struct node NODE;
NODE *start=NULL,*temp,*p,*t;
int ch,item,pos,i;
printf("\nEnter the number: ");
scanf("%d",&item);
temp=(NODE*)malloc(sizeof(NODE));
temp->data=item;
printf("\nEnter the position: ");
scanf("%d",&pos);
p=start;
for(i=1;i<pos-1;i++)
{
p=p->ptr;
}
temp->ptr=p->ptr;
p->ptr=temp;
getch();
}
www.ktustudents.in
1. If start=NULL, Print „No elements‟
2. Otherwise print elements from start to NULL
if(start==NULL)
printf("\nList is empty");
else
{
printf("\nElements are:");
for(p=start;p!=NULL;p=p->ptr)
printf(" %d",p->data);
}
Delete node at Start/First Position in Singly Linked List
Algorithm
1. If list is empty, deletion is not possible.
2. If the list contain only one element, set external pointer to NULL. Otherwise move the
external pointer point to the second node and delete the first node
www.ktustudents.in
Program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*ptr;
};
void main()
{
typedef struct node NODE;
NODE *start=NULL,*temp;
int item;
if(start==NULL)
printf("\nDeletion is not possible");
else if(start->ptr==NULL)
{
temp=start;
start=NULL;
printf("\nDeleted item is %d",temp->data);
free(temp);
}
else
{
temp=start;
start=start->ptr;
printf("\nDeleted item is %d",temp->data);
free(temp);
}
getch();
}
www.ktustudents.in
Delete node at End/Last Position in Singly Linked List
Algorithm
1. If the list is empty, deletion is not possible
2. If the list contain only one element, set external pointer to NULL
3. Otherwise go on traversing the second last node and set the link field point to NULL
www.ktustudents.in
Program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*ptr;
};
void main()
{
typedef struct node NODE;
NODE *start=NULL,*temp,*p,*t;
int ch,item,pos,i;
if(start==NULL)
printf("\nDeletion is not possible");
else if(start->ptr==NULL)
{
temp=start;
start=NULL;
printf("\nDeleted item is %d",temp->data);
free(temp);
}
else
{
temp=start;
t=start->ptr;
while(t->ptr!=NULL)
{
t=t->ptr;
temp=temp->ptr;
}
temp->ptr=NULL;
printf("\nDeleted item is %d",t->data);
www.ktustudents.in
getch();
}
free(t);
Program
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*ptr;
};
void main()
{
typedef struct node NODE;
www.ktustudents.in
NODE *start=NULL,*temp,*p,*t;
int ch,item,pos,i;
}
getch()
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
}; www.ktustudents.in
struct node*ptr;
void main()
{
typedef struct node NODE;
NODE *start=NULL,*temp,*p,*t;
int ch,item,pos,i;
clrscr();
while(1)
{
printf("\nMENU: \n1.Insert at beginning\n2.Insert at particular position\n3.Insert at end\n4.Delete
from beginning\n5.Delete from particular position\n6.Delete from end\n7.Display\n8.Exit\nEnter your
choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the number: ");
scanf("%d",&item);
temp=(NODE*)malloc(sizeof(NODE));
temp->data=item;
if(start==NULL)
{
temp->ptr=NULL;
start=temp;
}
else
{
temp->ptr=start;
start=temp;
}
break;
case 2:
printf("\nEnter the number: ");
scanf("%d",&item);
temp=(NODE*)malloc(sizeof(NODE));
temp->data=item;
printf("\nEnter the position: ");
scanf("%d",&pos);
p=start;
for(i=1;i<pos-1;i++)
p=p->ptr;
temp->ptr=p->ptr;
p->ptr=temp;
www.ktustudents.in
break;
case 3:
printf("\nEnter the number: ");
scanf("%d",&item);
temp=(NODE*)malloc(sizeof(NODE));
temp->data=item;
temp->ptr=NULL;
if(start==NULL)
start=temp;
else
{
p=start;
while(p->ptr!=NULL)
p=p->ptr;
p->ptr=temp;
}
break;
case 4:
if(start==NULL)
printf("\nDeletion is not possible");
else if(start->ptr==NULL)
{
temp=start;
start=NULL;
printf("\nDeleted item is %d",temp->data);
free(temp);
}
else
{
temp=start;
start=start->ptr;
printf("\nDeleted item is %d",temp->data);
free(temp);
}
break;
case 5:
for(i=1;i<pos-1;i++)
temp=temp->ptr;
t=temp->ptr;
temp->ptr=t->ptr;
printf("\nDeleted item is %d",t->data);
www.ktustudents.in
case 6:
free(t);
break;
if(start==NULL)
printf("\nDeletion is not possible");
else if(start->ptr==NULL)
{
temp=start;
start=NULL;
printf("\nDeleted item is %d",temp->data);
free(temp);
}
else
{
temp=start;
t=start->ptr;
while(t->ptr!=NULL)
{
t=t->ptr;
temp=temp->ptr;
}
temp->ptr=NULL;
www.ktustudents.in
DOUBLY LINKED LIST
A more sophisticated kind of linked list is a doubly-linked list or a two-way linked list. In a
doubly linked list, each node has two links: one pointing to the previous node and one pointing to the
next node.
Example:
head is the external pointer ,used to point starting of the doubly linked list. Tail is used to denote end
of the doubly linked list.
}; www.ktustudents.in
struct node *prev,*next;
head=NULL
www.ktustudents.in
head!=NULL
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct node
{
int data;
struct node *prev,*next;
};
void main()
{
typedef struct node NODE;
NODE *head=NULL,tail=NULL,*temp;
int no;
temp=(NODE*)malloc(sizeof(NODE));
printf("Enter the no: ");
scanf("%d",&no);
temp->data=no;
if(start==NULL)
{
temp->prev=NULL;
temp->next=NULL;
head=tail=temp;
}
else
{
temp->next=head;
head->prev=temp;
temp->prev=NULL;
head=temp;
}
if(head==NULL)
www.ktustudents.in
{
}
printf("No elements");
else
{
printf("\nElements are:");
for(p= head;p!=NULL;p=p->next)
{
printf(" %d",p->data);
}
}
getch();
}
4. Otherwise, tail->next=temp, temp->prev=tail and next pointer of new node set to NULL.
tail point to the new node
head=NULL
www.ktustudents.in
head!=NULL
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct node
{
int data;
struct node *prev,*next;
};
void main()
{
int ch,no;
typedef struct node NODE;
NODE *head=NULL,*tail=NULL,*temp;
temp=(NODE*)malloc(sizeof(NODE));
printf("Enter the no: ");
scanf("%d",&no);
temp->data=no;
if(start==NULL)
{
temp->prev=NULL;
temp->next=NULL;
head=tail=temp;
}
else
{
tail->next=temp;
temp->prev=tail;
temp->next=NULL;
tail=temp;
}
if(head==NULL)
{
printf("No elements");
}
www.ktustudents.in
else
{
printf("\nElements are:");
for(p= head;p!=NULL;p=p->next)
{
printf(" %d",p->data);
}
}
getch();
}
Delete a node at Start/First Position in Doubly Linked List
1. If start=NULL then,
Print deletion is not possible
2. If the list contain only one element, set external pointer to NULL
3. Otherwise move the external pointer point to the second node and delete first node
head=tail=NULL
www.ktustudents.in
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *prev,*next;
};
void main()
{
int no;
typedef struct node NODE;
NODE *head=NULL,*temp;
if(head==NULL)
{
printf("Deletion is not possible");
}
else if(head->next==NULL)
{
temp=start;
head=tail=NULL;
printf("Deleted element is: %d",temp->data);
free(temp);
}
else
{
temp=head;
head=temp->next;
head->prev=NULL;
printf("Deleted element is: %d",temp->data);
free(temp);
}
if(head==NULL)
{
printf("No elements");
}
else
{
printf("\nElements are:");
for(p=head;p!=NULL;p=p->next)
{
www.ktustudents.in
}
}
printf(" %d",p->data);
getch();
}
Delete a node at End/Last Position in Doubly Linked List
1. If the list is empty, deletion is not possible
2. If the list contain only one element, set external pointer to NULL
1. Otherwise go on traversing the last and set next field of second last node to NULL
head=tail=NULL
www.ktustudents.in
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *prev,*next;
};
void main()
{
int no;
typedef struct node NODE;
NODE *head=NULL,tail=NULL,*temp;
if(head==NULL)
{
printf("Deletion is not possible");
}
else if(head->next==NULL)
{
temp=start;
head=tail=NULL;
getch();
}
www.ktustudents.in
To write a program to implement doubly linked list.
1.
2.
Insert at beginning
Insert at end
3. Delete from beginning
4. Delete from end
5. Display
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *prev,*next;
};
void main()
{
int ch,no;
typedef struct node NODE;
NODE *head=NULL,tail=NULL,*temp,*p,*t;
clrscr();
while(1)
{
www.ktustudents.in
break;
case 2:
temp=(NODE*)malloc(sizeof(NODE));
printf("Enter the no: ");
scanf("%d",&no);
temp->data=no;
if(start==NULL)
{
temp->prev=NULL;
temp->next=NULL;
head=tail=temp;
}
else
{
tail->next=temp;
temp->prev=tail;
temp->next=NULL;
tail=temp;
}
break;
case 3:
if(head==NULL)
{
printf("Deletion is not possible");
}
else if(head->next==NULL)
{
temp=start;
head=tail=NULL;
printf("Deleted element is: %d",temp->data);
free(temp);
}
else
{
temp=head;
head=temp->next;
head->prev=NULL;
printf("Deleted element is: %d",temp->data);
free(temp);
}
break;
case 4:
if(head==NULL)
{
printf("Deletion is not possible");
}
www.ktustudents.in
else if(head->next==NULL)
{
temp=start;
head=tail=NULL;
printf("Deleted element is: %d",temp->data);
free(temp);
}
else
{
temp=tail;
tail=tail->prev;
free(temp);
}
break;
case 5:
if(start==NULL)
{
printf("No elements");
}
else
{
printf("\nElements are:");
for(p=head;p!=NULL;p=p->next)
{
printf(" %d",p->data);
}
}
break;
case 6:
exit(0);
}
getch();
}
}
www.ktustudents.in
head and tail nodes in such a linked list,
After insertion
We declare the structure for the circular linked list in the same way as we declare it for the
linear linked lists
www.ktustudents.in
struct node
{
int data;
struct node *next;
};
typedef struct node NODE;
NODE *head=NULL;
NODE *tail=NULL;
------------------------------------------------------------------
NODE *temp;
temp=(struct NODE*)malloc(sizeof(NODE));
printf(“Enter the element to be inserted”)
scanf(“%d”,&item);
temp->data=item;
if(head==NULL)
{
head=tail=temp;
tail->next=head;
}
else
{
temp->next=head;
head=temp;
tail->next=head;
}
Inserting a node at the End
1. Allocate a memory for new node
2. If list is empty then head and tail point to the new node. And linked field of tail pointed to
head.
3. If the list is not empty then
a) Linked field of tail point to the new node
www.ktustudents.in
b) tail point to new node
c) Linked field of tail pointed to head
After insertion
NODE *temp;
temp=(struct NODE*)malloc(sizeof(NODE));
printf(“Enter the element to be inserted”)
scanf(“%d”,&item);
temp->data=item;
if(head==NULL)
{
head=tail=temp;
www.ktustudents.in
}
else
tail->next=head;
{
tail->next=temp;
tail=temp;
tail->next=head;
}
After Deletion
www.ktustudents.in
if(head==NULL) //List is empty
{
printf(Deletion is not possible\n”);
}
else if(head==tail) //List contains only one node
{
free(head);
head=tail=NULL;
}
else
{
temp=head;
head=head->next;
tail->next=head;
free(temp);
}
Delete a node from the beginning
1. If list is empty then print deletion is not possible
2. If the list contain only on element the set head and tail to NULL
3. If the list contain more than one element then
a) „p‟ pointer point to the second last node and temp pointer points to the last node
b) tail move to the p
c) Linked field of tail pointed to head
d) Delete the last node
After Deletion
www.ktustudents.in
head=tail=NULL;
}
else
{
p=head;
while(p->next!=tail)
{
p-p->next;
}
temp=p->next;
tail=p;
tail->next=head;
free(temp);
}
www.ktustudents.in
representation of polynomials, each term is considered as a node. And such a node contains three
fields
Coefficient field
Exponent field
Link field
The coefficient field holds the value of the coefficient of a term and the exponent field contains
the exponent value of the term. And the link field contains the address of the next term in the
polynomial. The polynomial node structure is
Address of the
Coefficient(coeff) Exponent(expo)
next node(next)
Algorithm
Two polynomials can be added. And the steps involved in adding two polynomials are given below
1. Read the number of terms in the first polynomial P
Let us illustrate the way the two polynomials are added. Let p and q be two polynomials having three
www.ktustudents.in
terms each.
P=3x2+2x+7
Q=5x3+2x2+x
These two polynomial can be represented as
www.ktustudents.in
www.ktustudents.in
There is no node in the second polynomial to compare with. So, the last node in the first
polynomial is added to the end of the resultant linked list.
Step 5. Display the resultant linked list. The resultant linked list is pointed to by the pointer R
www.ktustudents.in