Use the code given to create the following function: 1. Add a function that Inse
ID: 3727586 • Letter: U
Question
Use the code given to create the following function:
1. Add a function that Insert in the Linkedlist.
2. Modify Add a function that will enable you to Enter more items in insert function using (Y/N):
E.g.
Enter your Choice: 1
Enter number to add to list: 12
Enter more(y/n): y
Enter number to add to list: 13
Enter more(y/n): n
3. Add a function ( Count() ) that count total element added in Linkedlist
4. Sort the nodes in the linked list
5. Delete from front/head of list
6. Delete from lasVtail of list
The following in the menu
Singly Linked List Menu:
1. Create or Append List
2. Insert in Beginning
3. Insert in Middle
4. Remove from the List (Head/Tail) H/T or you can write a separate function for it
5. Count Element in List
6. Show List of Elements
7. Exit
All the Singly Linked List operations are performed.
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void insertAtBeginning(int);
void insertBetween(int,int,int);
void display();
void removeBeginning();
void removeEnd();
void sort_ascend();
int count();
struct Node
{
int data;
struct Node *next;
}*head = NULL;
void main()
{
int choice,value,choice1,loc1,loc2;
clrscr();
while(1){
mainMenu: printf(" ********* MENU ************ 1. Insert 2. Display 3. Delete 4.Count 5.Sort 6.Exit Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
printf(“enter more:y/n”);
scanf(“%d”,&ans);
if(ans==y)
goto menuMore;
else
goto mainMenu;
while(1){
menuMore:printf("Where you want to insert: 1. At Beginning 2. At End 3. Between Enter your choice: ");
scanf("%d",&choice1);
switch(choice1)
{
case 1: insertAtBeginning(value);
break;
case 2: printf("Enter the two values where you wanto insert: ");
scanf("%d%d",&loc1,&loc2);
insertBetween(value,loc1,loc2);
break;
default: printf(" Wrong Input!! Try again!!! ");
goto mainMenu;
}
goto subMenuEnd;
}
subMenuEnd:
break;
case 2: display();
break;
case 3: printf("How do you want to Delete: 1. From Beginning 2. From End 3. Spesific Enter your choice: ");
scanf("%d",&choice1);
switch(choice1)
{
case 1: removeBeginning();
break;
case 2: removeEnd(value);
break;
default: printf(" Wrong Input!! Try again!!! ");
goto mainMenu;
}
break;
casae 4: count();
break;
case 5: sort_ascend();
break;
case 6: exit(0);
default: printf(" Wrong input!!! Try again!! ");
}
}
}
void insertAtBeginning(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(head == NULL)
{
newNode->next = NULL;
head = newNode;
}
else
{
newNode->next = head;
head = newNode;
}
printf(" One node inserted!!! ");
}
void insertBetween(int value, int loc1, int loc2)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(head == NULL)
{
newNode->next = NULL;
head = newNode;
}
else
{
struct Node *temp = head;
while(temp->data != loc1 && temp->data != loc2)
temp = temp->next;
newNode->next = temp->next;
temp->next = newNode;
}
printf(" One node inserted!!! ");
}
void removeBeginning()
{
if(head == NULL)
printf(" List is Empty!!!");
else
{
struct Node *temp = head;
if(head->next == NULL)
{
head = NULL;
free(temp);
}
else
{
head = temp->next;
free(temp);
printf(" One node deleted!!! ");
}
}
}
void removeEnd()
{
if(head == NULL)
{
printf(" List is Empty!!! ");
}
else
{
struct Node *temp1 = head,*temp2;
if(head->next == NULL)
head = NULL;
else
{
while(temp1->next != NULL)
{
temp2 = temp1;
temp1 = temp1->next;
}
temp2->next = NULL;
}
free(temp1);
printf(" One node deleted!!! ");
}
}
void display()
{
if(head == NULL)
{
printf(" List is Empty ");
}
else
{
struct Node *temp = head;
printf(" List elements are - ");
while(temp->next != NULL)
{
printf("%d --->",temp->data);
temp = temp->next;
}
printf("%d --->NULL",temp->data);
}
}
int count()
{
struct node *n;
int c=0;
n=head;
while(n!=NULL)
{
n=n->next;
c++;
}
return c;
}
{
for (ptr = first;ptr != NULL;ptr = ptr->next)
{
for (nxt = ptr->next;nxt != NULL;nxt = nxt->next)
{
if (ptr->value > nxt->value)
{
t = ptr->value;
ptr->value = nxt->value;
nxt->value = t;
}
}
}
printf(" ---Sorted List---");
for (ptr = first;ptr != NULL;ptr = ptr->next)
{
printf("%d ", ptr->value);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.