1. Write aprogram that will allow a user to add, delete, and search for items in
ID: 3618494 • Letter: 1
Question
1. Write aprogram that will allow a user to add, delete, and search for
items in a linked list. For this assignment, considerusing datatype
char, however you may use any type you wish. Be sureto instruct the
user of the datatype, and consider even the range ofvalues you expect
them to enter.
Present the user with the following options:
------------------------------------
1 - add to the end of the list
2 - add to the beginning of the list
3 - delete an entry
4 - display the list
5 - exit
Enter choice:
------------------------------------
2. Choices1 and 2 are self-explanatory.
For option 3, prompt the user for a value to deleteand advise the
user whether the item was successfully deleted or notfound.
For option 4, display the contents of the listseparated by one space.
Insure the user can only exit the program by choosingoption 5.
Explanation / Answer
/**
* Function for finding the size of the list
*/
int listSize()
{
struct Node *curNode;
int count=0;
curNode=Head;
while(curNode != NULL)
{
curNode=curNode->link;
count++;
}
return(count);
}
/**
* Deleting a node from List depending upon the node location in the list.
*/
int deleteNode(int nNodePosition)
{
struct Node *prevNode, *curNode;
int i;
curNode=Head;
if(nNodePosition > (listSize()) || nNodePosition <= 0)
{
printf(" Can't delete the node from given location ");
}
else
{
// If the location is starting of the list
if (nNodePosition == 1)
{
Head=curNode->link;
free(curNode);
return 0;
}
else
{
for(i=1;i<nNodePosition;i++)
{
prevNode=curNode;
curNode=curNode->link;
}
prevNode->link=curNode->link;
free(curNode);
}
}
return 1;
}
/**
* inserting the node at the end of the list
*/
void insertEnd(int data)
{
struct Node *newNode, *tempHeader;
newNode=(struct Node *)malloc(sizeof(struct Node));
newNode->data=data;
newNode->link=NULL;
tempHeader=Head;
if(Head == NULL)
{
//Creating the first node
Head=newNode;
}
else
{
while(tempHeader->link != NULL)
tempHeader=tempHeader->link;
tempHeader->link=newNode;
}
}
/**
* inserting the node at the begining of the list
*/
void insertBegining(int data)
{
struct Node *newNode;
newNode=(struct Node *)malloc(sizeof(struct Node));
newNode->data = data;
if (Head == NULL)
{
Head=newNode;
Head->link=NULL;
}
else
{
newNode->link=Head;
Head=newNode;
}
}
/**
* Adding the node at a particular position
*/
void insertAt(int data, int nNodePostion)
{
int i;
struct Node *newNode, *prevNode, *currentNode;
currentNode=Head;
if(nNodePostion > (listSize()+1) || nNodePostion <= 0)
{
printf(" Can't insert the node at given position ");
}
else
{
if (nNodePostion == 1)
{
insertBegining(data);
}
else
{
for(i=1;i<nNodePostion;i++)
{
prevNode=currentNode;
currentNode=currentNode->link;
}
newNode=(struct Node *)malloc(sizeof(struct Node));
newNode->data=data;
prevNode->link=newNode;
newNode->link=currentNode;
}
}
}
/**
* Displaying the contents of the list
*/
void displayList()
{
struct Node *currNode;
&nbs
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.