A linked list is a linear collection of data elements called nodes that are linked together through pointers. Each node contains a data field for storing information and a pointer to the next node. Linked lists allow efficient insertion and removal of nodes in the middle of the list compared to arrays. Dynamic memory allocation using functions like malloc() and free() allow the number of nodes in a linked list to vary at runtime. Common linked list operations include inserting and removing nodes, searching, and traversing the list.
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdfezzi97
Homework 05 - Linked Lists (C++)
(1) Implement the concepts of a union, and difference as defined noted in Chapter 1 questions 6,
and 8 using a linked list. You should not use arrays, dynamic arrays or the STL vector or STL
list classes.
You will be linking a series of nodes together to implement a single linked list class. Define a
Node as a struct that holds a character (like they did in the book) and a pointer to another Node:
struct Node {
char data;
Node* next;
};
Then define a LinkedList class which has (at a minimum) these member functions:
LinkedList();
~LinkedList();
bool insertAtFront();
bool insertBeforePosition(int index);
bool insertAtBack();
bool deleteAtFront();
bool deleteBeforePosition(int index);
bool deleteAtBack();
Implement each of these functions to provide the correct functionality. These functions return
true if successful in inserting or deleting, otherwise they return false (indicating the operation
was not successful).
Finally, create an overloaded + operator to handle the Union of two linked lists and an
overloaded - operator to handle the Difference of two linked lists.
Because we are dealing with pointers you should have both a LinkedList Constructor and
Destructor. Remember that you do not directly call a Constructor or Destructor Function. The
Destructor is automatically called when the variable loses scope or the program ends.
Remember, that we are dealing with not just one dynamically allocated Node (with the new
operator), but many, so you will have to start at the head of the list and go until the Node points
to nullptr. Then keep deleting the previous Node pointer until there are no Nodes left to delete.
(2) To verify your set class, write a main function that takes two lines of characters from the
input file input.txt and store each line of characters into two separate linked lists. Then using
these two lists, perform the Union and set Difference of the characters from the file, and print the
results to the console.
(3) Please also complete an asymptotic (Big O) analysis of your insertAtFront() and
insertAtBack() member functions. Place this in a file called analysis.txt.
Solution
Here is the complete code for your question. Please do rate the aswer if you are happy with
program. Please read comments in the program. Also pay special attention to comments in + and
- operators. You WILL NEED to comment out if condition in + if you want all elements in both
lists irrespective of if they duplicates or no. For now implemented to have true behaviour of no
duplicates in union.
check the functions by creating some lists and playing around different fucntions like its done in
main and commented out.
As far as complexity:
for insertAtFront() its O(1) since it does not depend on the size of the list. It just replaces the start
of the list. Hence it is O(1) and independent of the size of list.
for insertAtBack(), its O(n) since it depends on the number of elements in the list. It should
traverse till end of list and .
In computer science, a linked list is a linear collection of data elements, in which linear order is not given by their physical placement in memory. Instead, each element points to the next
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdfezzi97
Homework 05 - Linked Lists (C++)
(1) Implement the concepts of a union, and difference as defined noted in Chapter 1 questions 6,
and 8 using a linked list. You should not use arrays, dynamic arrays or the STL vector or STL
list classes.
You will be linking a series of nodes together to implement a single linked list class. Define a
Node as a struct that holds a character (like they did in the book) and a pointer to another Node:
struct Node {
char data;
Node* next;
};
Then define a LinkedList class which has (at a minimum) these member functions:
LinkedList();
~LinkedList();
bool insertAtFront();
bool insertBeforePosition(int index);
bool insertAtBack();
bool deleteAtFront();
bool deleteBeforePosition(int index);
bool deleteAtBack();
Implement each of these functions to provide the correct functionality. These functions return
true if successful in inserting or deleting, otherwise they return false (indicating the operation
was not successful).
Finally, create an overloaded + operator to handle the Union of two linked lists and an
overloaded - operator to handle the Difference of two linked lists.
Because we are dealing with pointers you should have both a LinkedList Constructor and
Destructor. Remember that you do not directly call a Constructor or Destructor Function. The
Destructor is automatically called when the variable loses scope or the program ends.
Remember, that we are dealing with not just one dynamically allocated Node (with the new
operator), but many, so you will have to start at the head of the list and go until the Node points
to nullptr. Then keep deleting the previous Node pointer until there are no Nodes left to delete.
(2) To verify your set class, write a main function that takes two lines of characters from the
input file input.txt and store each line of characters into two separate linked lists. Then using
these two lists, perform the Union and set Difference of the characters from the file, and print the
results to the console.
(3) Please also complete an asymptotic (Big O) analysis of your insertAtFront() and
insertAtBack() member functions. Place this in a file called analysis.txt.
Solution
Here is the complete code for your question. Please do rate the aswer if you are happy with
program. Please read comments in the program. Also pay special attention to comments in + and
- operators. You WILL NEED to comment out if condition in + if you want all elements in both
lists irrespective of if they duplicates or no. For now implemented to have true behaviour of no
duplicates in union.
check the functions by creating some lists and playing around different fucntions like its done in
main and commented out.
As far as complexity:
for insertAtFront() its O(1) since it does not depend on the size of the list. It just replaces the start
of the list. Hence it is O(1) and independent of the size of list.
for insertAtBack(), its O(n) since it depends on the number of elements in the list. It should
traverse till end of list and .
In computer science, a linked list is a linear collection of data elements, in which linear order is not given by their physical placement in memory. Instead, each element points to the next
Write a JAVA LinkedListRec class that has the following methods siz.pdfinfo785431
Write a JAVA LinkedListRec class that has the following methods: size, empty, insertBefore,
insertAfter, addAtHead, addAtEnd, remove, replace, peekFront, peekEnd, removeFront,
removeEnd, toString. Use recursion to implement most of these methods. Write a driver to test
your implemented LinkedListRec class methods.
Solution
import java.util.Scanner;
public class LinkedListRec {
int data;
LinkedListRec prev=null,next=null;
int size(LinkedListRec l)//method which returns the length of the list
{
if(l==null)return 0;
return 1+size(l.next);
}
boolean empty(LinkedListRec l)//method checks whether the list is empty or not...
{
if(l==null)return true;
else return false;
}
LinkedListRec insertBefore(LinkedListRec l,int value,int num_insertbefore)//method which
adds a new number before the given number..
{
if(l==null)return l;
if(l.data == num_insertbefore)
{
LinkedListRec n = new LinkedListRec();
n.data = value;
if(l.prev==null)
{
n.next = l;
l=n;
}
else
{
n.prev = l.prev;
n.next= l;
l.prev = n;
}
return l;
}
return insertBefore(l.next,value,num_insertbefore);
}
LinkedListRec insertAfter(LinkedListRec l,int value,int num_insertafter)//method which adds
the new number after the given number
{
if(l==null)return l;
if(l.data == num_insertafter)
{
LinkedListRec n = new LinkedListRec();
n.data = value;
if(l.next==null)
{
l.next = n;
n.prev = l;
}
else
{
n.next = l.next;
l.next.prev = n;
n.prev = l;
}
return l;
}
return insertAfter(l.next,value,num_insertafter);
}
LinkedListRec addAtHead(LinkedListRec l,int value)//method which adds a new number at
the head position of the list...
{
LinkedListRec n = new LinkedListRec();
n.data=value;
n.next = l;
l=n;
return l;
}
LinkedListRec addAtEnd(LinkedListRec l,int value)//method which adds a new number at
the end position of the list...
{
if(l==null){ l = new LinkedListRec();l.data = value;return l;}
if(l.next==null)
{
LinkedListRec n = new LinkedListRec();
n.data=value;
n.prev = l;
l.next = n;
return l;
}
return addAtEnd(l.next,value);
}
LinkedListRec remove(LinkedListRec l,int value)//method which removes given numbet
from the list....
{
if(l==null)return l;
if(l.data == value)
{
if(l.prev!=null){
l.prev = l.next;
}
else
l=l.next;
return l;
}
return remove(l.next,value);
}
boolean replace(LinkedListRec l,int value,int newvalue)//method which replaces current
number with new number,...
{
if(l==null)return false;
if(l.data == value)
{
l.data = newvalue;
return true;
}
return replace(l.next,value,newvalue);
}
int peekFront(LinkedListRec l)//method which returns the first value of the list...
{
if(l!=null)
return l.data;
else return -1;
}
int peekEnd(LinkedListRec l)//method which returns the last value of the list........
{
if(l==null)return -1;
if(l.next == null)return l.data;
return peekEnd(l.next);
}
LinkedListRec removeFront(LinkedListRec l)//method which removes the first element of the
list..
{
if(l==null)return l;
l=l.next;
return l;
}
boolean removeEnd(LinkedListRec l)//method which removes the last element o.
This presentation takes you on a functional programming journey, it starts from basic Scala programming language design concepts and leads to a concept of Monads, how some of them designed in Scala and what is the purpose of them
Write a JAVA LinkedListRec class that has the following methods siz.pdfinfo785431
Write a JAVA LinkedListRec class that has the following methods: size, empty, insertBefore,
insertAfter, addAtHead, addAtEnd, remove, replace, peekFront, peekEnd, removeFront,
removeEnd, toString. Use recursion to implement most of these methods. Write a driver to test
your implemented LinkedListRec class methods.
Solution
import java.util.Scanner;
public class LinkedListRec {
int data;
LinkedListRec prev=null,next=null;
int size(LinkedListRec l)//method which returns the length of the list
{
if(l==null)return 0;
return 1+size(l.next);
}
boolean empty(LinkedListRec l)//method checks whether the list is empty or not...
{
if(l==null)return true;
else return false;
}
LinkedListRec insertBefore(LinkedListRec l,int value,int num_insertbefore)//method which
adds a new number before the given number..
{
if(l==null)return l;
if(l.data == num_insertbefore)
{
LinkedListRec n = new LinkedListRec();
n.data = value;
if(l.prev==null)
{
n.next = l;
l=n;
}
else
{
n.prev = l.prev;
n.next= l;
l.prev = n;
}
return l;
}
return insertBefore(l.next,value,num_insertbefore);
}
LinkedListRec insertAfter(LinkedListRec l,int value,int num_insertafter)//method which adds
the new number after the given number
{
if(l==null)return l;
if(l.data == num_insertafter)
{
LinkedListRec n = new LinkedListRec();
n.data = value;
if(l.next==null)
{
l.next = n;
n.prev = l;
}
else
{
n.next = l.next;
l.next.prev = n;
n.prev = l;
}
return l;
}
return insertAfter(l.next,value,num_insertafter);
}
LinkedListRec addAtHead(LinkedListRec l,int value)//method which adds a new number at
the head position of the list...
{
LinkedListRec n = new LinkedListRec();
n.data=value;
n.next = l;
l=n;
return l;
}
LinkedListRec addAtEnd(LinkedListRec l,int value)//method which adds a new number at
the end position of the list...
{
if(l==null){ l = new LinkedListRec();l.data = value;return l;}
if(l.next==null)
{
LinkedListRec n = new LinkedListRec();
n.data=value;
n.prev = l;
l.next = n;
return l;
}
return addAtEnd(l.next,value);
}
LinkedListRec remove(LinkedListRec l,int value)//method which removes given numbet
from the list....
{
if(l==null)return l;
if(l.data == value)
{
if(l.prev!=null){
l.prev = l.next;
}
else
l=l.next;
return l;
}
return remove(l.next,value);
}
boolean replace(LinkedListRec l,int value,int newvalue)//method which replaces current
number with new number,...
{
if(l==null)return false;
if(l.data == value)
{
l.data = newvalue;
return true;
}
return replace(l.next,value,newvalue);
}
int peekFront(LinkedListRec l)//method which returns the first value of the list...
{
if(l!=null)
return l.data;
else return -1;
}
int peekEnd(LinkedListRec l)//method which returns the last value of the list........
{
if(l==null)return -1;
if(l.next == null)return l.data;
return peekEnd(l.next);
}
LinkedListRec removeFront(LinkedListRec l)//method which removes the first element of the
list..
{
if(l==null)return l;
l=l.next;
return l;
}
boolean removeEnd(LinkedListRec l)//method which removes the last element o.
This presentation takes you on a functional programming journey, it starts from basic Scala programming language design concepts and leads to a concept of Monads, how some of them designed in Scala and what is the purpose of them
UiPath Test Automation using UiPath Test Suite series, part 3DianaGray10
Welcome to UiPath Test Automation using UiPath Test Suite series part 3. In this session, we will cover desktop automation along with UI automation.
Topics covered:
UI automation Introduction,
UI automation Sample
Desktop automation flow
Pradeep Chinnala, Senior Consultant Automation Developer @WonderBotz and UiPath MVP
Deepak Rai, Automation Practice Lead, Boundaryless Group and UiPath MVP
Transcript: Selling digital books in 2024: Insights from industry leaders - T...BookNet Canada
The publishing industry has been selling digital audiobooks and ebooks for over a decade and has found its groove. What’s changed? What has stayed the same? Where do we go from here? Join a group of leading sales peers from across the industry for a conversation about the lessons learned since the popularization of digital books, best practices, digital book supply chain management, and more.
Link to video recording: https://bnctechforum.ca/sessions/selling-digital-books-in-2024-insights-from-industry-leaders/
Presented by BookNet Canada on May 28, 2024, with support from the Department of Canadian Heritage.
Elevating Tactical DDD Patterns Through Object CalisthenicsDorra BARTAGUIZ
After immersing yourself in the blue book and its red counterpart, attending DDD-focused conferences, and applying tactical patterns, you're left with a crucial question: How do I ensure my design is effective? Tactical patterns within Domain-Driven Design (DDD) serve as guiding principles for creating clear and manageable domain models. However, achieving success with these patterns requires additional guidance. Interestingly, we've observed that a set of constraints initially designed for training purposes remarkably aligns with effective pattern implementation, offering a more ‘mechanical’ approach. Let's explore together how Object Calisthenics can elevate the design of your tactical DDD patterns, offering concrete help for those venturing into DDD for the first time!
Generating a custom Ruby SDK for your web service or Rails API using Smithyg2nightmarescribd
Have you ever wanted a Ruby client API to communicate with your web service? Smithy is a protocol-agnostic language for defining services and SDKs. Smithy Ruby is an implementation of Smithy that generates a Ruby SDK using a Smithy model. In this talk, we will explore Smithy and Smithy Ruby to learn how to generate custom feature-rich SDKs that can communicate with any web service, such as a Rails JSON API.
Essentials of Automations: Optimizing FME Workflows with ParametersSafe Software
Are you looking to streamline your workflows and boost your projects’ efficiency? Do you find yourself searching for ways to add flexibility and control over your FME workflows? If so, you’re in the right place.
Join us for an insightful dive into the world of FME parameters, a critical element in optimizing workflow efficiency. This webinar marks the beginning of our three-part “Essentials of Automation” series. This first webinar is designed to equip you with the knowledge and skills to utilize parameters effectively: enhancing the flexibility, maintainability, and user control of your FME projects.
Here’s what you’ll gain:
- Essentials of FME Parameters: Understand the pivotal role of parameters, including Reader/Writer, Transformer, User, and FME Flow categories. Discover how they are the key to unlocking automation and optimization within your workflows.
- Practical Applications in FME Form: Delve into key user parameter types including choice, connections, and file URLs. Allow users to control how a workflow runs, making your workflows more reusable. Learn to import values and deliver the best user experience for your workflows while enhancing accuracy.
- Optimization Strategies in FME Flow: Explore the creation and strategic deployment of parameters in FME Flow, including the use of deployment and geometry parameters, to maximize workflow efficiency.
- Pro Tips for Success: Gain insights on parameterizing connections and leveraging new features like Conditional Visibility for clarity and simplicity.
We’ll wrap up with a glimpse into future webinars, followed by a Q&A session to address your specific questions surrounding this topic.
Don’t miss this opportunity to elevate your FME expertise and drive your projects to new heights of efficiency.
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Ramesh Iyer
In today's fast-changing business world, Companies that adapt and embrace new ideas often need help to keep up with the competition. However, fostering a culture of innovation takes much work. It takes vision, leadership and willingness to take risks in the right proportion. Sachin Dev Duggal, co-founder of Builder.ai, has perfected the art of this balance, creating a company culture where creativity and growth are nurtured at each stage.
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf91mobiles
91mobiles recently conducted a Smart TV Buyer Insights Survey in which we asked over 3,000 respondents about the TV they own, aspects they look at on a new TV, and their TV buying preferences.
Key Trends Shaping the Future of Infrastructure.pdfCheryl Hung
Keynote at DIGIT West Expo, Glasgow on 29 May 2024.
Cheryl Hung, ochery.com
Sr Director, Infrastructure Ecosystem, Arm.
The key trends across hardware, cloud and open-source; exploring how these areas are likely to mature and develop over the short and long-term, and then considering how organisations can position themselves to adapt and thrive.
Securing your Kubernetes cluster_ a step-by-step guide to success !KatiaHIMEUR1
Today, after several years of existence, an extremely active community and an ultra-dynamic ecosystem, Kubernetes has established itself as the de facto standard in container orchestration. Thanks to a wide range of managed services, it has never been so easy to set up a ready-to-use Kubernetes cluster.
However, this ease of use means that the subject of security in Kubernetes is often left for later, or even neglected. This exposes companies to significant risks.
In this talk, I'll show you step-by-step how to secure your Kubernetes cluster for greater peace of mind and reliability.
2. Linked Lists
A linked list is a linear collection of data
elements, called nodes, where the linear
order is given by means of pointers.
Each node is divided into two parts:
The first part contains the information of the
element and
The second part contains the address of the next
node (link /next pointer field) in the list.
4. Adding an Element to the front
of a Linked List
5
info next
list
info next info next
3 8 null
5. Some Notations for use in
algorithm (Not in C programs)
p: is a pointer
node(p): the node pointed to by p
info(p): the information portion of the node
next(p): the next address portion of the node
getnode(): obtains an empty node
freenode(p): makes node(p) available for
reuse even if the value of the pointer p is
changed.
6. Adding an Element to the front
of a Linked List
5
info next
list
info next info next
3 8
info next
p p = getnode()
null
7. Adding an Element to the front
of a Linked List
5
info next
list
info next info next
3 8
info next
p 6 info(p) = 6
null
8. Adding an Element to the front
of a Linked List
5
info next info next info next
3 8
info next
p 6
list
next(p) = list
null
9. Adding an Element to the front
of a Linked List
5
info next info next info next
3 8
info next
6
p
list list = p
null
10. Adding an Element to the front
of a Linked List
5
info next info next info next
3 8
info next
list 6 null
11. Removing an Element from the
front of a Linked List
5
info next info next info next
3 8
info next
list 6 null
12. Removing an Element from the
front of a Linked List
5
info next info next info next
3 8
info next
6
list
p
p = list
null
13. Removing an Element from the
front of a Linked List
5
info next info next info next
3 8
info next
6
list
p list = next(p)
null
14. Removing an Element from the
front of a Linked List
5
info next info next info next
3 8
info next
6
list
p x = info(p)
x = 6
null
15. Removing an Element from the
front of a Linked List
5
info next info next info next
3 8
info next
p
x = 6
freenode(p)
list null
16. Removing an Element from the
front of a Linked List
5
info next info next info next
3 8
list
x = 6 null
17. Linked List Implementation of
Stacks – PUSH(S,X)
The first node of the list is the top of the
stack. If an external pointer s points to such a
linked list, the operation push(s,x) may be
implemented by
p=getnode();
info(p)=x;
next(p)=s;
s=p;
18. Linked List Implementation of
Stacks – POP(S)
The operation x=pop(s) removes the first node from a nonempty
list and signals underflow if the list is empty:
if (empty(s)){ /* checks whether s equals null */
printf(‘stack underflow’);
exit(1);
}
else {
p =s;
s=next(p);
x = info(p);
freenode(p);
}
19. Linked List Implemantation of
QUEUES
5
info next info next info next
3 8
info next
6
front null
rear
5
info next info next info next
3 8
info next
6
front
info next
null
rear
12
20. Linked List Implemantation of
QUEUES
A queue q consists of a list and two pointers, q.front and q.rear. The operations
empty(q) and x=remove(q) are completely analogous to empty(s) and x=pop(s),
with the pointer q.front replacing s.
if(empty(q)){
printf(“queue undeflow”);
exit(1);
}
p=q.front;
x=info(p);
q.front=next(p);
if(q.front==null)
q.rear=null;
freenode(p);
return(x);
21. Linked List Implemantation of
QUEUES
The operation insert(q,x) is implemented by
p= getnode();
info(p)=x;
next(p)=null;
if(q.front==null)
q.front=p;
else
next(q.rear)=p;
q.rear=p;
22. Linked List as a Data Structure
An item is accesses in a linked list by
traversing the list from its beginning.
An array implementation allows acccess to
the nth item in a group using single operation,
whereas a list implementation requires n
operations.
The advantage of a list over an array occurs
when it is necessary to insert or delete an
element in the middle of a group of other
elements.
23. Element x is inserted between the
third an fourth elements in an array
X0
X1
X2
X3
X4
X5
X6
X0
X1
X2
X3
X4
X5
X6
X0
X1
X2
X3
X4
X5
X6
x
24. Inserting an item x into a list
after a node pointed to by p
X0 X1 X2 X3 X4 X5 X6 null
list
X0 X1 X2 X3 X4 X5 X6 null
list
p
p
x
q
25. Inserting an item x into a list
after a node pointed to by p
q=getnode();
info(q)=x;
next(q)=next(p);
next(p)=q;
26. Deleting an item x from a list
after a node pointed to by p
X0 X1 X2 X3 X4 X5 X6 null
list
p q
X0 X1 X2 X4 X5 X6 null
list
p
x =X3
X3
27. Deleting an item x from a list
after a node pointed to by p
q=next(p);
x=info(q);
next(p)=next(q);
freenode(q);
28. LINKED LISTS USING
DYNAMIC VARIABLES
In array implementation of the linked lists a fixed set of nodes
represented by an array is established at the beginning of the execution
A pointer to a node is represented by the relative position of the node
within the array.
In array implementation, it is not possible to determine the number of
nodes required for the linked list. Therefore;
Less number of nodes can be allocated which means that the program will
have overflow problem.
More number of nodes can be allocated which means that some amount of
the memory storage will be wasted.
The solution to this problem is to allow nodes that are dynamic, rather
than static.
When a node is required storage is reserved/allocated for it and when a
node is no longerneeded, the memory storage is released/freed.
29. ALLOCATING AND FREEING
DYNAMIC VARIABLES
C library function malloc() is used for dynamically
allocating a space to a pointer. Note that the
malloc() is a library function in <stdlib.h> header file.
The following lines allocate an integer space from
the memory pointed by the pointer p.
int *p;
p = (int *) malloc(sizeof(int));
Note that sizeof() is another library function that returns the
number of bytes required for the operand. In this example,
4 bytes for the int.
30. ALLOCATING AND FREEING
DYNAMIC VARIABLES
Allocate floating point number space for a
float pointer f.
float *f;
f = (float *) malloc(sizeof(float));
31. Question:What is the output of
the following lines?
int *p, *q;
int x;
p = (int *) malloc(sizeof(int));
*p = 3;
x = 6;
q = (int *) malloc(sizeof(int));
*q=x;
printf(“%d %d n”, *p, *q);
The above lines will print 3 and 6.
p
p 3
6
x
q
q 6
32. malloc() and free()
The following lines and the proceeding figure shows the
effectiveness of the free() function.
int *p, *q;
p = (int *) malloc(sizeof(int));
*p = 5;
q = (int *) malloc(sizeof(int));
*q = 8;
free(p);
p = q;
q = (int *) malloc(sizeof(int));
*q = 6;
printf(“%d %d n”, *p, *q);
33. LINKED LISTS STRUCTURES
AND BASIC FUNCTIONS
The value zero can be used in a C program as the null pointer. You
can use the following line to declare the NULL constant. Note that a
NULL pointer is considered NOT to point any storage location.
#define NULL 0
The following node structure can be used to implement Linked Lists.
Note that the info field, which can be some other data type (not
necessarily int), keeps the data of the node and the pointer next links
the node to the next node in the Linked List.
struct node{
int info;
struct node *next;
};
typedef struct node *NODEPTR;
34. LINKED LISTS STRUCTURES
AND BASIC FUNCTIONS
When a new node is required (e.g. to be inserted
into the list) the following function, getnode, can be
used to make a new node to be available for the list.
NODEPTR getnode(void)
{
NODEPTR p;
p = (NODEPTR) malloc(sizeof(struct node));
return p;
}
35. LINKED LISTS STRUCTURES
AND BASIC FUNCTIONS
When a new node is no longer used (e.g. to
be deleted from the list) the following
function, freenode, can be used to release
the node back to the memory.
void freenode(NODEPTR p)
{
free(p);
}
36. PRIMITIVE FUNCTIONS FOR
LINEAR LINKED LISTS
The following functions insertafter(p,x) and
delafter(p,px) are primitive functions that can
be used for the dynamic implementation of a
linked list. Assume that list is a pointer
variable pointing the first node of a list (if any)
and equals NULL in the case of an empty list.
39. Searching through the linked
list.
The following function searches through the
linked list and returns a pointer the first
occurrence of the search key or returns NULL
pointer if the search key is not in the list. Note
that the linked list contains integer data items.
40. NODEPTR searchList(NODEPTR plist, int key)
{
NODEPTR p;
p = plist;
while(p != NULL){
if(p->info == key)
return p;
p = p->next;
}
return NULL;
}
41. Displaying the linked list
elements
Write a function to display the student with highest
CGPA in a linked list containing student data. Use
the following node structure for your linked list.
struct node{
int stNo;
float CGPA;
struct node *next;
};
typedef struct node *NODEPTR;
42. void DisplayMax(NODEPTR plist)
{
NODEPTR p;
float maxCGPA=-1.0;
int maxstNo;
p = plist; /*current node*/
if(p == NULL){
printf(“no node/data is available in the listn”);
return;
}
do{
if(p->CGPA > maxCGPA){
maxCGPA = p->CGPA;
maxstNo = p->stNo;
}
p = p->next;
} while(p!= NULL);
printf(“The student number with max CGPA: %dn”, maxstNo);
printf(“The student’s CGPA: %dn”, maxCGPA);
}