2 C programs required. The 2nd is just a slight change of the first, not much mo
ID: 3574047 • Letter: 2
Question
2 C programs required. The 2nd is just a slight change of the first, not much more work required.
1.Write a C program to implement a “linked list” of items, where each item is implemented using a structure type variable with the members of the structure representing the different features of an item. Create a linked list with 5 items, each item having 4 members, 1) item id, 2) item weight, 3) item price and 4) a pointer to the next item. Your program should also list all the items with their first three parameters on the screen.
2. Modify the program to demonstrate 1) how to delete an item (say the third) from the list and then display the list with 4 items and 2) how to add a new item (say, in between the 2nd and 3rd) and display the list of 6 items.
Thank you for the help!
Explanation / Answer
/*
* C Program to Implement Singly Linked List using Dynamic Memory Allocation
*/
#include <stdio.h>
#include <stdlib.h>
#define ISEMPTY printf(" EMPTY LIST:");
/*
* Item Declaration
*/
struct item
{
int id;
double weight;
double price;
struct item *next;
};
typedef struct item snode;
snode *newnode, *ptr, *prev, *temp;
snode *first = NULL, *last = NULL;
snode* create_node(int,double,double);
void insert_node_first();
void insert_node_last();
void insert_node_pos();
void delete_pos();
void display();
/*
* Main :contains menu
*/
int main()
{
int ch;
char ans = 'Y';
while (ans == 'Y'||ans == 'y')
{
printf(" --------------------------------- ");
printf(" Operations on singly linked list ");
printf(" --------------------------------- ");
printf(" 1.Insert node at first");
printf(" 2.Insert node at last");
printf(" 3.Insert node at position");
printf(" 4.Delete Node from any Position");
printf(" 5.Display List");
printf(" 6.Exit ");
printf(" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ");
printf(" Enter your choice");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf(" ...Inserting node at first... ");
insert_node_first();
break;
case 2:
printf(" ...Inserting node at last... ");
insert_node_last();
break;
case 3:
printf(" ...Inserting node at position... ");
insert_node_pos();
break;
case 4:
printf(" ...Deleting Node from any Position... ");
delete_pos();
break;
case 5:
printf(" ...Displaying List... ");
display();
break;
case 6:
printf(" ...Exiting... ");
return 0;
break;
default:
printf(" ...Invalid Choice... ");
break;
}
printf(" YOU WANT TO CONTINUE (Y/N)");
scanf(" %c", &ans);
}
return 0;
}
/*
* Creating Item
*/
snode* create_node(int id,double weight,double price)
{
newnode = (snode *)malloc(sizeof(snode));
if (newnode == NULL)
{
printf(" Memory was not allocated");
return 0;
}
else
{
newnode->id = id;
newnode->weight = weight;
newnode->price = price;
newnode->next = NULL;
return newnode;
}
}
/*
* Inserting Item at First
*/
void insert_node_first()
{
int id;
double weight;
double price;
printf(" Enter the id for the item: ");
scanf("%d", &id);
printf(" Enter the weight for the item: ");
scanf("%lf", &weight);
printf(" Enter the price for the item: ");
scanf("%lf", &price);
newnode = create_node(id,weight,price);
if (first == last && first == NULL)
{
first = last = newnode;
first->next = NULL;
last->next = NULL;
}
else
{
temp = first;
first = newnode;
first->next = temp;
}
printf(" ----INSERTED----");
}
/*
* Inserting Item at Last
*/
void insert_node_last()
{
int id;
double weight;
double price;
printf(" Enter the id for the item: ");
scanf("%d", &id);
printf(" Enter the weight for the item: ");
scanf("%lf", &weight);
printf(" Enter the price for the item: ");
scanf("%lf", &price);
newnode = create_node(id,weight,price);
if (first == last && last == NULL)
{
first = last = newnode;
first->next = NULL;
last->next = NULL;
}
else
{
last->next = newnode;
last = newnode;
last->next = NULL;
}
printf(" ----INSERTED----");
}
/*
* Inserting Node at position
*/
void insert_node_pos()
{
int pos, cnt = 0, i;
int id;
double weight;
double price;
printf(" Enter the id for the item: ");
scanf("%d", &id);
printf(" Enter the weight for the item: ");
scanf("%lf", &weight);
printf(" Enter the price for the item: ");
scanf("%lf", &price);
newnode = create_node(id,weight,price);
printf(" Enter the position ");
scanf("%d", &pos);
ptr = first;
while (ptr != NULL)
{
ptr = ptr->next;
cnt++;
}
if (pos == 1)
{
if (first == last && first == NULL)
{
first = last = newnode;
first->next = NULL;
last->next = NULL;
}
else
{
temp = first;
first = newnode;
first->next = temp;
}
printf(" Inserted");
}
else if (pos>1 && pos<=cnt)
{
ptr = first;
for (i = 1;i < pos;i++)
{
prev = ptr;
ptr = ptr->next;
}
prev->next = newnode;
newnode->next = ptr;
printf(" ----INSERTED----");
}
else
{
printf("Position is out of range");
}
}
/*
* Delete Item from specified position in a non-empty list
*/
void delete_pos()
{
int pos, cnt = 0, i;
if (first == NULL)
{
ISEMPTY;
printf(":No item to delete ");
}
else
{
printf(" Enter the position of value to be deleted:");
scanf(" %d", &pos);
ptr = first;
if (pos == 1)
{
first = ptr->next;
printf(" Element deleted");
}
else
{
while (ptr != NULL)
{
ptr = ptr->next;
cnt = cnt + 1;
}
if (pos > 0 && pos <= cnt)
{
ptr = first;
for (i = 1;i < pos;i++)
{
prev = ptr;
ptr = ptr->next;
}
prev->next = ptr->next;
}
else
{
printf("Position is out of range");
}
free(ptr);
printf(" Element deleted");
}
}
}
/*
* Displays non-empty List from Beginning to End
*/
void display()
{
if (first == NULL)
{
ISEMPTY;
printf(":No items in the list to display ");
}
else
{
for (ptr = first;ptr != NULL;ptr = ptr->next)
{
printf("%d %lf %lf ", ptr->id,ptr->weight,ptr->price);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.