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
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
Data Structures in C++I am really new to C++, so links are really .pdfrohit219406
Data Structures in C++
I am really new to C++, so links are really hard topic for me. It would be nice if you can provide
explanations of what doubly linked lists are and of some of you steps... Thank you
In this assignment, you will implement a doubly-linked list class, together with some list
operations. To make things easier, you’ll implement a list of int, rather than a template class.
Solution
A variable helps us to identify the data. For ex: int a = 5; Here 5 is identified through variable a.
Now, if we have collection of integers, we need some representation to identify them. We call it
array. For ex: int arr[5]
This array is nothing but a Data Structure.
So, a Data Structure is a way to group the data.
There are many Data Structures available like Arrays, Linked List, Doubly-Linked list, Stack,
Queue, etc.
Doubly-Linked list are the ones where you can traverse from the current node both in left and
right directions.
Why so many different types of Data Structures are required ?
Answer is very simple, grouping of data, storage of data and accessing the data is different.
For example, in case of Arrays we store all the data in contiguous locations.
What if we are not able to store the data in contiguous locations because we have huge data.
Answer is go for Linked List/Doubly-Linked list.
Here we can store the data anywhere and link the data through pointers.
I will try to provide comments for the code you have given. May be this can help you.
#pragma once
/*
dlist.h
Doubly-linked lists of ints
*/
#include
class dlist {
public:
dlist() { }
// Here we are creating a NODE, it has a integer value and two pointers.
// One pointer is to move to next node and other to go back to previous node.
struct node {
int value;
node* next;
node* prev;
};
// To return head pointer, i.e. start of the Doubly-Linked list.
node* head() const { return _head; }
// To return Tail pointer, i.e. end of the Doubly-Linked list.
node* tail() const { return _tail; }
// **** Implement ALL the following methods ****
// Returns the node at a particular index (0 is the head).
node* at(int index){
int cnt = 0;
struct node* tmp = head();
while(tmp!=NULL)
{
if (cnt+1 == index)
return tmp;
tmp = tmp->next;
}
}
// Insert a new value, after an existing one
void insert(node *previous, int value){
// check if the given previous is NULL
if (previous == NULL)
{
printf(\"the given previous node cannot be NULL\");
return;
}
// allocate new node
struct node* new_node =(struct node*) malloc(sizeof(struct node));
// put in the data
new_node->data = new_data;
// Make next of new node as next of previous
new_node->next = previous->next;
// Make the next of previous as new_node
previous->next = new_node;
// Make previous as previous of new_node
new_node->prev = previous;
// Change previous of new_node\'s next node
if (new_node->next != NULL)
new_node->next->prev = new_node;
}
// Delete the given node
void del(node* which){
struct node* head_ref = head();
/* base case */
if(*head_ref == NUL.
–PLS write program in c++Recursive Linked List OperationsWrite a.pdfpasqualealvarez467
–PLS write program in c++
Recursive Linked List Operations
Write a C++ to manage a list of string elements. Besides the basic operations expected of a
linked list (add an element, remove an element, check for empty list, report size (number) of
elements in the list, and print out the content of the list), the linked list class also needs to have a
method called sort() and a method called reverse() the manipulate the string elements. The sort
method rearranges the elements in the list so they are sorted in alphabetical order. The reverse
method reverses the order of the elements of the list. The class should use recursion to
implement the sort and reverse operations
Solution
#include
#include
#include
#include
struct node //declaring node
{
int info;
struct node *next;
}*start;
class single_llist //declaring class
{
public:
node* create_node(int);
void insert_begin();
void insert_pos();
void insert_last();
void delete_pos();
void sort();
void reverse();
void display();
single_llist()
{
start = NULL;
}
};
main()
{
int choice, nodes, position, i;
char str[100];
single_llist sl;
start = NULL;
while (1)
{
cout<>choice;
switch(choice)
{
case 1:
cout<<\"Inserting Node at Beginning: \"<info = value;
temp->next = NULL;
return temp;
}
}
/*
* Inserting element in beginning
*/
void single_llist::insert_begin()
{
int value;
cout<<\"Enter the value to be inserted: \";
cin>>value;
struct node *temp, *p;
temp = create_node(value);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout<<\"Element Inserted at beginning\"<>value;
struct node *temp, *s;
temp = create_node(value);
s = start;
while (s->next != NULL)
{
s = s->next;
}
temp->next = NULL;
s->next = temp;
cout<<\"Element Inserted at last\"<>value;
struct node *temp, *s, *ptr;
temp = create_node(value);
cout<<\"Enter the postion at which node to be inserted: \";
cin>>pos;
int i;
s = start;
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos == 1)
{
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
ptr = start;
start = temp;
start->next = ptr;
}
}
else if (pos > 1 && pos <= counter)
{
s = start;
for (i = 1; i < pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else
{
cout<<\"Positon out of range\"<next;s !=NULL;s = s->next)
{
if (strcmp(ptr->info > s->info))
{
value = ptr->info;
ptr->info = s->info;
s->info = value;
}
}
ptr = ptr->next;
}
}
void single_llist::delete_pos() //delete element from given position
{
int pos, i, counter = 0;
if (start == NULL)
{
cout<<\"List is empty\"<>pos;
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start = s->next;
}
else
{
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos > 0 && pos <= counter)
{
s = start;
for (i = 1;i < pos;i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
{
cout<<\"Position out of range\"<next == NULL)
{
return;
}
ptr1 = start;
ptr2 = ptr1->next;
ptr3 = ptr2->next;
ptr1->next = NULL;
ptr2->next = ptr1;
while (ptr3 != NULL)
{
ptr1 = ptr2;
ptr2.
in C++ , Design a linked list class named IntegerList to hold a seri.pdfeyewaregallery
in C++ , Design a linked list class named IntegerList to hold a series of integers. The class must
have public member functions for appending , inserting, deleting and displaying nodes. The
append function must append a node to the end of the list. The insert function must insert node in
ascending order. The delete function must delete the node with the given value. The display
function should display the list from head to tail. The IntegerList class should also have a
destructor that destroys the list and release memory. The list node must be declared as a structure
using keyword struct. This structure is a private member of IntegerList. You should also store the
head of the list as a private member.
Write a test program to test the implementation your IntegerList class works properly.
Solution
Here is the code for the above scenario:
#include
using namespace std;
struct ListNode
{
float value;
ListNode *next;
};
ListNode *head;
class LinkedList
{
public:
int insertNode(float num);
int appendNode(float num);
void deleteNode(float num);
void destroyList();
void displayList();
LinkedList(void) {head = NULL;}
~LinkedList(void) {destroyList();}
};
int LinkedList::appendNode(float num)
{
ListNode *newNode, *nodePtr = head;
newNode = new ListNode;
if(newNode == NULL)
{
cout << \"Error allocating memory for new list member!\ \";
return 1;
}
newNode->value = num;
newNode->next = NULL;
if(head == NULL)
{
cout << \"List was empty - \" << newNode->value;
cout << \" is part of list\'s first node.\ \";
head = newNode;
}
else
{
while(nodePtr->next != NULL)
nodePtr = nodePtr->next;
nodePtr->next = newNode;
}
return 0;
}
int LinkedList::insertNode(float num)
{
struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL;
newNode = new ListNode;
if(newNode == NULL)
{
cout << \"Error allocating memory for new list member!\ \";
return 1;
}
newNode->value = num;
newNode->next = NULL;
if(head==NULL) {
cout << \"List was empty - \" << newNode->value;
cout << \" is part of list\'s first node.\ \";
head = newNode;
}
else
{
while((nodePtr != NULL) && (nodePtr->value < num)) {
prevNodePtr = nodePtr;
nodePtr = nodePtr->next;
}
if(prevNodePtr==NULL)
newNode->next = head;
else
newNode->next = nodePtr; prevNodePtr->next = newNode;
}
return 0;
}
void LinkedList::deleteNode(float num)
{
struct ListNode *nodePtr = head, *prevNodePtr = NULL;
if(head==NULL) {
cout << \"The list was empty!\ \";
return;
}
if(head->value == num) {
head = nodePtr->next;
delete [] nodePtr;
}
else
{
while((nodePtr!= NULL)&&(nodePtr->value != num)) {
prevNodePtr = nodePtr;
nodePtr = nodePtr->next;
}
if(nodePtr==NULL)
cout << \"The value \" << num << \" is not in this list!\ \";
else {
prevNodePtr->next = nodePtr->next;
delete [] nodePtr;
}
}
}
void LinkedList::destroyList()
{
struct ListNode *nodePtr = head, *nextNode = nodePtr;
if(head==NULL) {
cout << \"The list is empty!\ \";
return;
}
while (nodePtr != NULL) {
nextNode = nodePtr->next;
delete [] nodePtr;
nodePtr = nextNode;
}
}
void LinkedList.
The LinkedList1 class implements a Linked list. class.pdfmalavshah9013
/**
The LinkedList1 class implements a Linked list.
*/
class LinkedList1
{
/**
The Node class stores a list element
and a reference to the next node.
*/
private class Node
{
String value;
Node next;
/**
Constructor.
@param val The element to store in the node.
@param n The reference to the successor node.
*/
Node(String val, Node n)
{
value = val;
next = n;
}
/**
Constructor.
@param val The element to store in the node.
*/
Node(String val)
{
// Call the other (sister) constructor.
this(val, null);
}
}
private Node first; // list head
private Node last; // last element in list
/**
Constructor.
*/
public LinkedList1()
{
first = null;
last = null;
}
/**
The isEmpty method checks to see
if the list is empty.
@return true if list is empty,
false otherwise.
*/
public boolean isEmpty()
{
return first == null;
}
/**
The size method returns the length of the list.
@return The number of elements in the list.
*/
public int size()
{
int count = 0;
Node p = first;
while (p != null)
{
// There is an element at p
count ++;
p = p.next;
}
return count;
}
/**
The add method adds an element to
the end of the list.
@param e The value to add to the
end of the list.
*/
public void add(String e)
{
if (isEmpty())
{
first = new Node(e);
last = first;
}
else
{
// Add to end of existing list
last.next = new Node(e);
last = last.next;
}
}
/**
The add method adds an element at a position.
@param e The element to add to the list.
@param index The position at which to add
the element.
@exception IndexOutOfBoundsException When
index is out of bounds.
*/
public void add(int index, String e)
{
if (index < 0 || index > size())
{
String message = String.valueOf(index);
throw new IndexOutOfBoundsException(message);
}
// Index is at least 0
if (index == 0)
{
// New element goes at beginning
first = new Node(e, first);
if (last == null)
last = first;
return;
}
// Set a reference pred to point to the node that
// will be the predecessor of the new node
Node pred = first;
for (int k = 1; k <= index - 1; k++)
{
pred = pred.next;
}
// Splice in a node containing the new element
pred.next = new Node(e, pred.next);
// Is there a new last element ?
if (pred.next.next == null)
last = pred.next;
}
/**
The toString method computes the string
representation of the list.
@return The string form of the list.
*/
public String toString()
{
StringBuilder strBuilder = new StringBuilder();
// Use p to walk down the linked list
Node p = first;
while (p != null)
{
strBuilder.append(p.value + \"\ \");
p = p.next;
}
return strBuilder.toString();
}
/**
The remove method removes the element at an index.
@param index The index of the element to remove.
@return The element removed.
@exception IndexOutOfBoundsException When index is
out of bounds.
*/
public String remove(int index)
{
if (index < 0 || index >= size())
{
String message = String.valueOf(index);
throw new IndexOutOfBoundsException(message);
}
String element; // The element to return
if (index == 0)
{
// Removal of first item in the list
element.
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfpoblettesedanoree498
C++: Doubly-Linked Lists
The goal of the exercise is to implement a class List of doubly- linked lists.
My goal is to implement the class in a file doubly-linked.cpp.
The class List implements lists using the following structures for list ele- ments:
struct Node { int val ;
Node next;
Node prev; };
Each Node contains a value (an integer) and two pointers: one meant to point to the next element
of the list and one meant to point to the previous element in the list. The class provides the
following methods that you need to implement:
• A constructor and a destructor, with no arguments;
• void insert(int n): insert an integer at the end of the list;
• void reverse(): reverse the list;
• void print(): print the list to cout in the same format as the input (i.e. integers separated by
spaces);
doubly-linked.h
#ifndef __dll__
#define __dll__
#include
using namespace std;
// Basic structure to store elements of a list
struct Node {
int val; // contains the value
Node * next; // pointer to the next element in the list
Node * prev; // pointer to the previous element in the list
};
// Class List
class List {
public:
List(void); // Constructor
~List(void); // Destructor
void insert(int n); // This should insert n in the list
void reverse(void); // This should reverse the list
void print(void); // This shoiuld print the list
private:
Node * first; // Pointer to the first (if any) element in the list
};
#endif
main.cpp
#include
#include \"doubly-linked.h\"
using namespace std;
int main(void){
List l;
int n;
while(cin >> n){
l.insert(n);
}
// Print list as read from cin
l.print();
// Reverse the list and print it
l.reverse();
l.print();
// Reverse again and print it
l.reverse();
l.print();
return 0;
}
Solution
#include
using namespace std;
/* Linked list structure */
struct list {
struct list *prev;
int data;
struct list *next;
} *node = NULL, *first = NULL, *last = NULL, *node1 = NULL, *node2 = NULL;
class linkedlist {
public:
/* Function for create/insert node at the beginning of Linked list */
void insrt_frnt() {
list *addBeg = new list;
cout << \"Enter value for the node:\" << endl;
cin >> addBeg->data;
if(first == NULL) {
addBeg->prev = NULL;
addBeg->next = NULL;
first = addBeg;
last = addBeg;
cout << \"Linked list Created!\" << endl;
}
else {
addBeg->prev = NULL;
first->prev = addBeg;
addBeg->next = first;
first = addBeg;
cout << \"Data Inserted at the beginning of the Linked list!\" << endl;
}
}
/* Function for create/insert node at the end of Linked list */
void insrt_end() {
list *addEnd = new list;
cout << \"Enter value for the node:\" << endl;
cin >> addEnd->data;
if(first == NULL) {
addEnd->prev = NULL;
addEnd->next = NULL;
first = addEnd;
last = addEnd;
cout << \"Linked list Created!\" << endl;
}
else {
addEnd->next = NULL;
last->next = addEnd;
addEnd->prev = last;
last = addEnd;
cout << \"Data Inserted at the end of the Linked list!\" << endl;
}
}
/* Function for Display Linked list */
void display() {
node = first;
cout << \"List of data in L.
Can AI do good? at 'offtheCanvas' India HCI preludeAlan Dix
Invited talk at 'offtheCanvas' IndiaHCI prelude, 29th June 2024.
https://www.alandix.com/academic/talks/offtheCanvas-IndiaHCI2024/
The world is being changed fundamentally by AI and we are constantly faced with newspaper headlines about its harmful effects. However, there is also the potential to both ameliorate theses harms and use the new abilities of AI to transform society for the good. Can you make the difference?
Data Structures in C++I am really new to C++, so links are really .pdfrohit219406
Data Structures in C++
I am really new to C++, so links are really hard topic for me. It would be nice if you can provide
explanations of what doubly linked lists are and of some of you steps... Thank you
In this assignment, you will implement a doubly-linked list class, together with some list
operations. To make things easier, you’ll implement a list of int, rather than a template class.
Solution
A variable helps us to identify the data. For ex: int a = 5; Here 5 is identified through variable a.
Now, if we have collection of integers, we need some representation to identify them. We call it
array. For ex: int arr[5]
This array is nothing but a Data Structure.
So, a Data Structure is a way to group the data.
There are many Data Structures available like Arrays, Linked List, Doubly-Linked list, Stack,
Queue, etc.
Doubly-Linked list are the ones where you can traverse from the current node both in left and
right directions.
Why so many different types of Data Structures are required ?
Answer is very simple, grouping of data, storage of data and accessing the data is different.
For example, in case of Arrays we store all the data in contiguous locations.
What if we are not able to store the data in contiguous locations because we have huge data.
Answer is go for Linked List/Doubly-Linked list.
Here we can store the data anywhere and link the data through pointers.
I will try to provide comments for the code you have given. May be this can help you.
#pragma once
/*
dlist.h
Doubly-linked lists of ints
*/
#include
class dlist {
public:
dlist() { }
// Here we are creating a NODE, it has a integer value and two pointers.
// One pointer is to move to next node and other to go back to previous node.
struct node {
int value;
node* next;
node* prev;
};
// To return head pointer, i.e. start of the Doubly-Linked list.
node* head() const { return _head; }
// To return Tail pointer, i.e. end of the Doubly-Linked list.
node* tail() const { return _tail; }
// **** Implement ALL the following methods ****
// Returns the node at a particular index (0 is the head).
node* at(int index){
int cnt = 0;
struct node* tmp = head();
while(tmp!=NULL)
{
if (cnt+1 == index)
return tmp;
tmp = tmp->next;
}
}
// Insert a new value, after an existing one
void insert(node *previous, int value){
// check if the given previous is NULL
if (previous == NULL)
{
printf(\"the given previous node cannot be NULL\");
return;
}
// allocate new node
struct node* new_node =(struct node*) malloc(sizeof(struct node));
// put in the data
new_node->data = new_data;
// Make next of new node as next of previous
new_node->next = previous->next;
// Make the next of previous as new_node
previous->next = new_node;
// Make previous as previous of new_node
new_node->prev = previous;
// Change previous of new_node\'s next node
if (new_node->next != NULL)
new_node->next->prev = new_node;
}
// Delete the given node
void del(node* which){
struct node* head_ref = head();
/* base case */
if(*head_ref == NUL.
–PLS write program in c++Recursive Linked List OperationsWrite a.pdfpasqualealvarez467
–PLS write program in c++
Recursive Linked List Operations
Write a C++ to manage a list of string elements. Besides the basic operations expected of a
linked list (add an element, remove an element, check for empty list, report size (number) of
elements in the list, and print out the content of the list), the linked list class also needs to have a
method called sort() and a method called reverse() the manipulate the string elements. The sort
method rearranges the elements in the list so they are sorted in alphabetical order. The reverse
method reverses the order of the elements of the list. The class should use recursion to
implement the sort and reverse operations
Solution
#include
#include
#include
#include
struct node //declaring node
{
int info;
struct node *next;
}*start;
class single_llist //declaring class
{
public:
node* create_node(int);
void insert_begin();
void insert_pos();
void insert_last();
void delete_pos();
void sort();
void reverse();
void display();
single_llist()
{
start = NULL;
}
};
main()
{
int choice, nodes, position, i;
char str[100];
single_llist sl;
start = NULL;
while (1)
{
cout<>choice;
switch(choice)
{
case 1:
cout<<\"Inserting Node at Beginning: \"<info = value;
temp->next = NULL;
return temp;
}
}
/*
* Inserting element in beginning
*/
void single_llist::insert_begin()
{
int value;
cout<<\"Enter the value to be inserted: \";
cin>>value;
struct node *temp, *p;
temp = create_node(value);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout<<\"Element Inserted at beginning\"<>value;
struct node *temp, *s;
temp = create_node(value);
s = start;
while (s->next != NULL)
{
s = s->next;
}
temp->next = NULL;
s->next = temp;
cout<<\"Element Inserted at last\"<>value;
struct node *temp, *s, *ptr;
temp = create_node(value);
cout<<\"Enter the postion at which node to be inserted: \";
cin>>pos;
int i;
s = start;
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos == 1)
{
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
ptr = start;
start = temp;
start->next = ptr;
}
}
else if (pos > 1 && pos <= counter)
{
s = start;
for (i = 1; i < pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else
{
cout<<\"Positon out of range\"<next;s !=NULL;s = s->next)
{
if (strcmp(ptr->info > s->info))
{
value = ptr->info;
ptr->info = s->info;
s->info = value;
}
}
ptr = ptr->next;
}
}
void single_llist::delete_pos() //delete element from given position
{
int pos, i, counter = 0;
if (start == NULL)
{
cout<<\"List is empty\"<>pos;
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start = s->next;
}
else
{
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos > 0 && pos <= counter)
{
s = start;
for (i = 1;i < pos;i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
{
cout<<\"Position out of range\"<next == NULL)
{
return;
}
ptr1 = start;
ptr2 = ptr1->next;
ptr3 = ptr2->next;
ptr1->next = NULL;
ptr2->next = ptr1;
while (ptr3 != NULL)
{
ptr1 = ptr2;
ptr2.
in C++ , Design a linked list class named IntegerList to hold a seri.pdfeyewaregallery
in C++ , Design a linked list class named IntegerList to hold a series of integers. The class must
have public member functions for appending , inserting, deleting and displaying nodes. The
append function must append a node to the end of the list. The insert function must insert node in
ascending order. The delete function must delete the node with the given value. The display
function should display the list from head to tail. The IntegerList class should also have a
destructor that destroys the list and release memory. The list node must be declared as a structure
using keyword struct. This structure is a private member of IntegerList. You should also store the
head of the list as a private member.
Write a test program to test the implementation your IntegerList class works properly.
Solution
Here is the code for the above scenario:
#include
using namespace std;
struct ListNode
{
float value;
ListNode *next;
};
ListNode *head;
class LinkedList
{
public:
int insertNode(float num);
int appendNode(float num);
void deleteNode(float num);
void destroyList();
void displayList();
LinkedList(void) {head = NULL;}
~LinkedList(void) {destroyList();}
};
int LinkedList::appendNode(float num)
{
ListNode *newNode, *nodePtr = head;
newNode = new ListNode;
if(newNode == NULL)
{
cout << \"Error allocating memory for new list member!\ \";
return 1;
}
newNode->value = num;
newNode->next = NULL;
if(head == NULL)
{
cout << \"List was empty - \" << newNode->value;
cout << \" is part of list\'s first node.\ \";
head = newNode;
}
else
{
while(nodePtr->next != NULL)
nodePtr = nodePtr->next;
nodePtr->next = newNode;
}
return 0;
}
int LinkedList::insertNode(float num)
{
struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL;
newNode = new ListNode;
if(newNode == NULL)
{
cout << \"Error allocating memory for new list member!\ \";
return 1;
}
newNode->value = num;
newNode->next = NULL;
if(head==NULL) {
cout << \"List was empty - \" << newNode->value;
cout << \" is part of list\'s first node.\ \";
head = newNode;
}
else
{
while((nodePtr != NULL) && (nodePtr->value < num)) {
prevNodePtr = nodePtr;
nodePtr = nodePtr->next;
}
if(prevNodePtr==NULL)
newNode->next = head;
else
newNode->next = nodePtr; prevNodePtr->next = newNode;
}
return 0;
}
void LinkedList::deleteNode(float num)
{
struct ListNode *nodePtr = head, *prevNodePtr = NULL;
if(head==NULL) {
cout << \"The list was empty!\ \";
return;
}
if(head->value == num) {
head = nodePtr->next;
delete [] nodePtr;
}
else
{
while((nodePtr!= NULL)&&(nodePtr->value != num)) {
prevNodePtr = nodePtr;
nodePtr = nodePtr->next;
}
if(nodePtr==NULL)
cout << \"The value \" << num << \" is not in this list!\ \";
else {
prevNodePtr->next = nodePtr->next;
delete [] nodePtr;
}
}
}
void LinkedList::destroyList()
{
struct ListNode *nodePtr = head, *nextNode = nodePtr;
if(head==NULL) {
cout << \"The list is empty!\ \";
return;
}
while (nodePtr != NULL) {
nextNode = nodePtr->next;
delete [] nodePtr;
nodePtr = nextNode;
}
}
void LinkedList.
The LinkedList1 class implements a Linked list. class.pdfmalavshah9013
/**
The LinkedList1 class implements a Linked list.
*/
class LinkedList1
{
/**
The Node class stores a list element
and a reference to the next node.
*/
private class Node
{
String value;
Node next;
/**
Constructor.
@param val The element to store in the node.
@param n The reference to the successor node.
*/
Node(String val, Node n)
{
value = val;
next = n;
}
/**
Constructor.
@param val The element to store in the node.
*/
Node(String val)
{
// Call the other (sister) constructor.
this(val, null);
}
}
private Node first; // list head
private Node last; // last element in list
/**
Constructor.
*/
public LinkedList1()
{
first = null;
last = null;
}
/**
The isEmpty method checks to see
if the list is empty.
@return true if list is empty,
false otherwise.
*/
public boolean isEmpty()
{
return first == null;
}
/**
The size method returns the length of the list.
@return The number of elements in the list.
*/
public int size()
{
int count = 0;
Node p = first;
while (p != null)
{
// There is an element at p
count ++;
p = p.next;
}
return count;
}
/**
The add method adds an element to
the end of the list.
@param e The value to add to the
end of the list.
*/
public void add(String e)
{
if (isEmpty())
{
first = new Node(e);
last = first;
}
else
{
// Add to end of existing list
last.next = new Node(e);
last = last.next;
}
}
/**
The add method adds an element at a position.
@param e The element to add to the list.
@param index The position at which to add
the element.
@exception IndexOutOfBoundsException When
index is out of bounds.
*/
public void add(int index, String e)
{
if (index < 0 || index > size())
{
String message = String.valueOf(index);
throw new IndexOutOfBoundsException(message);
}
// Index is at least 0
if (index == 0)
{
// New element goes at beginning
first = new Node(e, first);
if (last == null)
last = first;
return;
}
// Set a reference pred to point to the node that
// will be the predecessor of the new node
Node pred = first;
for (int k = 1; k <= index - 1; k++)
{
pred = pred.next;
}
// Splice in a node containing the new element
pred.next = new Node(e, pred.next);
// Is there a new last element ?
if (pred.next.next == null)
last = pred.next;
}
/**
The toString method computes the string
representation of the list.
@return The string form of the list.
*/
public String toString()
{
StringBuilder strBuilder = new StringBuilder();
// Use p to walk down the linked list
Node p = first;
while (p != null)
{
strBuilder.append(p.value + \"\ \");
p = p.next;
}
return strBuilder.toString();
}
/**
The remove method removes the element at an index.
@param index The index of the element to remove.
@return The element removed.
@exception IndexOutOfBoundsException When index is
out of bounds.
*/
public String remove(int index)
{
if (index < 0 || index >= size())
{
String message = String.valueOf(index);
throw new IndexOutOfBoundsException(message);
}
String element; // The element to return
if (index == 0)
{
// Removal of first item in the list
element.
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfpoblettesedanoree498
C++: Doubly-Linked Lists
The goal of the exercise is to implement a class List of doubly- linked lists.
My goal is to implement the class in a file doubly-linked.cpp.
The class List implements lists using the following structures for list ele- ments:
struct Node { int val ;
Node next;
Node prev; };
Each Node contains a value (an integer) and two pointers: one meant to point to the next element
of the list and one meant to point to the previous element in the list. The class provides the
following methods that you need to implement:
• A constructor and a destructor, with no arguments;
• void insert(int n): insert an integer at the end of the list;
• void reverse(): reverse the list;
• void print(): print the list to cout in the same format as the input (i.e. integers separated by
spaces);
doubly-linked.h
#ifndef __dll__
#define __dll__
#include
using namespace std;
// Basic structure to store elements of a list
struct Node {
int val; // contains the value
Node * next; // pointer to the next element in the list
Node * prev; // pointer to the previous element in the list
};
// Class List
class List {
public:
List(void); // Constructor
~List(void); // Destructor
void insert(int n); // This should insert n in the list
void reverse(void); // This should reverse the list
void print(void); // This shoiuld print the list
private:
Node * first; // Pointer to the first (if any) element in the list
};
#endif
main.cpp
#include
#include \"doubly-linked.h\"
using namespace std;
int main(void){
List l;
int n;
while(cin >> n){
l.insert(n);
}
// Print list as read from cin
l.print();
// Reverse the list and print it
l.reverse();
l.print();
// Reverse again and print it
l.reverse();
l.print();
return 0;
}
Solution
#include
using namespace std;
/* Linked list structure */
struct list {
struct list *prev;
int data;
struct list *next;
} *node = NULL, *first = NULL, *last = NULL, *node1 = NULL, *node2 = NULL;
class linkedlist {
public:
/* Function for create/insert node at the beginning of Linked list */
void insrt_frnt() {
list *addBeg = new list;
cout << \"Enter value for the node:\" << endl;
cin >> addBeg->data;
if(first == NULL) {
addBeg->prev = NULL;
addBeg->next = NULL;
first = addBeg;
last = addBeg;
cout << \"Linked list Created!\" << endl;
}
else {
addBeg->prev = NULL;
first->prev = addBeg;
addBeg->next = first;
first = addBeg;
cout << \"Data Inserted at the beginning of the Linked list!\" << endl;
}
}
/* Function for create/insert node at the end of Linked list */
void insrt_end() {
list *addEnd = new list;
cout << \"Enter value for the node:\" << endl;
cin >> addEnd->data;
if(first == NULL) {
addEnd->prev = NULL;
addEnd->next = NULL;
first = addEnd;
last = addEnd;
cout << \"Linked list Created!\" << endl;
}
else {
addEnd->next = NULL;
last->next = addEnd;
addEnd->prev = last;
last = addEnd;
cout << \"Data Inserted at the end of the Linked list!\" << endl;
}
}
/* Function for Display Linked list */
void display() {
node = first;
cout << \"List of data in L.
Can AI do good? at 'offtheCanvas' India HCI preludeAlan Dix
Invited talk at 'offtheCanvas' IndiaHCI prelude, 29th June 2024.
https://www.alandix.com/academic/talks/offtheCanvas-IndiaHCI2024/
The world is being changed fundamentally by AI and we are constantly faced with newspaper headlines about its harmful effects. However, there is also the potential to both ameliorate theses harms and use the new abilities of AI to transform society for the good. Can you make the difference?
7 Alternatives to Bullet Points in PowerPointAlvis Oh
So you tried all the ways to beautify your bullet points on your pitch deck but it just got way uglier. These points are supposed to be memorable and leave a lasting impression on your audience. With these tips, you'll no longer have to spend so much time thinking how you should present your pointers.
Expert Accessory Dwelling Unit (ADU) Drafting ServicesResDraft
Whether you’re looking to create a guest house, a rental unit, or a private retreat, our experienced team will design a space that complements your existing home and maximizes your investment. We provide personalized, comprehensive expert accessory dwelling unit (ADU)drafting solutions tailored to your needs, ensuring a seamless process from concept to completion.
You could be a professional graphic designer and still make mistakes. There is always the possibility of human error. On the other hand if you’re not a designer, the chances of making some common graphic design mistakes are even higher. Because you don’t know what you don’t know. That’s where this blog comes in. To make your job easier and help you create better designs, we have put together a list of common graphic design mistakes that you need to avoid.
Transforming Brand Perception and Boosting Profitabilityaaryangarg12
In today's digital era, the dynamics of brand perception, consumer behavior, and profitability have been profoundly reshaped by the synergy of branding, social media, and website design. This research paper investigates the transformative power of these elements in influencing how individuals perceive brands and products and how this transformation can be harnessed to drive sales and profitability for businesses.
Through an exploration of brand psychology and consumer behavior, this study sheds light on the intricate ways in which effective branding strategies, strategic social media engagement, and user-centric website design contribute to altering consumers' perceptions. We delve into the principles that underlie successful brand transformations, examining how visual identity, messaging, and storytelling can captivate and resonate with target audiences.
Methodologically, this research employs a comprehensive approach, combining qualitative and quantitative analyses. Real-world case studies illustrate the impact of branding, social media campaigns, and website redesigns on consumer perception, sales figures, and profitability. We assess the various metrics, including brand awareness, customer engagement, conversion rates, and revenue growth, to measure the effectiveness of these strategies.
The results underscore the pivotal role of cohesive branding, social media influence, and website usability in shaping positive brand perceptions, influencing consumer decisions, and ultimately bolstering sales and profitability. This paper provides actionable insights and strategic recommendations for businesses seeking to leverage branding, social media, and website design as potent tools to enhance their market position and financial success.
Between Filth and Fortune- Urban Cattle Foraging Realities by Devi S Nair, An...Mansi Shah
This study examines cattle rearing in urban and rural settings, focusing on milk production and consumption. By exploring a case in Ahmedabad, it highlights the challenges and processes in dairy farming across different environments, emphasising the need for sustainable practices and the essential role of milk in daily consumption.
2. Linked List
Linked List can be defined as collection of objects called nodes that are
randomly stored in the memory.
A node contains two fields i.e. data stored at that particular address and the
pointer which contains the address of the next node in the memory.
The last node of the list contains pointer to the null.
Uses of Linked List
• The list is not required to be contiguously present in the memory. The node
can reside any where in the memory and linked together to make a list. This
achieves optimized utilization of space.
• list size is limited to the memory size and doesn't need to be declared in
advance.
• Empty node can not be present in the linked list.
• We can store values of primitive types or objects in the singly linked list.
3. Node Creation :
struct node
{
int data;
struct node *next;
};
struct node *head, *ptr;
ptr = (struct node *)malloc(sizeof(struct node *));
Insertion in Linked list
SN Operation Description
1 Insertion at beginning It involves inserting any element at the front of the list. We
just need to a few link adjustments to make the new node
as the head of the list.
2 Insertion at end of the list It involves insertion at the last of the linked list. The new
node can be inserted as the only node in the list or it can
be inserted as the last one. Different logics are
implemented in each scenario.
3 Insertion after specified
node
It involves insertion after the specified node of the linked
list. We need to skip the desired number of nodes in order
to reach the node after which the new node will be
inserted. .
5. Random Insert at Position
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("nOVERFLOW");
}
else
{
printf("nEnter element value");
scanf("%d",&item);
ptr->data = item;
printf("nEnter the location after which you want to insert ");
scanf("n%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("ncan't insertn");
return;
}
}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("nNode inserted");
}
6. Begin Delete
struct node *ptr;
if(head == NULL)
{
printf("nList is emptyn");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("nNode deleted from the begining ...n");
}
Delete Last node
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("nlist is empty");
}
else if(head -> next == NULL)
{
head = NULL;
free(head);
printf("nOnly node of the list deleted ...n");
}
else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("nDeleted Node from the last ...n");
}
7. Random Delete node at Position
struct node *ptr,*ptr1;
int loc,i;
printf("n Enter the location of the node after which you want to perform deletion n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;
if(ptr == NULL)
{
printf("nCan't delete");
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("nDeleted node %d ",loc+1);
8. Find node in Linked List
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("nEmpty Listn");
}
else
{
printf("nEnter item which you want to
search?n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d
",i+1);
flag=0;
}
else
{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("Item not foundn");
}
}
10. DOUBLY LINKED LIST
What is a doubly-linked list?
A doubly linked list is another type of the linked list. It is called a doubly linked list because it
contains two addresses while a singly linked list contains a single address. It is a list that has
total three parts, one is a data part, and others two are the pointers, i.e., previous and next.
The previous pointer holds the address of the previous node, and the next pointer holds the
address of the next node. Therefore, we can say that list has two references, i.e., forward and
backward reference to traverse in either direction.
In c the structure of a node is describe as :
struct node
{
struct node *prev;
int data;
struct node *next;
}
11. Memory Representation of a doubly linked list is shown in the following image.
Generally, doubly linked list consumes more space for every node and therefore,
causes more expansive basic operations such as insertion and deletion. However, we
can easily manipulate the elements of the list since the list maintains pointers in both
the directions (forward and backward).
In the following image, the first element of the list that is i.e. 13 stored at address 1.
The head pointer points to the starting address 1. Since this is the first element being
added to the list therefore the prev of the list contains null. The next node of the list
resides at address 4 therefore the first node contains 4 in its next pointer.
We can traverse the list in this way until we find any node containing null or -1 in its
next part.
MEMORY REPRESENTATION OF A DOUBLY LINKED
LIST
13. Insert at Position
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("n OVERFLOW");
}
else
{
temp=head;
printf("Enter the location");
scanf("%d",&loc);
for(i=1;i<loc-1; i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("n There are less than %d elements", loc);
return;
}
}
printf("Enter value");
scanf("%d",&ptr->data);
ptr -> prev = temp;
ptr->next = temp->next;
temp->next = ptr;
temp->next->prev=ptr;
printf("nnode insertedn");
}
Deletion at Begining
struct node *ptr;
if(head == NULL)
{
printf("n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("n node deletedn");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;
free(ptr);
printf("nnode deletedn");
}
14. Deletion at last
struct node *ptr;
if(head == NULL)
{
printf("n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("nnode deletedn");
}
else
{
ptr = head;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("nnode deletedn");
}
Deletion at Position
struct node *ptr, *temp;
int val;
printf("n Enter the data after which the node is to be del
eted : ");
scanf("%d", &val);
ptr = head;
while(ptr -> data != val)
ptr = ptr -> next;
if(ptr -> next == NULL)
{
printf("nCan't deleten");
}
else if(ptr -> next -> next == NULL)
{
ptr ->next = NULL;
}
else
{
temp = ptr -> next;
ptr -> next = temp -> next;
temp -> next -> prev = ptr;
free(temp);
printf("nnode deletedn");
}
15. CIRCULAR LINKED LIST
In a circular Singly linked list, the last node of the list contains a pointer to the
first node of the list. We can have circular singly linked list as well as circular
doubly linked list.
We traverse a circular singly linked list until we reach the same node where we
started. The circular singly liked list has no beginning and no ending. There is no
null value present in the next part of any of the nodes.
The following image shows a circular singly linked list.
16. Begin Insert
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("nOVERFLOW");
}
else
{
printf("nEnter the node data?");
scanf("%d",&item);
ptr -> data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp->next != head)
temp = temp->next;
ptr->next = head;
temp -> next = ptr;
head = ptr;
}
printf("nnode insertedn");
}
Last Insert
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("nOVERFLOWn");
}
else
{
printf("nEnter Data?");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp -> next != head)
{
temp = temp -> next;
}
temp -> next = ptr;
ptr -> next = head;
}
printf("nnode insertedn");
}
20. Dynamic memory allocation in C
The concept of dynamic memory allocation in c language
enables the C programmer to allocate memory at runtime.
Dynamic memory allocation in c language is possible by 4
functions of stdlib.h header file.
malloc()
calloc()
realloc()
free()
Before learning above functions, let's understand the difference
between static memory allocation and dynamic memory
allocation.
21. static memory allocation dynamic memory allocation
memory is allocated at compile
time.
memory is allocated at run
time.
memory can't be increased
while executing program.
memory can be increased
while executing program.
used in array. used in linked list.
malloc() allocates single block of
requested memory.
calloc() allocates multiple block of
requested memory.
realloc() reallocates the memory
occupied by malloc() or
calloc() functions.
free() frees the dynamically allocated
memory.