Question

Input the data for node 1 1 Enter more yes/Kno ? n Display Insert First Insert Last Insert Anywhere Delete First Delete Last

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

Code in C language:

#include <stdio.h>
#include<malloc.h>
struct node
{
int value;
struct node *next;
};
typedef struct node Node;

Node* insertBegin(Node* head,int n)
{  
   struct node *newnode=(struct node*)malloc(sizeof(struct node));
   newnode->value=n;
   newnode->next=head;
   head=newnode;
   return head;
}
Node* inserAtLocation(Node *head, int n, int pos)
{
if(pos == 1){           //if the inserted position is 1
return (insertBegin(head,n));
}
   int count=1;
   struct node *newnode=(struct node*)malloc(sizeof(struct node));
   newnode->value=n;
   newnode->next=NULL;
   if(!head){
head = newnode;
return head;
}
  
   struct node *temp=head;
   while(count != pos-1)           //go to position's previous element
   {
       temp=temp->next;
       count++;
   }
   newnode->next=temp->next;
   temp->next=newnode;
  
   return head;
}
Node* insertEnd(Node* head, int n){
Node* temp = head;

struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->value = n;
newnode->next = NULL;
if(!head){           //if no data is present in list, insert at begin
head = newnode;
return head;
}
while(temp->next){           //else go to last node
temp = temp->next;
}
temp->next = newnode;
return head;
}

Node* delAtLoc(Node* head,int pos)
{                       //if no head
   if(head==NULL)
       return head;
   if(head->next==NULL)
   {                   //if only one node is present
       free(head);
       head=NULL;
       return head;
   }
   int count=1;
   Node* temp=head;
   while(count!=pos-1)
   {
       temp=temp->next;
   }
   struct node *t=temp->next;
   temp->next=temp->next->next;
   free(t);
   return head;
}

Node *delBegin(Node *head)
{
   if(head==NULL)
       return head;   //if no head
   if(head->next==NULL)
   {                   //if only one element present
       free(head);
       head=NULL;
       return head;
   }
   Node* temp=head;
   head=head->next;
   free(temp);
   return head;
}

Node *delEnd(Node *head)
{
   if(head==NULL)       //if no data
       return head;
   if(head->next==NULL)
   {                       //if only one element present
       free(head);
       head=NULL;
       return head;
   }
   Node* temp=head;
   while(temp->next->next)
       temp=temp->next;
   struct node *t=temp->next;
   temp->next=NULL;
   free(t);
   return head;
}
printList(Node* head){
if(!head){           //no data to print condition
printf("No data to print\n");
}
while(head){
printf("%d ",head->value);
head = head->next;
}
printf("\n");
}
searchNode(Node* head,int val){
int flag = 0;
while(head){
if(head->value == val){       //condition checking if present
flag = 1;
break;
}
head = head->next;
}
if(flag == 1)
printf("Value found in the linked list\n");
else
printf("Value not found in the linked list\n");
}
int main ()
{
int loc,data;
Node* start = NULL;
char opt;
do{
   printf("=================================\n");
printf("a. Display\n");
printf("b. Insert at First Location\n");
printf("c. Insert at Last Location\n");
printf("d. Insert at Given Location\n");
printf("e. Delete from First Location\n");
printf("f. Delete from Last Location\n");
printf("g. Delete from Given Location\n");
printf("h. Search (based on data)\n");
printf("i. Exit\n");
printf("Enter Option to Select: ");
scanf("\n%c",&opt);
switch(opt){
case 'a':
printf("Printing the list: \n");
printList(start);
break;
case 'b':
printf("Enter data to insert: ");
scanf("%d",&data);
start = insertBegin(start,data);
break;
case 'c':
printf("Enter data to insert: ");
scanf("%d",&data);
start = insertEnd(start,data);
break;
case 'd':
printf("Enter data to insert: ");
scanf("%d",&data);
printf("Enter the Location to insert: ");
scanf("%d",&loc);
start = inserAtLocation(start,data,loc);
break;
case 'e':
start = delBegin(start);
break;
case 'f':
start = delEnd(start);
break;
case 'g':
printf("Enter the Location to delete: ");
scanf("%d",&loc);
start = delAtLoc(start,loc);
break;
case 'h':
printf("Enter data to search: ");
scanf("%d",&data);
searchNode(start,data);
break;
}
  
} while(opt != 'i');
return 0;
}

Output:

a. Display b. Insert at First Location c. Insert at Last Location d. Insert at Given Location e. Delete from First Location f

Add a comment
Know the answer?
Add Answer to:
Input the data for node 1 1 Enter more yes/Kno ? n Display Insert First Insert...
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
  • write a C program to make the dictionary USING LINKED LISTS and STORE DATA in File HANDLING..... you have to create functions 1. insert 2. delete 3. search(if we search with any letter then the words...

    write a C program to make the dictionary USING LINKED LISTS and STORE DATA in File HANDLING..... you have to create functions 1. insert 2. delete 3. search(if we search with any letter then the words with same first letter have been shown) 4. display 6. exit Appearance should be like proper dictionary.

  • write a ContactBook in C++ ContactBook that holds 10 names with that names corresponding contact (last...

    write a ContactBook in C++ ContactBook that holds 10 names with that names corresponding contact (last name and first name of the owner). Ask the user to input up to 10 Contacts (It may be less. The user should have the ability to stop inputting Contacts whenever he wishes). 3.This class will store a list of Contacts in a stack allocated array of default capacity 10. 4.You must be able to add new contacts to a list, delete old contacts,...

  • QUESTION 1 In a tree, a ____ is a node with successor nodes. root child descendent...

    QUESTION 1 In a tree, a ____ is a node with successor nodes. root child descendent parent sibling QUESTION 2 In a tree, the ____ is a measure of the distance from a node to the root. count degree branch height level QUESTION 3 Which of the following is not a characteristic of a binary search tree? Each node has zero, one, or two successors. The preorder traversal processes the node first, then the left subtree, and then the right...

  • Build a singly linked list with 10 noes. The data are "This is my first project...

    Build a singly linked list with 10 noes. The data are "This is my first project in Data structure and algorithm". First node is "This" second "is" and so on. Build insert function, insert "the " before "Data" and delete function, delete "my" before "first". Search "in" and output it location and then append "C++" and output the linked list again. In writing this program use menu which includes ”1. Create; 2. Insert; 3. Delete; 4. Append; 5. Search; 6....

  • a. Define the struct node that can store a name and up to 3 phone numbers...

    a. Define the struct node that can store a name and up to 3 phone numbers (use an array for the phone numbers). The node struct will also store the address to the next node. b. Define a class contactList that will define a variable to keep track of the number of contacts in the list, a pointer to the first and last node of the list. c. Add the following methods to the class: i. Add a new contact....

  • I have a C++ code that lets me enter, display and delete a student record. I...

    I have a C++ code that lets me enter, display and delete a student record. I need to implement a function that prints the average grade score of the students I input. Below is my code and a picture of how my code looks right now. #include<iostream> #include<stdlib.h> using namespace std; //Node Declaration struct node {    string name;    string id;    int score;    node *next;   }; //List class class list {        private:        //head...

  • C++ 1. Please use attached script for your reference to create the node structure and a...

    C++ 1. Please use attached script for your reference to create the node structure and a linked list class, say LinkedList, that has the following basic member methods:     constructor, destructor//IMPORTANT, display(), add_node(). 2. Please implement the following additional member methods:     Please feel free to change T with any data type you'd like to use for your node stricture's data type. -- addFirst(T data) // Adds an node with data at the beginning of the list -- pop() //...

  • 14.3: More Sentences Write a program that allows a user to enter a sentence and then...

    14.3: More Sentences Write a program that allows a user to enter a sentence and then the position of two characters in the sentence. The program should then report whether the two characters are identical or different. When the two characters are identical, the program should display the message: <char> and <char> are identical! Note that <char> should be replaced with the characters from the String. See example output below for more information. When the two characters are different, the...

  • Write a MIPS assembly language program that uses dynamic memory allocation to create and manage a...

    Write a MIPS assembly language program that uses dynamic memory allocation to create and manage a linked list data structure. Gives the user the following options:                         1. To create and add the first node to a linked list.          2. To add a single node to the pre-existing linked list.             The list must already exist before a new node can be added.            The nodes should be maintained in ascending order based on the data value within the nodes...

  • Write a program that performs the following: 1. Presents the user a menu where they choose...

    Write a program that performs the following: 1. Presents the user a menu where they choose between:              a. Add a new student to the class                           i. Prompts for first name, last name                           ii. If assignments already exist, ask user for new student’s scores to assignments              b. Assign grades for a new assignment                           i. If students already exist, prompt user with student name, ask them for score                           ii. Students created after assignment will need to...

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