write any 2 c functions for a linked list and a doubly-linked sorted list
create at least two data structures
We will be wirting c function to
1)insert an element
2)delete an element
For linked list, to insert an element, we simply need to create a node, and add it to the beginning of the list
To delete an element in linked list, we need to find that element and delete it from the list.
For doubly sorted linked list, to insert an element we need to find out the correct position of the linked list and put the node there since it is sorted. To delete the node, we need to find the node and delete it.
Given below are the two data structures created as node1 and node2 for singly linked list and doubly linked list respectively:
======================================================
#include<stdio.h>
#include<stdlib.h>
typedef struct node1 {
int data;
struct node1 *next;
} node1;
typedef struct node2 {
int data;
struct node2 *prev;
struct node2 *next;
} node2;
node1 *makeNode1(int x){ //returns new node for linked
list
node1 *newNode;
newNode = (node1*)malloc(sizeof(node1));
newNode->data=x;
newNode->next=NULL;
return newNode;
}
node2 *makeNode2(int x){ //returns new node for doubly linked
list
node2 *newNode;
newNode = (node2*)malloc(sizeof(node2));
newNode->data=x;
newNode->next=NULL;
newNode->prev=NULL;
return newNode;
}
node1 *insertLinkedNode1(node1 *head,int x){
node1 *temp = makeNode1(x);
temp->next=head;
head=temp;
printf("\nNode %d inserted, new list is: ", x);
temp=head;
while(temp){
printf(" %d ", temp->data);
temp=temp->next;
}
return head;
}
node1 *deleteLinkedNode1(node1 *head,int x){
node1 *temp;
temp=head;
int flag=0;
if(head->data==x){
head=head->next;
free(temp);
flag=1;
}
else{
while(temp->next!=NULL &&
temp->next->data!=x)
temp=temp->next;
if(temp->next!=NULL){
node1 *temp3 = temp->next;
temp->next=temp->next->next;
free(temp3);
flag=1;
}
}
if(flag==0)
printf("\nNode not found");
else{
printf("\nNode %d deleted, new list is: ",x);
temp=head;
while(temp){
printf(" %d ", temp->data);
temp=temp->next;
}
}
return head;
}
node2 *insertLinkedNode2(node2 *head,int x){
node2 *temp = makeNode2(x);
if(temp->data < head->data){
temp->next=head;
head->prev=temp;
temp->prev=NULL;
head=temp;}
else{
node2 *temp1;
temp1=head;
while(temp1->next!=NULL && temp->data >
temp1->next->data)
temp1=temp1->next;
if(temp1->next==NULL){
temp1->next=temp;
temp->prev=temp1;
}
else{
temp->next=temp1->next;
temp->prev=temp;
temp1->next=temp;
temp->next->prev=temp;
}
}
printf("\nNode %d inserted, new list is: ", x);
temp=head;
while(temp){
printf(" %d ", temp->data);
temp=temp->next;
}
return head;
}
node2 *deleteLinkedNode2(node2 *head,int x){
node2 *temp;
temp=head;
int flag=0;
if(head->data==x){
head=head->next;
free(temp);
flag=1;
}
else{
while(temp->next!=NULL &&
temp->next->data!=x)
temp=temp->next;
if(temp->next!=NULL){
//node2 *temp3= temp->next;
temp->next=temp->next->next;
if(temp->next!=NULL)
temp->next->prev=temp;
// free(temp3);
flag=1;
}
}
if(flag==0)
printf("\nNode not found");
else{
printf("\nNode %d deleted, new list is: ", x);
temp=head;
while(temp){
printf(" %d ", temp->data);
temp=temp->next;
}
}
return head;
}
int main(){
node1 *head1;
node1 *temp1 = makeNode1(3);
head1=temp1;
printf("Linked List: ");
head1=insertLinkedNode1(head1,10);
head1=insertLinkedNode1(head1,7);
head1=insertLinkedNode1(head1,2);
head1=deleteLinkedNode1(head1,7);
head1=deleteLinkedNode1(head1,10);
head1=deleteLinkedNode1(head1,3);
printf("\n\nDoubly sorted linked list: ");
node2 *head2;
node2 *temp2 = makeNode2(3);
head2 = temp2;
head2=insertLinkedNode2(head2,10);
head2=insertLinkedNode2(head2,7);
head2=insertLinkedNode2(head2,2);
head2=deleteLinkedNode2(head2,7);
head2=deleteLinkedNode2(head2,10);
head2=deleteLinkedNode2(head2,3);
return 0;
}
===============================================
Here is the screenshot of code:
write any 2 c functions for a linked list and a doubly-linked sorted list create at...
Implement a simple Doubly Linked List using nodes. Functions to include: an insert in a sorted list, and a delete function, including constructor. Please include comments to make the code traceable and understandable
In C++ Create a data structure doubly linked list, implement the following operations for the doubly linked list: addFirst addLast insertBefore insertAfter delete printList
In python - Implement a doubly linked circular linked list of Node objects called CircularDoublyLinkedList. The data of each Node in the list is an integer. You will collect measurements for: An already sorted linked list An already sorted linked list in descending order A linked list containing random data
Write a C++implementation of a doubly linked list class using a template class representation for a node and using pointers.In its public API provide functions to insert,find, delete, get size and get position of an element. Write C++ code to show those functions work as expected. (MUST COMPILE AND RUN)
C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...
Using C++, create a doubly linked list data structure that stores strings. At a minimum, you must have a List class that contains the list functionality (including an insert function) and a linkable object ("link node") class. For convenience, you may include the data directly or the data may be external to the link node, connected with a reference. You may use an inner class for the LinkNode and/or include the LinkNode class with the List class file if you...
1) Create a portfolio class which stores stocks in doubly-linked list. 2) Portfolio class has load and store functions to keep all its stocks on files. 3) How do you prove that the portfolio is indeed linked correctly in both directions? (Want one answer? highlight next line) how about print and reverse print? 4) Create your own test data such that you can demonstrate stock insertion and deletion at the beginning / middle / end of the portfolio.
Using a doubly linked list, create a list L1 with words from a text file in Java.
Exercise-2: 15 points Develop a node class and a doubly list class. The node class should have two state variables namely data and nextNode. The doubly list class should contain the following methods: Middlelnsert- insert a node somewhere in the middle of the list Startinsert-insert a node at start of the Linked list Endinsert- insert a node at the end of the Linked list Delete-delete a node Traverse-prints all the node's data Reverse-reverses the linked list . . Note: Choose...
[C++] Create three functions for a singly linked list: - Function 1: Insert a string into the linked list - Function 1: Insert a node after a given node (Node* curNodeptr, Node* newNodePtr) - Function 2: Delete the node passed to it by a pointer, it will take in the head and curPtr '(Node*, Node*)' struct Node{ string data; Node *next; };