Question

In C program

1. Create a Linked List of type Float. (including the functions specified below) a. Insertion i. at the Beginning, ii. at the

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C-program to illustrate inserting and deleting from linklist

#include<stdio.h>

#include<stdlib.h>

//Link list structure for of type float start is the starting of our linklist

struct linkList

{

float data;

struct linkList *next;

}*start=NULL,*q,*temp,*prev;

//This function inserts new value at the start of the linklist

void insertAtBegin()

{

float num;

//create temporary objet to hold new data

temp = (struct linkList*)malloc(sizeof(struct linkList));

printf("Enter data: ");

scanf("%f",&num);

temp->data = num;

temp->next = NULL;

//If list is empty

if(start == NULL){

temp->next = NULL;

start = temp;

}else{

temp->next = start; //appends all list to end of temp

start = temp; //assign new start

}

}

//This function inserts new value at the end of the linklist

void insertAtEnd()

{

float num;

//create temporary objet to hold new data

temp=(struct linkList*)malloc(sizeof(struct linkList));

printf("Enter data: ");

scanf("%f",&num);

temp->data = num;

temp->next = NULL;

//If list is empty

if(start == NULL){

start = temp;

}else{

q = start;

while(q->next!=NULL) //traverse list upto the last value

q = q->next;

q->next = temp; //attach temp data to the list at end

}

}

//This function inserts new value After the node pass in parameter linklist

int insertAfterValue(float nodeValue)

{

float num;

if(start == NULL){

printf("List is empty!! Fail to insert");

return 0;

}

//create temporary objet to hold new data

temp = (struct linkList*)malloc(sizeof(struct linkList));

printf("Enter data:");

scanf("%f",&num);

temp->data=num;

temp->next = NULL;

q = start;

if(q->data == nodeValue){

temp->next = q->next;

q->next = temp;

return 0;

}

//traverse list upto the last value and node not holds nodeVlue

while(q!=NULL && q->data != nodeValue){

q = q->next;

}

if(q == NULL)

printf("Node Value Not Found Fail to insert");

else{

temp->next = q->next;

q->next = temp ;//attach temp data to next value to node

}

return 0;

}

//This is Display the list

void display()

{

if(start == NULL){

printf("Link List is empty!!");

}else{

q = start;

printf("The Linked list is : \n\t");

//Traverse node by node and print value

while(q != NULL){

if(q->next != NULL){

printf("%.2f-> ", q->data); //%.2f prints output upto 2 decimal places

q = q->next;

}else{

printf("%.2f-> NULL", q->data);

q = q->next;

}

}

}

}

//This function Delete value from the start of the linklist

void deleteFromBegin()

{

if(start == NULL){

printf("The list is empty!!");

}else{

q = start;

start = start->next; //list start pointer move to next element

printf("Deleted element is %.2f ", q->data);

free(q); //free memory

}

}

//This function Delete value from the end of the linklist

void deleteFromEnd()

{

if(start == NULL){

printf("The list is empty!!");

}else{

q = start;

while(q->next->next != NULL) //travese upto second last element

q = q->next;

temp = q->next; //Holds data to be delete

q->next = NULL;

printf("Deleted element is %.2f ", temp->data);

free(temp); //free memory

}

}

//This function delete value of the node pass in parameter linklist

int deleteNode(float nodeValue)

{

if(start == NULL){

printf("List is empty!!");

return 0;

}

q = prev = start;

if(q->data == nodeValue){

temp = q;

start = q->next;

free(temp);

return 0;

}

//traverse list upto the last value and node not holds nodeVlue

while(q!=NULL && q->data != nodeValue){

prev = q;

q = q->next;

}

if(q == NULL)

printf("Node Value Not Found");

else{

temp = q ;

prev->next = q->next ; //attach prev node to next value to node

printf("Deleted element is %.2f ", temp->data);

free(temp); //free memory

}

return 0;

}

int main(){

int ch; //for getting choice from user

float num;

//executing till user enter exit choice

while(1){

//Show all menu option to the user

printf("\n\n**************************************");

printf("\n** Singly Float Linked List Menu **");

printf("\n**************************************\n");

printf("\n1) Insert\n2) Display\n3) Delete\n4) Exit\n\n");

printf("Enter your choice(1-4): ");

scanf("%d",&ch);

switch(ch){

case 1:

//Give option to insert the value in the linklist

printf("\n---- Insert Menu ----");

printf("\n1. at the Beginning,\n2. at the end,\n3. After the node value");

printf("\n\nEnter your choice(1-3): ");

scanf("%d",&ch);

switch(ch)

{

case 1: insertAtBegin();

break;

case 2: insertAtEnd();

break;

case 3:

printf("\nEnter node value to insert after: ");

scanf("%f",&num);

insertAfterValue(num);

break;

default: printf("Wrong Choice!!");

}

break;

case 2: display();

break;

case 3:

//Give option to Delete the value in the linklist

printf("\n---- Delete Menu ----");

printf("\n1. at the Beginning,\n2. at the end,\n3. Node value");

printf("\n\nEnter your choice(1-3): ");

scanf("%d",&ch);

switch(ch)

{

case 1: deleteFromBegin();

break;

case 2: deleteFromEnd();

break;

case 3:

printf("\nEnter node value to delete: ");

scanf("%f",&num);

deleteNode(num);

break;

default: printf("Wrong Choice!!");

}

break;

case 4: exit(0);

default: printf("Wrong Choice!!");

}

}

return 0;

}


Add a comment
Know the answer?
Add Answer to:
In C program 1. Create a Linked List of type Float. (including the functions specified below)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a linked list with the following features. A Node class that stores the data type...

    Create a linked list with the following features. A Node class that stores the data type of your choice. A constructor that creates a dummy header node. void display() -- A method for printing the list. void add(item) -- A method for adding a value to the beginning of the list void addEnd(item) -- A method of adding a value to the end of the list. bool contains(item) -- A method for finding a specified value in the list. int...

  • [C++] Create three functions for a singly linked list: - Function 1: Insert a string into...

    [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; };

  • Create Functions for the following prototypes. Below is the setup.*program is in c* #include <stdio.h> #include...

    Create Functions for the following prototypes. Below is the setup.*program is in c* #include <stdio.h> #include <string.h> #include <stdarg.h> #include <stdlib.h> //#define constant values #define MAX_URL_LENGTH 50 #define TRUE 1 #define FALSE 0 //typedef for the Element struct which constains a c string to store a URL in the BrowserList typedef struct { char szURL[MAX_URL_LENGTH]; } Element; //Typedef for a node in the doubly linked list (has next and previous pointers). typedef struct NodeDL { Element element; struct NodeDL *pNext;...

  • In C++ syntax please Write a program that implements and demonstrates a linked list using functions....

    In C++ syntax please Write a program that implements and demonstrates a linked list using functions. Your program should first dehne a node that stores an integer and then your program will include the following functions appendo- This function accepts the head pointer (by reference) of a linked list and an integer as it's only arguments. It then creates a node, stores the integer argument in the node, and adds it to the end of the list. findo-This function accepts...

  • In C++ Assume entries in a linked list are of type struct listrec: struct listrec {...

    In C++ Assume entries in a linked list are of type struct listrec: struct listrec {             struct listrec    *prev; float                 value;             struct listrec    *next;   }; listrec *head, *tail; Write a main() routine in which the user is asked the number of nodes to create in the list (number greater than or equal to zero) then create the following type of linked list (use a loop to initialize list) based on the number of nodes requested: Write a...

  • C++ Compsci 165: For your twelfth programming assignment you will be implementing a program that uses a linked list.You...

    C++ Compsci 165: For your twelfth programming assignment you will be implementing a program that uses a linked list.You will be implementing the following structure and functions: struct LinkedList { int value; LinkedList *next; }; Note that with a singly linked list, you need to maintain a head pointer (pointer to the beginning of the list). Typically a tail pointer (pointer to the end of the list) is not maintained in a singly linked list (because you can only iterate...

  • Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each...

    Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each node has a parent field as well as the usual left child, right child, and key fields. -Condition : Do not use Array representation. Use the following structure. typedef struct node *treePointer; typedef struct node { int key; treePointer parent; treePointer leftChild, rightChild; }; - INPUT i k : Insert the node with the key value of k in...

  • Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each...

    Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each node has a parent field as well as the usual left child, right child, and key fields. -Condition : Do not use Array representation. Use the following structure. typedef struct node *treePointer; typedef struct node { int key; treePointer parent; treePointer leftChild, rightChild; };    - INPUT i k : Insert the node with the key value of k in...

  • 1) Create a portfolio class which stores stocks in doubly-linked list. 2) Portfolio class has load...

    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.

  • In this lab, using C++, you will create an abstract data type, using a doubly-linked circular...

    In this lab, using C++, you will create an abstract data type, using a doubly-linked circular structure to store the values and links. You must create it by your own and not use any existing containers. You will need a QueueNode. You can use a struct for this, as each node will not have any associated functions. It will have members for data, and the next and previous pointers. Data will be positive integers. There will be no NULL pointers....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT