Database structure Structures Link list and trees and Recurison complete Adnan abid
Database structure Structures Link list and trees and Recurison complete Database structure Structures Link list and trees and Recurison complete Database structure Structures Link list and trees and Recurison complete Database structure Structures Link list and trees and Recurison complete Database structure Structures Link list and trees and Recurison complete
Database structure Structures Link list and trees and Recurison complete Database structure Structures Link list and trees and Recurison complete Database structure Structures Link list and trees and Recurison complete
Data Structure Introduction
Data Structure Definition
Data Structure Types
Data Structure Characteristics
Need for Data Structure
Stack Definition
Stack Representation
Stack Operations
Stack Algorithm
Program for Stack in C++
Linked List Definition
Linked List Representation
Linked List Operations
Linked List Algorithm
Program for Linked List in C++
Linked List Defination
Linked List Representation
Linked List Operations
Linked List Algorithm
Program for Linked List in C++
Memory management is one of the most fundamental and important aspect for any computer programming language. In the dynamic memory allocation, the memory is allocated to a variable or program at the run time.
Database structure Structures Link list and trees and Recurison complete Adnan abid
Database structure Structures Link list and trees and Recurison complete Database structure Structures Link list and trees and Recurison complete Database structure Structures Link list and trees and Recurison complete Database structure Structures Link list and trees and Recurison complete Database structure Structures Link list and trees and Recurison complete
Database structure Structures Link list and trees and Recurison complete Database structure Structures Link list and trees and Recurison complete Database structure Structures Link list and trees and Recurison complete
Data Structure Introduction
Data Structure Definition
Data Structure Types
Data Structure Characteristics
Need for Data Structure
Stack Definition
Stack Representation
Stack Operations
Stack Algorithm
Program for Stack in C++
Linked List Definition
Linked List Representation
Linked List Operations
Linked List Algorithm
Program for Linked List in C++
Linked List Defination
Linked List Representation
Linked List Operations
Linked List Algorithm
Program for Linked List in C++
Memory management is one of the most fundamental and important aspect for any computer programming language. In the dynamic memory allocation, the memory is allocated to a variable or program at the run time.
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfkarymadelaneyrenne19
Abstract Data Types
(a) Explain briefly what is meant by the term abstract data type (ADT). Give two
reasons why use of ADTs is good programming practice.
(b) Write out a signature, or interface, that defines the operations of a stack ADT.
(c) Consider a string of characters of the form
... (.( ... ).) ...
where ... indicates an arbitrary sequence of characters (except for parentheses),
(.( indicates an arbitrary number (one or more) of opening parentheses, and
similarly ).) indicates an arbitrary number of closing parentheses.
Using only the stack abstraction operations defined above, write pseudocode for
an algorithm that determines, using a stack, whether or not the number of closing
parentheses is the same as the number of opening parentheses.
You may assume the existence of a function read(str,ch) that reads the next character
of string str into ch.
You may also assume that you can invoke a function reportFail, that will cause
termination with failure, and similarly, reportSuccess causes termination with a
success indication.
Further, you may also assume that you can call a function newStack(S) to create
a new empty stack S, and eos(str) that returns false when you reach the end of
the string.
Solution
(a) Explain briefly what is meant by the term abstract data type (ADT). Give two
reasons why use of ADTs is good programming practice.
A data type is a collection of values and a set of operations on those values. That collection and
these operations form a mathematical construct that may be implemented with the use of a
particular hardware or software data structure. The term abstract data type (ADT) refers to the
basic mathematical concept that defines the data type. We have discussed four different
implementations of the list data structure.
In case of implementation of the list with the use of an array, the size of the array gives difficulty
if increased.
To avoid this, we allocate memory dynamically for nodes before connecting these nodes with the
help of pointers.
For this purpose, we made a singly linked list and connected it with the next pointer to make a
chain.
Moving forward is easy but going back is a difficult task.
To overcome this problem, we made a doubly linked list using prev andnext pointers. With the
help of these pointers, we can move forward and backward very easily. Now we face another
problem that the prev pointer of first node and the next pointer of the last node are NULL.
Therefore, we have to be careful in case of NULL pointers. To remove the NULL pointers, we
made the circular link list by connecting the first and last node.
The program employing the list data structure is not concerned with its implementation.
We do not care how the list is being implemented whether through an array, singly linked list,
doubly linked list or circular linked list. It has been witnessed that in these four implementations
of the list, the interface remained the same i.e. it implements the same methods like add, get,
next, start a.
An array consists of a set of objects (called its elements), all of which are of the same type and are arranged contiguously in memory. In general, only the array itself has a symbolic name, not its elements. Each element is identified by an index which denotes the position of the element in the array. The number of elements in an array is called its dimension. The dimension of an array is fixed and predetermined; it cannot be changed during program execution.
Arrays are suitable for representing composite data which consist of many similar, individual items. Examples include: a list of names, a table of world cities and their current temperatures, or the monthly transactions for a bank account.
A pointer is simply the address of an object in memory. Generally, objects can be accessed in two ways: directly by their symbolic name, or indirectly through a pointer. The act of getting to an object via a pointer to it, is called dereferencing the pointer. Pointer variables are defined to point to objects of a specific type so that when the pointer is dereferenced, a typed object is obtained.
Pointers are useful for creating dynamic objects during program execution. Unlike normal (global and local) objects which are allocated storage on the runtime stack, a dynamic object is allocated memory from a different storage area called the heap. Dynamic objects do not obey the normal scope rules.
Linked List Static and Dynamic Memory AllocationProf Ansari
Static variables are declared and named while writing the program. (Space for them exists as long as the program, in which they are declared, is running.) Static variables cannot be created or destroyed during execution of the program in which they are declared.
Dynamic variables are created (and may be destroyed) during program execution since dynamic variables do not exist while the program is compiled, but only when it is run, they cannot be assigned names while it is being written. The only way to access dynamic variables is by using pointers. Once it is created, however, a dynamic variable does contain data and must have a type like any other variable. If a dynamic variable is created in a function, then it can continue to exist even after the function terminates.
Linked Linear List
We saw in previous chapters how static representation of linear ordered list through Array leads to wastage of memory and in some cases overflows. Now we don't want to assign memory to any linear list in advance instead we want to allocate memory to elements as they are inserted in list. This requires Dynamic Allocation of memory and it can be achieved by using malloc() or calloc() function.
But memory assigned to elements will not be contiguous, which is a requirement for linear ordered list, and was provided by array representation. How we could achieve this?
Cosmetic shop management system project report.pdfKamal Acharya
Buying new cosmetic products is difficult. It can even be scary for those who have sensitive skin and are prone to skin trouble. The information needed to alleviate this problem is on the back of each product, but it's thought to interpret those ingredient lists unless you have a background in chemistry.
Instead of buying and hoping for the best, we can use data science to help us predict which products may be good fits for us. It includes various function programs to do the above mentioned tasks.
Data file handling has been effectively used in the program.
The automated cosmetic shop management system should deal with the automation of general workflow and administration process of the shop. The main processes of the system focus on customer's request where the system is able to search the most appropriate products and deliver it to the customers. It should help the employees to quickly identify the list of cosmetic product that have reached the minimum quantity and also keep a track of expired date for each cosmetic product. It should help the employees to find the rack number in which the product is placed.It is also Faster and more efficient way.
Courier management system project report.pdfKamal Acharya
It is now-a-days very important for the people to send or receive articles like imported furniture, electronic items, gifts, business goods and the like. People depend vastly on different transport systems which mostly use the manual way of receiving and delivering the articles. There is no way to track the articles till they are received and there is no way to let the customer know what happened in transit, once he booked some articles. In such a situation, we need a system which completely computerizes the cargo activities including time to time tracking of the articles sent. This need is fulfilled by Courier Management System software which is online software for the cargo management people that enables them to receive the goods from a source and send them to a required destination and track their status from time to time.
Final project report on grocery store management system..pdfKamal Acharya
In today’s fast-changing business environment, it’s extremely important to be able to respond to client needs in the most effective and timely manner. If your customers wish to see your business online and have instant access to your products or services.
Online Grocery Store is an e-commerce website, which retails various grocery products. This project allows viewing various products available enables registered users to purchase desired products instantly using Paytm, UPI payment processor (Instant Pay) and also can place order by using Cash on Delivery (Pay Later) option. This project provides an easy access to Administrators and Managers to view orders placed using Pay Later and Instant Pay options.
In order to develop an e-commerce website, a number of Technologies must be studied and understood. These include multi-tiered architecture, server and client-side scripting techniques, implementation technologies, programming language (such as PHP, HTML, CSS, JavaScript) and MySQL relational databases. This is a project with the objective to develop a basic website where a consumer is provided with a shopping cart website and also to know about the technologies used to develop such a website.
This document will discuss each of the underlying technologies to create and implement an e- commerce website.
Democratizing Fuzzing at Scale by Abhishek Aryaabh.arya
Presented at NUS: Fuzzing and Software Security Summer School 2024
This keynote talks about the democratization of fuzzing at scale, highlighting the collaboration between open source communities, academia, and industry to advance the field of fuzzing. It delves into the history of fuzzing, the development of scalable fuzzing platforms, and the empowerment of community-driven research. The talk will further discuss recent advancements leveraging AI/ML and offer insights into the future evolution of the fuzzing landscape.
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdffxintegritypublishin
Advancements in technology unveil a myriad of electrical and electronic breakthroughs geared towards efficiently harnessing limited resources to meet human energy demands. The optimization of hybrid solar PV panels and pumped hydro energy supply systems plays a pivotal role in utilizing natural resources effectively. This initiative not only benefits humanity but also fosters environmental sustainability. The study investigated the design optimization of these hybrid systems, focusing on understanding solar radiation patterns, identifying geographical influences on solar radiation, formulating a mathematical model for system optimization, and determining the optimal configuration of PV panels and pumped hydro storage. Through a comparative analysis approach and eight weeks of data collection, the study addressed key research questions related to solar radiation patterns and optimal system design. The findings highlighted regions with heightened solar radiation levels, showcasing substantial potential for power generation and emphasizing the system's efficiency. Optimizing system design significantly boosted power generation, promoted renewable energy utilization, and enhanced energy storage capacity. The study underscored the benefits of optimizing hybrid solar PV panels and pumped hydro energy supply systems for sustainable energy usage. Optimizing the design of solar PV panels and pumped hydro energy supply systems as examined across diverse climatic conditions in a developing country, not only enhances power generation but also improves the integration of renewable energy sources and boosts energy storage capacities, particularly beneficial for less economically prosperous regions. Additionally, the study provides valuable insights for advancing energy research in economically viable areas. Recommendations included conducting site-specific assessments, utilizing advanced modeling tools, implementing regular maintenance protocols, and enhancing communication among system components.
Saudi Arabia stands as a titan in the global energy landscape, renowned for its abundant oil and gas resources. It's the largest exporter of petroleum and holds some of the world's most significant reserves. Let's delve into the top 10 oil and gas projects shaping Saudi Arabia's energy future in 2024.
Explore the innovative world of trenchless pipe repair with our comprehensive guide, "The Benefits and Techniques of Trenchless Pipe Repair." This document delves into the modern methods of repairing underground pipes without the need for extensive excavation, highlighting the numerous advantages and the latest techniques used in the industry.
Learn about the cost savings, reduced environmental impact, and minimal disruption associated with trenchless technology. Discover detailed explanations of popular techniques such as pipe bursting, cured-in-place pipe (CIPP) lining, and directional drilling. Understand how these methods can be applied to various types of infrastructure, from residential plumbing to large-scale municipal systems.
Ideal for homeowners, contractors, engineers, and anyone interested in modern plumbing solutions, this guide provides valuable insights into why trenchless pipe repair is becoming the preferred choice for pipe rehabilitation. Stay informed about the latest advancements and best practices in the field.
Water scarcity is the lack of fresh water resources to meet the standard water demand. There are two type of water scarcity. One is physical. The other is economic water scarcity.
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)MdTanvirMahtab2
This presentation is about the working procedure of Shahjalal Fertilizer Company Limited (SFCL). A Govt. owned Company of Bangladesh Chemical Industries Corporation under Ministry of Industries.
Overview of the fundamental roles in Hydropower generation and the components involved in wider Electrical Engineering.
This paper presents the design and construction of hydroelectric dams from the hydrologist’s survey of the valley before construction, all aspects and involved disciplines, fluid dynamics, structural engineering, generation and mains frequency regulation to the very transmission of power through the network in the United Kingdom.
Author: Robbie Edward Sayers
Collaborators and co editors: Charlie Sims and Connor Healey.
(C) 2024 Robbie E. Sayers
Quality defects in TMT Bars, Possible causes and Potential Solutions.PrashantGoswami42
Maintaining high-quality standards in the production of TMT bars is crucial for ensuring structural integrity in construction. Addressing common defects through careful monitoring, standardized processes, and advanced technology can significantly improve the quality of TMT bars. Continuous training and adherence to quality control measures will also play a pivotal role in minimizing these defects.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
06 linked list
1. [ DATA STRUCTURES]
ChapterChapterChapterChapter ---- 06 :06 :06 :06 : ““““Linked ListLinked ListLinked ListLinked List””””
MEMORY ALLOCATIONS IN C
There are two types of memory allocations possible in C:
(a) Static memory allocation (Compile-time allocation using arrays)
(b) Dynamic memory allocation (Run-time allocation using pointers)
Static memory allocation (using Arrays)
In Static memory allocation, the required amount of memory is allocated to the program
elements at the start of the program. Here the memory to be allocated to the variable is fixed and
is determined by the compiler at the compile time (if it is a single integer variable it allocates two
bytes to it, if it is an array of five integer values it allocates ten bytes to it and if it is a single float
type of variable compiler allocates four bytes to it. For example, consider the following
declaration:
int x, y;
float A [5];
When the first statement is encountered, the compiler will allocate two bytes to each
variable x and y of type int. The second statement results into the allocation of 20 bytes to the
array A (5 * 4, where there are five elements and each element of float type takes four bytes).
Note that as there is no bound checking in C for array boundaries, i.e., if you have declared an
array of five elements, as above and by mistake you are intending to read more than five values
in the array A, it will still work without error. For example, you are reading the above array as
follows:
for (i=0; i<10; i++)
{
scanf (“%f”, &A[i]);
}
Though you have declared size of array 5, you are trying to read ten elements. However,
the problem with this is that the extra elements added as a part of this array “A” are not allocated
the consecutive memory location after the five elements, i.e. only the first five elements are stored
in consecutive memory locations and the other elements are stored randomly at any unknown
locations in the memory. Thus during accessing these extra elements would not be made
available to the user, only the first five values can be accessible.
The second problem with static memory allocation is that if you store less number of
elements than the number of elements for which you have declared memory, then the rest of the
memory will be wasted (i.e. it is not made available to other applications and its status is set as
allocated and not free). This leads to the wastage of memory.
2. Dynamic memory allocation (using pointers)
The concept of Dynamic or Run-time memory allocation helps us to overcome this
problem in arrays, as well as allows us to be able to get the required portion of memory at run-
time (or we say as the need arises). This is best suited type of allocation where we do not know
the memory requirement in advance, which is the case with most of real-life problems. In other
words, dynamic memory allocation gives flexibility for programmer. As well as it makes efficient
use of memory by allocating the required amount of memory whenever needed, unlike static
allocation where we declare the amount of memory to be allocated statically.
malloc ( ) function
The malloc( ) function allocates a block of memory in bytes. The user should explicitly give
the block size it requires for the user. The malloc( ) function is like a request to the RAM of the
system to allocate memory, if the request is granted (i.e. if malloc( ) function stays successful in
allocating memory), returns a pointer to the first block of that memory. The type of the pointer it
returns is void, which means that we can assign it any type of pointer. However if the malloc( )
function fails to allocate the required amount of memory, it returns a NULL. The syntax of this
function is as follows:
malloc (number of elements * size of each element);
For example,
int *ptr;
ptr= malloc (10 * size of each element);
where size represents the size of memory required in bytes (i.e. number of contiguous
memory locations to be allocated). But the function malloc ( ) returns a void pointer so a cast
operator is required to change the returned pointer type according to our need, the above
declaration would take the following form:
ptr_var = (type_cast *) malloc (size);
where ptr_var is the name of pointer that holds the starting address of allocated memory
block, type_cast is the data type into which the returned pointer (of type void) is to be converted,
and size specifies the size of allocated memory block in bytes.
For example,
int *ptr;
ptr= (int *) malloc (10 * sizeof(int));
After the execution of this statement, a consecutive memory blocks of 20 bytes (size of
integer is 2 bytes, and we are requesting for 10 elements of type integer, therefore, 10 * 2 = 20
bytes) is allocated to the program. The address of first byte of this block is first converted into int
and then assigned to the pointer ptr. Consider another example for type char,
char *ptr;
ptr = (char *) malloc (10 * sizeof (char));
Similarly, for allocation of memory to a structure variable, the following statements are
required:
struct student
{
3. char name[30];
int roll_no;
float percentage;
};
struct student *st_ptr;
For allocating of memory, the following statement is required:
st_ptr = (struct student *) malloc (sizeof (struct student));
After the execution of this statement, a contiguous block of memory of size 36 bytes (30
bytes for char type name, 2 bytes for integer roll_no and 4 bytes for float percentage) is allocated
to st_ptr;
Note that to check that if the request made through the function malloc ( ) to allocate the
memory is rejected by system RAM (in case if required space is not available), the malloc ( )
function returns a NULL. This can be done as follows:
int *ptr;
ptr = (int *) malloc (5 * sizeof (int));
if (ptr = = NULL)
{
printf (“nThe required amount of memory is not available”);
getch ();
exit (0);
}
free ( ) function
The free ( ) function is used to de-allocate the previously allocated memory using malloc ()
functions. The syntax of this function is:
free (ptr_var);
where ptr_var is the pointer in which the address of the allocated memory block is
assigned. The free function is used to return the allocated memory to the system RAM.
Need for Dynamic Data structures
The simplest one of data structures i.e. an array can be used only when their numbers of
elements along with elements sizes are predetermined, since the memory is reserved before
processing. For this reason, arrays are called static data structures. Now, consider a situation
where exact numbers of elements are not known. In such a case, estimates might go wrong. Also
during processing, i.e., either more memory is required or extra free spaces lie in the array.
Another problem associated with arrays is complexity involved in insertions and deletions of
elements.
Linked lists(Dynamic data structures) overcome the drawbacks of arrays as in linked lists
number of elements need not be predetermined, more memory can be allocated or released
during the processing as and when required, making insertions and deletions much easier and
simpler.
Dynamic memory allocation technique facilitates allocation of memory during the program
execution itself using malloc( ) function as and when required. Dynamic memory allocation also
4. facilitates release of memory using free( ) function, if memory is not required any more. Data
structures like linked lists and trees use this technique for their memory allocation.
LINKED LIST :
“A linked list is a linear collection of data elements, called node pointing to the next
nodes by means of pointers.”
Each node is divided into two parts: the first part containing the information of the element,
and the second part called the link or next pointer containing the address of the next node in the
list.
Start
1000
Info Link Info Link Info Link
10 2000 20 3000 30 0
1000 2000 3000
Node-1 Node-2 Node-3
OPERATIONS ON LINKED LISTS
The basic operations to be performed on the linked lists are as follows :
1. Creation 2. Insertion 3. Deletion 4. Traversing
5. Searching 6. Concatenation 7. Display
Creation:
This operation is used to create a linked list.
Insertion:
This operation is used to insert a new node in the linked list at the specified position. A
new node may be inserted
At the beginning of a linked list
At the end of a linked list
At the specified position in a linked list.
If the list itself is empty, then the new node is inserted as a first node.
Deletion:
This operation is used to delete a node from the linked list. A node may be deleted from
the
Beginning of a linked list
End of a linked list.
Specified position in the linked list.
Traversing:
It is a process of going through all the nodes of a linked list from one end to the other end.
It we start traversing from the very first node towards the last node, it is called forward traversing.
It we start traversing from the very last node towards the first node, it is called reverse traversing.
5. Searching:
If the desired element is found, we signal operation “SUCCESSFULL”. Otherwise, we
signal it as “UNSUCCESSFULL”.
Concatenation:
It is a process of appending (joining) the second list to the end of the first list consisting of
m nodes. When we concatenate two lists, the second list has n nodes, then the concatenated list
will be having (m + n) nodes. The last node of the first linked list is modified so that it is now
pointing to the first node in the second list.
Display:
This operation is used to print each and every node’s information. We access each node
from the beginning (or the specified position) of the list and output the data stored there.
TYPES OF LINKED LISTS
Basically, we can put linked lists into the following four types:
Singly linked list (Linear linked list)
Doubly linked list
Circular linked list
Circular doubly linked list
1. Singly linked list
A singly linked list is one in which all nodes are linked together in some sequential
manner. Hence, it is also called linear linked list. Clearly, it has the beginning and the end. The
problem with this list is that we cannot access the predecessor node (previous node) from the
current node. This can be overcome by doubly linked lists. A linear linked list is shown in Fig. (1).
Start
1000
Info Link Info Link Info Link
10 2000 20 3000 30 0
1000 2000 3000
Node-1 Node-2 Node-3
Fig. (1). A Singly (Linear) Linked list
2. Doubly linked list
A Doubly linked list is one in which all nodes are linked together by multiple links which
help in accessing both the successor node (next node) and predecessor node (previous node) for
any random node within the list. Therefore, each node in a doubly linked list points to the left
node (previous) and the right node (next). This helps to traverse the list in the forward direction
and backward direction. A doubly linked list is shown in Fig. (2).
Start Last
6. 1000 3000
Left Info Right Left Info Right Left Info Right
0 2 2000 1000 4 3000 2000 6 0
1000 2000 3000
Node-1 Node-2 Node-3
Fig. (2). A Doubly Linked list
3. Circular linked list
A circular linked list is one which has no beginning and no end. A singly linked list can be
made circular linked list by simply storing the address of the very first node in the link field of the
last node. A circular linked list is shown in Fig. (3).
Start Last
1000 3000
Info Link Info Link Info Link
2 2000 4 3000 6 1000
1000 2000 3000
Node-1 Node-2 Node-3
Fig. (3). A Circular Linked list
7. 4. Circular doubly linked list
A circular doubly linked list is one which has both the successor pointer and
predecessor pointer in circular manner. A Circular doubly linked list is shown in Fig. (4).
Start Last
1000 3000
Left Info Right Left Info Right Left Info Right
3000 2 2000 1000 4 3000 2000 6 1000
1000 2000 3000
Node-1 Node-2 Node-3
Fig. (4). A Circular Doubly Linked List
OPERATIONS OF SINGLY LINKED LISTS
(A) INSERTION
(I) Algorithm to insert node at the beginning of the singly linked list
Let PTR is the structure pointer which allocates memory for the new node at the
beginning of the singly linked list & NUM is the element to be inserted into the linked list, INFO
represents the information part of the new node and LINK represents the link or next pointer of
the new node pointing to the address of next node. START represents the address of first
node. Initially, before inserting first node in the singly linked list, START=NULL.
Step 1 : Allocate memory for the new node using PTR.
Step 2 : Read NUM to be inserted into singly linked list.
Step 3 : Set PTR->INFO = NUM
Step 4 : Set PTR->LINK=START;
Step 5 : Set START:=PTR
Step 6 : Exit
8. Function to insert node at the beginning of the singly linked list
void insert_beg()
{
struct list *ptr;
int num;
ptr=(list *)malloc(sizeof(list));
printf("nEnter the element to be inserted in singly linked list : ");
scanf("%d",&num);
ptr->info=num;
ptr->link=start;
start=ptr;
}
(II) Algorithm to insert node at the end of the singly linked list
Let PTR is the structure pointer which allocates memory for the new node at the end of
the singly linked list, TEMP is a structure pointer to modify the link part of the previous node &
NUM is the element to be inserted into the linked list, INFO represents the information part of
the new node and LINK represents the link or next pointer of the new node pointing to the
address of next node. START represents the address of first node. Initially, before inserting
first node in the singly linked list, START=NULL.
Step 1 : Allocate memory for the new node using PTR.
Step 2 : Read NUM to be inserted into singly linked list.
Step 3 : Set PTR->INFO = NUM
Step 4 : Set PTR->LINK := NULL
Step 5 : If START = NULL : then
Set START := PTR.
Else
Set TEMP := START;
Repeat while TEMP->LINK !=NULL
Set TEMP := TEMP->LINK.
[End of loop]
Set TEMP-> LINK := PTR.
[End of If Else Structure]
Step 6 : Exit
9. Function to insert node at the end of the singly linked list
void insert_end()
{
struct list *ptr,*temp;
int num;
ptr=(list *)malloc(sizeof(list));
printf("nEnter the element to be inserted in singly linked list : ");
scanf("%d",&num);
ptr->info=num;
ptr->link=NULL;
if (start==NULL)
start=ptr;
else
{
temp=start;
while(temp->link != NULL)
temp=temp->link;
temp->link=ptr;
}
}
(II) Algorithm to insert node at a specific location in the singly linked list
Let PTR is the structure pointer which allocates memory for the new node at a specific
location in a singly linked list, TEMP is a structure pointer to modify the link part of the previous
node & NUM is the element to be inserted into the linked list, LOC is the location of the node to
be inserted, INFO represents the information part of the new node and LINK represents the
link or next pointer of the new node pointing to the address of next node. START represents
the address of first node. Initially, before inserting first node in the singly linked list,
START=NULL, F=0.
Step 1 : Read the location LOC where you want to insert the node.
Step 2 : If LOC > 1 : then
Set TEMP := START.
Repeat loop for I = 1 to LOC – 1
TEMP := TEMP->LINK.
If TEMP := NULL : then
Set F :=1 and Exit from loop.
[End of If Statement]
[End of loop]
If (F=1 OR (START=NULL AND LOC>1)) : then
Write : “Total nodes in list are lesser than this position.”
Write : “So Node cannot be inserted.” and return.
[End of If structure]
[End of Step 2 If Structure]
10. Step 3 : Allocate memory for the new node using PTR.
Step 4 : Read NUM to be inserted into singly linked list.
Step 5 : Set PTR->INFO = NUM
Step 6 : If LOC :=1 : then
Set PTR->LINK := START.
Set START := PTR.
Else
Set PTR->LINK := TEMP->LINK.
TEMP->LINK := PTR.
[End of If Else Structure].
Step 7 : Exit
Function to insert node at specific location in the singly linked list
void insert_spe()
{
struct list *ptr,*temp;
int i,num,loc,f=0;
printf("nEnter the location where you want to store the value : ");
scanf("%d",&loc);
if(loc>1)
{
temp=start;
for(i=1; i<loc-1;i++)
{
temp=temp->link;
if(temp==NULL)
{
f=1;
break;
}
}
if(f==1 || (start==NULL && loc>1))
{
printf("nTotal nodes in the list are lesser than this position. ");
printf("So node cannot be inserted.n");
return;
}
}
ptr=(list *)malloc(sizeof(list));
printf("nEnter the element to be inserted in singly linked list : ");
scanf("%d",&num);
ptr->info=num;
11. if(loc==1)
{
ptr->link=start;
start=ptr;
}
else
{
ptr->link=temp->link;
temp->link=ptr;
}
}
(B) DELETION
(IV) Algorithm to delete node from the beginning of the singly linked list
Let PTR is the structure pointer which deallocates memory of the node at the beginning
of the singly linked list & NUM is the element to be deleted from the linked list, INFO
represents the information part of the deleted node and LINK represents the link or next
pointer of the deleted node pointing to the address of next node. START represents the
address of first node.
Step 1 : If START := NULL : then
Write : “List is empty” and return.
Step 2 : Set PTR := START.
Step 3 : Set NUM := PTR->INFO.
Step 4 : Write : “Deleted element from the beginning of the singly linked list : “,NUM.
Step 5 : Set START := START->LINK.
Step 6 : Deallocate memory of the node at the beginning of singly linked list using PTR.
Step7 : Exit
Function to delete node from the beginning of the singly linked list
void delete_beg()
{
if(start==NULL)
{
printf("nList is empty");
return;
}
struct list *ptr;
int num;
ptr=start;
num=ptr->info;
printf("nThe deleted element from the beginning of singly linked list is : %d",num);
start=start->link;
free(ptr);
}
12. (V) Algorithm to delete node from the end of the singly linked list
Let PTR is the structure pointer which deallocates memory of the node from the end of
the singly linked list, TEMP is a structure pointer to modify the LINK part of previous node &
NUM is the element to be deleted from the linked list, INFO represents the information part of
the deleted node and LINK represents the link or next pointer of the deleted node pointing to
the address of next node. START represents the address of first node.
Step 1 : If START := NULL : then
Write : “List is empty” and return.
Step 2 : If START->LINK := NULL : then
Set PTR := START.
Set START := NULL.
Else
Set TEMP := START.
Set PTR := PTR->LINK.
Repeat loop while PTR->LINK!=NULL
Set TEMP := PTR.
Set PTR := PTR->LINK.
[End of loop]
Set TEMP->LINK := NULL.
[End of Step 2 If Else Structure]
Step 3 : Set NUM := PTR->INFO.
Step 4 : Write : “Deleted element from the end of the singly linked list : “,NUM.
Step 5 : Deallocate memory of the node at the end of singly linked list.
Step 6 : Exit
Function to delete node from the end of the singly linked list
void delete_end()
{
if(start==NULL)
{
printf("nList is empty");
return;
}
struct list *ptr,*temp;
int num;
if(start->link==NULL)
{
ptr=start;
start=NULL;
}
13. else
{
temp=start;
ptr=start->link;
while(ptr->link!=NULL)
{
temp=ptr;
ptr=ptr->link;
}
temp->link=NULL;
}
num=ptr->info;
printf("nThe deleted element from the singly linked list is : %d",num);
free(ptr);
}
(V) Algorithm to delete node from a specific location in the singly linked list
Let PTR is the structure pointer which deallocates memory of the node from a specific
location in the singly linked list, TEMP is a structure pointer to modify the LINK part of previous
node & NUM is the element to be deleted from the linked list, INFO represents the information
part of the deleted node and LINK represents the link or next pointer of the deleted node
pointing to the address of next node. START represents the address of first node.
Step 1 : If START := NULL : then
Write : “List is empty” and return.
Step 2 : Set PTR := START.
Step 3 : Read the location LOC from where you want to delete the node.
Step 4 : If LOC := 1 : then
START := PTR->LINK
Else
Repeat loop for I = 1 to LOC.
Set TEMP := PTR.
Set PTR := PTR->LINK.
If PTR = NULL : then
Write : “Total nodes in the list are lesser than this position”.
Write : “So node cannot be deleted” and return.
[End of If structure]
[End of loop]
Set TEMP->LINK := PTR->LINK.
[End of Step 4 If Else structure]
Step 5 : Set NUM := PTR->INFO.
Step 6 : Write : “Deleted element from the singly linked list : “,NUM.
Step 7 : Deallocate memory of the node at the location LOC of singly linked list.
Step 8 : Exit
14. Function to delete node from a specific location in singly linked list
void delete_spe()
{
if(start==NULL)
{
printf("nList is emptyn");
return;
}
struct list *ptr,*temp;
int num, loc;
ptr=start;
printf("nEnter the location from where you want to delete the value : ");
scanf("%d",&loc);
if(loc==1)
start=ptr->link;
else
{
for(int i=1;i<loc;i++)
{
temp=ptr;
ptr=ptr->link;
if(ptr==NULL)
{
printf("nTotal nodes in the list are lesser than this position. ");
printf("nSo node cannot be deletedn");
return;
}
}
temp->link=ptr->link;
}
num=ptr->info;
printf("nThe deleted element from the singly linked list is : %d",num);
free(ptr);
}
15. PROGRAM – 1 : SINGLY LINKED LIST OPERATIONS
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct list
{
int info;
struct list *link;
};
struct list *start;
void initialize();
void insert_beg();
void insert_end();
void insert_spe();
void traverse();
void delete_beg();
void delete_end();
void delete_spe();
void main()
{
int choice;
initialize();
while(1)
{
clrscr();
printf(" IMPLEMENTATION OF LINKED LIST n");
printf("------------------------------------n");
printf("1. Insertion at the beginning of list n");
printf("2. Insertion at the end of list n");
printf("3. Insertion at the specific location in list n");
printf("4. Deletion from beginning n");
printf("5. Deletion from end n");
printf("6. Deletion from specific location n");
printf("7. Traverse the list n");
printf("8. Exit n");
printf("------------------------------------n");
printf("nEnter your choice [1/2/3/4/5/6/7/8] : ");
scanf("%d",&choice);
16. switch(choice)
{
case 1 : insert_beg();
break;
case 2 : insert_end();
break;
case 3 : insert_spe();
break;
case 4 : delete_beg();
break;
case 5 : delete_end();
break;
case 6 : delete_spe();
break;
case 7 : traverse();
break;
case 8 : exit(0);
default : printf("nYou entered wrong choice. ");
}
getch();
}
}
// Function to initialize Singly linked list
void initialize( )
{
start=NULL;
}
// Function to insert node at the beginning of the singly linked list
void insert_beg()
{
struct list *ptr;
int num;
ptr=(list *)malloc(sizeof(list));
printf("nEnter the element to be inserted in singly linked list : ");
scanf("%d",&num);
ptr->info=num;
ptr->link=start;
start=ptr;
}
17. // Function to insert node at the end of the singly linked list
void insert_end()
{
struct list *ptr,*temp;
int num;
ptr=(list *)malloc(sizeof(list));
printf("nEnter the element to be inserted in singly linked list : ");
scanf("%d",&num);
ptr->info=num;
ptr->link=NULL;
if(start==NULL)
start=ptr;
else
{
temp=start;
while(temp->link!=NULL)
temp=temp->link;
temp->link=ptr;
}
}
// Function to insert node at specific location of the singly linked list
void insert_spe()
{
struct list *ptr,*temp;
int i,num,loc,f=0;
printf("nEnter the location where you want to store the value : ");
scanf("%d",&loc);
if(loc>1)
{
temp=start;
for(i=1;i<loc-1;i++)
{
temp=temp->link;
if(temp==NULL)
{
f=1;
break;
}
}
if(f==1 || (start==NULL && loc>1))
{
printf("nTotal nodes in the list are lesser than this position. ");
printf("So node cannot be inserted.n");
return;
}
}
18. ptr=(list *)malloc(sizeof(list));
printf("nEnter the element to be inserted in singly linked list : ");
scanf("%d",&num);
ptr->info=num;
if(loc==1)
{
ptr->link=start;
start=ptr;
}
else
{
ptr->link=temp->link;
temp->link=ptr;
}
}
// Function to delete node from the beginning of the singly linked list
void delete_beg()
{
if(start==NULL)
{
printf("nList is empty");
return;
}
struct list *ptr;
int num;
ptr=start;
num=ptr->info;
printf("nThe deleted element from the singly linked list is : %d",num);
start=start->link;
free(ptr);
}
// Function to delete node from the end of the singly linked list
void delete_end()
{
if(start==NULL)
{
printf("nList is empty");
return;
}
struct list *ptr,*temp;
int num;
20. return;
}
}
temp->link=ptr->link;
}
num=ptr->info;
printf("nThe deleted element from the singly linked list is : %d",num);
free(ptr);
}
// Function to traverse singly linked list
void traverse()
{
printf("nStart : %d",start);
if(start==NULL)
{
printf("nList is empty");
return;
}
struct list *ptr;
ptr=start;
printf("nTraverse the list : n");
printf("nPTR->INFOtPTR->LINKtPTRn");
while(ptr!=NULL)
{
printf("n%dtt%dtt%d",ptr->info,ptr->link,ptr);
ptr=ptr->link;
}
}
21. OPERATIONS OF CIRCULAR LINKED LISTS
(A) INSERTION
(I) Algorithm to insert node at the beginning of the circular linked list
Let PTR is the structure pointer which allocates memory for the new node at the
beginning of the circular linked list & NUM is the element to be inserted into the linked list,
INFO represents the information part of the new node and LINK represents the link or next
pointer of the new node pointing to the address of next node. START represents the address
of first node & LAST represents the address of the last node. Initially , before inserting first
node in the singly linked list, START=NULL, LAST=NULL.
Step 1 : Allocate memory for the new node using PTR.
Step 2 : Read NUM to be inserted into circular linked list.
Step 3 : Set PTR->INFO = NUM
Step 4 : If START = NULL : then
Set START := LAST := PTR
Set PTR->LINK := PTR.
Else
Set PTR->LINK :=START.
Set START := PTR
Set LAST->LINK := PTR
[End of If Else Structure]
Step 5 : Exit
Function to insert node at the beginning of circular linked list
void insertbeg()
{
struct list *ptr;
int num;
ptr=(struct list *)malloc(sizeof(struct list));
printf("nEnter the element to be inserted at the beginning of circular linked list : ");
scanf("%d",&num);
ptr->info=num;
if(start==NULL)
{
start=last=ptr;
ptr->link=ptr;
}
else
{
ptr->link=start;
start=ptr;
last->link=ptr;
}
}
22. (II) Algorithm to insert node at the end of the circular linked list
Let PTR is the structure pointer which allocates memory for the new node at the
beginning of the circular linked list & NUM is the element to be inserted into the linked list,
INFO represents the information part of the new node and LINK represents the link or next
pointer of the new node pointing to the address of next node. START represents the address
of first node & LAST represents the address of the last node. Initially , before inserting first
node in the singly linked list, START=NULL, LAST=NULL.
Step 1 : Allocate memory for the new node using PTR.
Step 2 : Read NUM to be inserted into circular linked list.
Step 3 : Set PTR->INFO = NUM
Step 4 : If START = NULL : then
Set START := LAST := PTR
Set PTR->LINK := PTR.
Else
Set LAST->LINK :=PTR.
Set LAST := PTR
Set PTR->LINK := START
[End of If Else Structure]
Step 6 : Exit
Function to insert node at the end of the circular linked list
void insertend()
{
struct list *ptr;
int num;
ptr=(struct list *)malloc(sizeof(struct list));
printf("nEnter the element to be inserted at the end of circular linked list : ");
scanf("%d",&num);
ptr->info=num;
if(start==NULL)
{
start=last=ptr;
ptr->link=ptr;
}
else
{
last->link=ptr;
last=ptr;
ptr->link=start;
}
}
23. (B) DELETION
(III) Algorithm to delete node from the beginning of the circular linked list
Let PTR is the structure pointer which deallocates memory of the node at the beginning
of the circular linked list & NUM is the element to be deleted from the linked list, INFO
represents the information part of the deleted node and LINK represents the link or next
pointer of the deleted node pointing to the address of next node. START represents the
address of first node, LAST represents the address of the last node.
Step 1 : If START := NULL : then
Write : “List is empty” and return.
Step 2 : Set PTR := START.
Step 3 : Set NUM := PTR->INFO.
Step 4 : Write : “Deleted element from the beginning of the circular linked list : “,NUM.
Step 5 : If START = LAST : then
Set START := LAST := NULL.
Else
Set START := START->LINK.
Set LAST->LINK := START.
[End of If else Structure]
Step 6 : Deallocate memory of the node at the beginning of circular linked list.
Step7 : Exit
Function to delete node from the beginning of circular linked list
void deletebeg()
{
if(start==NULL)
{
printf("nList is empty");
return;
}
struct list *ptr;
ptr=start;
int num=ptr->info;
printf("nDeleted element from beginning of linked list is : %d",num);
if(start==last)
start=last=NULL;
else
{
start=start->link;
last->link=start;
}
free(ptr);
}
24. (IV) Algorithm to delete node from the end of the circular linked list
Let PTR is the structure pointer which deallocates memory of the node at the end of the
circular linked list, TEMP is a structure pointer to modify the LINK part of previous node & NUM
is the element to be deleted from the linked list, INFO represents the information part of the
deleted node and LINK represents the link or next pointer of the deleted node pointing to the
address of next node. START represents the address of first node, LAST represents the
address of the last node.
Step 1 : If START := NULL : then
Write : “List is empty” and return.
Step 2 : Set PTR := START.
Step 3 : If START = LAST : then
Set START := LAST := NULL.
Else
Repeat loop while PTR->LINK != START
Set TEMP := PTR
Set PTR := PTR->LINK.
[End of loop]
Set TEMP->LINK := PTR->LINK.
Set LAST := TEMP.
[End of Step 3 If Else structure]
Step 4 : Set NUM := PTR->INFO.
Step 5 : Write : “Deleted element from the end of the circular linked list : “,NUM.
Step 6 : Deallocate memory of the node at the beginning of circular linked list.
Step7 : Exit
Function to delete node from the end of circular linked list
void deleteend()
{
if(start==NULL)
{
printf("nList is empty");
return;
}
struct list *ptr,*temp;
int num;
ptr=start;
if(start==last)
start=last=NULL;
26. PROGRAM – 2 : CIRCULAR LINKED LIST OPERATIONS
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct list
{
int info;
struct list *link;
};
struct list *start,*last;
void initialize();
void insertbeg();
void insertend();
void deletebeg();
void deleteend();
void traverse();
void main()
{
int choice;
initialize();
while(1)
{
clrscr();
printf("nIMPLEMENTATION OF A CIRCULAR LINKED LIST");
printf("n----------------------------------------");
printf("n1. Insertion at beginning");
printf("n2. Insertion at end");
printf("n3. Deletion from beginning");
printf("n4. Deletion from end");
printf("n5. Traverse");
printf("n6. Exit");
printf("n----------------------------------------");
printf("nnEnter your choice [1/2/3/4/5] : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : insertbeg();
break;
case 2 : insertend();
break;
case 3 : deletebeg();
break;
27. case 4 : deleteend();
break;
case 5 : traverse();
break;
case 6 : exit(0);
default : printf("nInvalid choice");
}
getch();
}
}
// Function to initialize circular linked list
void initialize()
{
start=last=NULL;
}
// Function to insert node at the beginning of circular linked list
void insertbeg()
{
struct list *ptr;
int num;
ptr=(struct list *)malloc(sizeof(struct list));
printf("nEnter the element to be inserted at the beginning of circular linked list : ");
scanf("%d",&num);
ptr->info=num;
if(start==NULL)
start=last=ptr->link=ptr;
else
{
ptr->link=start;
start=ptr;
last->link=ptr;
}
}
// Function to insert node at the end of the circular linked list
void insertend()
{
struct list *ptr;
int num;
ptr=(struct list *)malloc(sizeof(struct list));
printf("nEnter the element to be inserted at the end of circular linked list : ");
scanf("%d",&num);
ptr->info=num;
28. if(start==NULL)
{
start=last=ptr;
ptr->link=ptr;
}
else
{
last->link=ptr;
last=ptr;
ptr->link=start;
}
}
// Function to delete node from the beginning of circular linked list
void deletebeg()
{
if(start==NULL)
{
printf("nList is empty");
return;
}
struct list *ptr;
int num;
ptr=start;
num=ptr->info;
printf("nDeleted element from beginning of linked list is : %d",num);
if(start==last)
start=last=NULL;
else
{
start=start->link;
last->link=start;
}
free(ptr);
}
// Function to delete node from the end of circular linked list
void deleteend()
{
if(start==NULL)
{
printf("nList is empty");
return;
}
struct list *ptr,*temp;
int num;
30. OPERATIONS OF DOUBLY LINKED LISTS
(A) INSERTION
(I) Algorithm to insert node at the beginning of the doubly linked list
Let PTR is the structure pointer which allocates memory for the new node at the
beginning of the doubly linked list & NUM is the element to be inserted into the linked list, INFO
represents the information part of the new node, LEFT represents the structure pointer of the
new node pointing to the address of previous node and RIGHT represents the structure pointer
of the new node pointing to the address of next node in the list. START represents the address
of first node & LAST represents the address of the last node. Initially , before inserting first
node in the singly linked list, START=NULL, LAST=NULL.
Step 1 : Allocate memory for the new node using PTR.
Step 2 : Read NUM to be inserted into doubly linked list.
Step 3 : Set PTR->INFO = NUM
Step 4 : If START = NULL : then
Set PTR->LEFT := PTR->RIGHT := NULL.
Set START := LAST := PTR
Else
Set PTR->LEFT :=NULL.
Set PTR->RIGHT := START.
Set START->LEFT := PTR
Set START := PTR.
[End of If Else Structure]
Step 5 : Exit
Function to insert node at the beginning of doubly linked list
void insertbeg()
{
struct list *ptr;
int num;
ptr=(struct list *)malloc(sizeof(struct list));
printf("nEnter element to be inserted in doubly linked list : ");
scanf("%d",&num);
ptr->info=num;
if(start==NULL)
{
ptr->left=ptr->right=NULL;
start=last=ptr;
}
31. else
{
ptr->left=NULL;
ptr->right=start;
start->left=ptr;
start=ptr;
}
}
(II) Algorithm to insert node at the end of the doubly linked list
Let PTR is the structure pointer which allocates memory for the new node at the end of
the doubly linked list & NUM is the element to be inserted into the linked list, INFO represents
the information part of the new node, LEFT represents the structure pointer of the new node
pointing to the address of previous node and RIGHT represents the structure pointer of the
new node pointing to the address of next node in the list. START represents the address of
first node & LAST represents the address of the last node. Initially , before inserting first node
in the singly linked list, START=NULL, LAST=NULL.
Step 1 : Allocate memory for the new node using PTR.
Step 2 : Read NUM to be inserted into doubly linked list.
Step 3 : Set PTR->INFO := NUM
Step 4 : Set PTR->RIGHT := NULL
Step 5 : If START = NULL : then
Set PTR->RIGHT := NULL.
Set START := LAST := PTR
Else
Set PTR->LEFT := LAST
Set LAST->RIGHT := PTR
Set LAST := PTR
[End of If Else Structure]
Step 6 : Exit
Function to insert node at the end of doubly linked list
void insertend()
{
struct list *ptr,*temp;
int num;
ptr=(struct list *)malloc(sizeof(struct list));
printf("nEnter element to be inserted in doubly linked list : ");
scanf("%d",&num);
32. ptr->info=num;
ptr->right=NULL;
if(start==NULL)
{
ptr->left=ptr->right=NULL;
start=last=ptr;
}
else
{
ptr->left=last;
last->right=ptr;
last=ptr;
}
}
(B) DELETION
(III) Algorithm to delete node at the beginning of the doubly linked list
Let PTR is the structure pointer which deallocates the memory of the node at the
beginning of the doubly linked list & NUM is the element to be deleted from the linked list,
INFO represents the information part of the node, LEFT represents the structure pointer of the
deleted node pointing to the address of previous node and RIGHT represents the structure
pointer of the deleted node pointing to the address of next node in the list. START represents
the address of first node & LAST represents the address of the last node.
Step 1 : If START := NULL : then
Write : “List is empty” and return.
[End of If Structure]
Step 2 : Set PTR := START.
Step 3 : If START = LAST : then
Set START := LAST := NULL.
Else
Set START := START->RIGHT.
Set START->LEFT := NULL.
[End of If else Structure]
Step 4 : Set NUM := PTR->INFO.
Step 5 : Write : “Deleted element from the beginning of the Doubly linked list : “,NUM.
Step 6 : Deallocate memory of the node at the beginning of Doubly linked list.
Step7 : Exit
33. Function to delete node from the beginning of the doubly linked list
void deletebeg()
{
if(start==NULL)
{
printf("nList is empty");
return;
}
struct list *ptr;
int num;
ptr=start;
if(start==last)
start=last=NULL;
else
{
start=start->right;
start->left=NULL;
}
num=ptr->info;
printf("nDeleted element from the beginning of doubly linked list is : %d",num);
free(ptr);
}
(IV) Algorithm to delete node at the end of the doubly linked list
Let PTR is the structure pointer which deallocates the memory of the node at the end of
the doubly linked list & NUM is the element to be deleted from the linked list, INFO represents
the information part of the node, LEFT represents the structure pointer of the deleted node
pointing to the address of previous node and RIGHT represents the structure pointer of the
deleted node pointing to the address of next node in the list. START represents the address of
first node & LAST represents the address of the last node.
Step 1 : If START := NULL : then
Write : “List is empty” and return.
[End of If Structure]
Step 2 : Set PTR := START.
Step 3 : If START = LAST : then
Set START := LAST := NULL.
Else
Set LAST := LAST->LEFT.
Set LAST->RIGHT := NULL.
[End of If else Structure]
Step 4 : Set NUM := PTR->INFO.
Step 5 : Write : “Deleted element from the end of the Doubly linked list : “,NUM.
Step 6 : Deallocate memory of the node at the end of Doubly linked list.
Step7 : Exit
34. Function to delete node from the end of the doubly linked list
void deleteend()
{
if(start==NULL)
{
printf("nList is empty");
return;
}
struct list *ptr;
int num;
ptr=last;
if(start==last)
start=last=NULL;
else
{
last=last->left;
last->right=NULL;
}
num=ptr->info;
printf("nDeleted element from the end of the doubly linked list is : %d",num);
free(ptr);
}
35. PROGRAM – 3 : DOUBLY LINKED LIST OPERATIONS
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct list
{
int info;
struct list *left,*right;
};
struct list *start=NULL,*last=NULL;
void insertbeg();
void insertend();
void deletebeg();
void deleteend();
void traverse();
void main()
{
int choice;
while(1)
{
clrscr();
printf(" IMPLEMENTATION OF DOUBLY LINKED LIST n");
printf("------------------------------------n");
printf("1. Insertion at Beginningn");
printf("2. Insertion at Endn");
printf("3. Deletion from beginningn");
printf("4. Deletion from endn");
printf("5. Traverse the list n");
printf("6. Exit n");
printf("------------------------------------n");
printf("nEnter your choice [1/2/3/4/5/6] : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : insertbeg();
break;
case 2 : insertend();
break;
case 3 : deletebeg();
break;
36. case 4 : deleteend();
break;
case 5 : traverse();
break;
case 6 : exit(0);
default : printf("nInvalid choice");
}
getch();
}
}
// Function to insert node at the beginning of doubly linked list
void insertbeg()
{
struct list *ptr;
int num;
ptr=(struct list *)malloc(sizeof(struct list));
printf("nEnter element to be inserted in doubly linked list : ");
scanf("%d",&num);
ptr->info=num;
if(start==NULL)
{
ptr->left=ptr->right=NULL;
start=last=ptr;
}
else
{
ptr->left=NULL;
ptr->right=start;
start->left=ptr;
start=ptr;
}
}
// Function to insert node at the end of doubly linked list
void insertend()
{
struct list *ptr,*temp;
int num;
ptr=(struct list *)malloc(sizeof(struct list));
printf("nEnter element to be inserted in doubly linked list : ");
scanf("%d",&num);
ptr->info=num;
ptr->right=NULL;
37. if(start==NULL)
{
ptr->left=ptr->right=NULL;
start=last=ptr;
}
else
{
ptr->left=last;
last->right=ptr;
last=ptr;
}
}
// Function to delete node from the beginning of the doubly linked list
void deletebeg()
{
if(start==NULL)
{
printf("nList is empty");
return;
}
struct list *ptr;
int num;
ptr=start;
if(start==last)
start=last=NULL;
else
{
start=start->right;
start->left=NULL;
}
num=ptr->info;
printf("nDeleted element from the beginning of doubly linked list is : %d",num);
free(ptr);
}
// Function to delete node from the end of the doubly linked list
void deleteend()
{
if(start==NULL)
{
printf("nList is empty");
return;
}
38. struct list *ptr;
int num;
ptr=last;
if(start==last)
start=last=NULL;
else
{
last=last->left;
last->right=NULL;
}
num=ptr->info;
printf("nDeleted element from the end of the doubly linked list is : %d",num);
free(ptr);
}
// Function to traverse the Doubly Linked list
void traverse()
{
printf("nStart : %d",start);
printf("nLast : %d",last);
if(start==NULL)
{
printf("nList is empty");
return;
}
struct list *ptr;
ptr=start;
printf("nTraverse the list : n");
printf("nPTR->LEFTtPTR->INFOtPTR->RIGHTtPTRn");
while(ptr!=NULL)
{
printf("n%dtt%dtt%dtt%d",ptr->left,ptr->info,ptr->right,ptr);
ptr=ptr->right;
}
}
39. OPERATIONS OF CIRCULAR DOUBLY LINKED LISTS
(A) INSERTION
(I) Algorithm to insert node at the beginning of the circular doubly linked list
Let PTR is the structure pointer which allocates memory for the new node at the
beginning of the circular doubly linked list & NUM is the element to be inserted into the circular
doubly linked list, INFO represents the information part of the new node, LEFT represents the
structure pointer of the new node pointing to the address of previous node and RIGHT
represents the structure pointer of the new node pointing to the address of next node in the list.
START represents the address of first node & LAST represents the address of the last node.
Initially , before inserting first node in the circular doubly linked list, START=NULL,
LAST=NULL.
Step 1 : Allocate memory for the new node using PTR.
Step 2 : Read NUM to be inserted into circular doubly linked list.
Step 3 : Set PTR->INFO = NUM
Step 4 : If START = NULL : then
Set PTR->LEFT := PTR->RIGHT := PTR.
Set START := LAST := PTR
Else
Set PTR->LEFT :=LAST.
Set PTR->RIGHT := START.
Set START->LEFT := PTR
Set LAST->RIGHT :=PTR
Set START := PTR.
[End of If Else Structure]
Step 5 : Exit
Function to insert node at the beginning of circular doubly linked list
void insertbeg()
{
struct list *ptr;
int num;
ptr=(struct list *)malloc(sizeof(struct list));
printf("nEnter element to be inserted in circular doubly linked list : ");
scanf("%d",&num);
ptr->info=num;
40. if(start==NULL)
{
ptr->left=ptr->right=ptr;
start=last=ptr;
}
else
{
ptr->left=last;
ptr->right=start;
start->left=ptr;
last->right=ptr;
start=ptr;
}
}
(II) Algorithm to insert node at the end of the circular doubly linked list
Let PTR is the structure pointer which allocates memory for the new node at the end of
the circular doubly linked list & NUM is the element to be inserted into the circular doubly
linked list, INFO represents the information part of the new node, LEFT represents the
structure pointer of the new node pointing to the address of previous node and RIGHT
represents the structure pointer of the new node pointing to the address of next node in the list.
START represents the address of first node & LAST represents the address of the last node.
Initially , before inserting first node in the circular doubly linked list, START=NULL,
LAST=NULL.
Step 1 : Allocate memory for the new node using PTR.
Step 2 : Read NUM to be inserted into circular doubly linked list.
Step 3 : Set PTR->INFO := NUM
Step 4 : If START = NULL : then
Set PTR->LEFT := PTR->RIGHT := PTR.
Set START := LAST := PTR
Else
Set PTR->RIGHT := START.
Set PTR->LEFT := LAST.
Set LAST->RIGHT := PTR
Set START->LEFT :=PTR
Set LAST := PTR.
[End of If Else Structure]
Step 5 : Exit
41. Function to insert node at the end of circular doubly linked list
void insertend()
{
struct list *ptr;
int num;
ptr=(struct list *)malloc(sizeof(struct list));
printf("nEnter element to be inserted in circular doubly linked list : ");
scanf("%d",&num);
ptr->info=num;
if(start==NULL)
{
ptr->left=ptr->right=ptr;
start=last=ptr;
}
else
{
ptr->right=start;
ptr->left=last;
last->right=ptr;
start->left=ptr;
last=ptr;
}
}
(B) DELETION
(III) Algorithm to delete node from the beginning of the circular doubly linked list
Let PTR is the structure pointer which deallocates the memory of the node at the
beginning of the circular doubly linked list & NUM is the element to be deleted from the linked
list, INFO represents the information part of the node, LEFT represents the structure pointer of
the deleted node pointing to the address of previous node and RIGHT represents the structure
pointer of the deleted node pointing to the address of next node in the list. START represents
the address of first node & LAST represents the address of the last node.
Step 1 : If START := NULL : then
Write : “List is empty” and return.
[End of If Structure]
Step 2 : Set PTR := START.
42. Step 3 : If START = LAST : then
Set START := LAST := NULL.
Else
Set START := START->RIGHT.
Set START->LEFT := LAST.
Set LAST->RIGHT := START.
[End of If Else Structure]
Step 4 : Set NUM := PTR->INFO.
Step 5 : Write : “Deleted element from the Circular doubly linked list : “,NUM.
Step 6 : Deallocate memory of the node at the beginning of Circular doubly linked list.
Step7 : Exit
Function to delete node from the beginning of the circular doubly linked list
void deletebeg()
{
if(start==NULL)
{
printf("nList is empty");
return;
}
struct list *ptr;
int num;
ptr=start;
if(start==last)
start=last=NULL;
else
{
start=start->right;
start->left=last;
last->right=start;
}
num=ptr->info;
printf("nDeleted element from the beginning of circular doubly linked list is : %d",num);
free(ptr);
}
43. (IV) Algorithm to delete node from the end of the circular doubly linked list
Let PTR is the structure pointer which deallocates the memory of the node at the end of
the circular doubly linked list & NUM is the element to be deleted from the linked list, INFO
represents the information part of the node, LEFT represents the structure pointer of the
deleted node pointing to the address of previous node and RIGHT represents the structure
pointer of the deleted node pointing to the address of next node in the list. START represents
the address of first node & LAST represents the address of the last node.
Step 1 : If START := NULL : then
Write : “List is empty” and return.
[End of If Structure]
Step 2 : Set PTR := START.
Step 3 : If START = LAST : then
Set START := LAST := NULL.
Else
Set LAST := LAST->LEFT.
Set LAST->RIGHT := START.
Set START->LEFT := LAST
[End of If else Structure]
Step 4 : Set NUM := PTR->INFO.
Step 5 : Write : “Deleted element from the end of the Circular doubly linked list : “,NUM.
Step 6 : Deallocate memory of the node at the end of Circular doubly linked list.
Step7 : Exit
// Function to delete node from the end of the circular doubly linked list
void deleteend()
{
if(start==NULL)
{
printf("nList is empty");
return;
}
struct list *ptr;
int num;
ptr=last;
if(start==last)
start=last=NULL;
else
{
last=last->left;
last->right=start;
start->left=last;
}
num=ptr->info;
printf("nDeleted element from the end of the circular doubly linked list is : %d",num);
free(ptr); }
44. PROGRAM - 4 : CIRCULAR DOUBLY LINKED LIST OPERATIONS
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct list
{
int info;
struct list *left,*right;
};
struct list *start=NULL,*last=NULL;
void insertbeg();
void insertend();
void deletebeg();
void deleteend();
void traverse();
void main()
{
int choice;
while(1)
{
clrscr();
printf(" IMPLEMENTATION OF CIRCULAR DOUBLY LINKED LIST n");
printf("------------------------------------n");
printf("1. Insertion at Beginningn");
printf("2. Insertion at Endn");
printf("3. Deletion from beginningn");
printf("4. Deletion from endn");
printf("5. Traverse the list n");
printf("6. Exit n");
printf("------------------------------------n");
printf("nEnter your choice [1/2/3/4/5/6] : ");
scanf("%d",&choice);
switch(choice)
{
case 1 : insertbeg();
break;
case 2 : insertend();
break;
case 3 : deletebeg();
break;
case 4 : deleteend();
break;
45. case 5 : traverse();
break;
case 6 : exit(0);
default : printf("nInvalid choice");
}
getch();
}
}
// Function to insert node at the beginning of circular doubly linked list
void insertbeg()
{
struct list *ptr;
int num;
ptr=(struct list *)malloc(sizeof(struct list));
printf("nEnter element to be inserted in circular doubly linked list : ");
scanf("%d",&num);
ptr->info=num;
if(start==NULL)
{
ptr->left=ptr->right=ptr;
start=last=ptr;
}
else
{
ptr->left=last;
ptr->right=start;
start->left=ptr;
last->right=ptr;
start=ptr;
}
}
// Function to insert node at the end of circular doubly linked list
void insertend()
{
struct list *ptr;
int num;
ptr=(struct list *)malloc(sizeof(struct list));
printf("nEnter element to be inserted in circular doubly linked list : ");
scanf("%d",&num);
ptr->info=num;
if(start==NULL)
{
ptr->left=ptr->right=ptr;
start=last=ptr;
46. }
else
{
ptr->right=start;
ptr->left=last;
last->right=ptr;
start->left=ptr;
last=ptr;
}
}
// Function to delete node from the beginning of the circular doubly linked list
void deletebeg()
{
if(start==NULL)
{
printf("nList is empty");
return;
}
struct list *ptr;
int num;
ptr=start;
if(start==last)
start=last=NULL;
else
{
start=start->right;
start->left=last;
last->right=start;
}
num=ptr->info;
printf("nDeleted element from the beginning of circular doubly linked list is : %d",num);
free(ptr);
}
// Function to delete node from the end of the circular doubly linked list
void deleteend()
{
if(start==NULL)
{
printf("nList is empty");
return;
}
struct list *ptr;
int num;
ptr=last;
47. if(start==last)
start=last=NULL;
else
{
last=last->left;
last->right=start;
start->left=last;
}
num=ptr->info;
printf("nDeleted element from the end of the circular doubly linked list is : %d",num);
free(ptr);
}
//Function to display the elements of Circular Doubly Linked list
void traverse()
{
printf("nStart : %d",start);
printf("nLast : %d",last);
if(start==NULL)
{
printf("nList is empty");
return;
}
struct list *ptr;
ptr=start;
printf("nTraverse the list : n");
printf("nPTR->LEFTtPTR->INFOtPTR->RIGHTtPTRn");
while(ptr->right!=start)
{
printf("n%dtt%dtt%dtt%d",ptr->left,ptr->info,ptr->right,ptr);
ptr=ptr->right;
}
printf("n%dtt%dtt%dtt%d",ptr->left,ptr->info,ptr->right,ptr);
}