Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

\"For this question you will need to use the working copy of a linked list imple

ID: 3640838 • Letter: #

Question

"For this question you will need to use the working copy of a linked list
implementation . I suggest you compile and run this code so
that you get a feel for how it works. You should edit this code to implement
the following data structures:"

1. A LIFO (last in rst out) queue or stack.


the working copy

#include
#include
#include "simpio.h"


//function prototypes
void delnode(int num);
void append(int num);
void addbeg(int num);
void addafter(int num, int loc);
void display(void);
int count(void);
void reverse(void);

//Structure containing a data part & a link part

struct node
{
int data;
struct node *next;
}*Head;


// Deletes a node with a particular data value from the list.
// Searches the list for the value num and if found deletes that node.
// Otherwise displays an element not found message
void delnode(int num)
{
struct node *prev_ptr;
struct node *cur_ptr;

cur_ptr=Head;
prev_ptr = NULL;

while(cur_ptr != NULL)
{
if(cur_ptr->data == num)
{
if(cur_ptr==Head)
{
Head=cur_ptr->next;
free(cur_ptr);
return;
}
else
{
prev_ptr->next=cur_ptr->next;
free(cur_ptr);
return;
}
}
else
{
prev_ptr=cur_ptr;
cur_ptr=cur_ptr->next;
}
}

printf(" Element %d is not found in the List", num);
}

//Adds a node at the end of the list

void append(int num)
{
struct node *temp,*r;

temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;

// Copying the Head location into another node.
r=Head;

if (Head == NULL) // If List is empty we create First Node.
{
Head=temp;
Head->next =NULL;
}
else
{ // Traverse down to end of the list.
while(r->next != NULL)
r=r->next;

// Appending at the end of the list.
temp->next=NULL;
r->next =temp;
}
}

// Adds a node at the beginning of the list

void addbeg(int num)
{
struct node *temp;

temp=(struct node *)malloc(sizeof(struct node));
temp->data = num;

if ( Head == NULL)
{
Head=temp;
Head->next=NULL;
}
else
{
temp->next=Head;
Head=temp;
}
}

// Adding a new node at a given location in the list.
// Checks that the location is valid - prints an error message if not
// Otherwise iterates through the list until the desired location is
// found and inserts the new node there

void addafter(int num, int loc)
{
int i;
struct node *temp,*prev_ptr,*cur_ptr;

cur_ptr=Head;

if(loc > count()+1 || loc <= 0)
{
printf(" Insertion at given location is not possible ");
return;
}

if (loc == 1)// if list is null then add at beginning
{
addbeg(num);
return;
}
else
{
for(i=1;i {
prev_ptr=cur_ptr; // t will be holding previous value of r
cur_ptr=cur_ptr->next;
}

temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;

prev_ptr->next=temp;
temp->next=cur_ptr;
}
}

// Displays the values of all nodes in the list
void display()
{
struct node *cur_ptr;

cur_ptr=Head;

if(cur_ptr==NULL)
{
printf(" List is Empty");
return;
}

//traverse the entire linked list

while(cur_ptr!=NULL)
{
printf(" -> %d ",cur_ptr->data);
cur_ptr=cur_ptr->next;
}
printf(" ");
}

// Counts the number of items in the list and returns that number

int count()
{
struct node *cur_ptr;
int c=0;

cur_ptr=Head;

while(cur_ptr!=NULL)
{
cur_ptr=cur_ptr->next;
c++;
}
return(c);
}



//This function reverses the order of nodes in the list

void reverse()
{
struct node *prev_ptr, *cur_ptr, *temp;

cur_ptr=Head;
prev_ptr=NULL;

while(cur_ptr != NULL)
{
temp=prev_ptr;
prev_ptr=cur_ptr;

cur_ptr=cur_ptr->next;
prev_ptr->next=temp;
}

Head=prev_ptr;
}

int main()
{
int i;
Head=NULL;

while(TRUE)
{
printf(" *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*");
printf(" Insert a number 1. At Beginning");
printf(" 2. At End");
printf(" 3. At a Particular Location in List");
printf(" 4. Print the Elements in the List");
printf(" 5. Print number of elements in the List");
printf(" 6. Delete a Node in the List");
printf(" 7. Reverse the linked List");
printf(" 8. Exit");
printf(" *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*");
printf(" Choose Option: ");
i = GetInteger();

switch(i)
{
case 1:
{
int num;
printf(" Enter the Number to insert: ");
num = GetInteger();
addbeg(num);
break;
}
case 2:
{
int num;
printf(" Enter the Number to insert: ");
num = GetInteger();
append(num);
break;
}
case 3:
{
int num, loc, k;
printf(" Enter the Number to insert: ");
num = GetInteger();
printf(" Enter the location Number: ");
loc = GetInteger();
addafter(num,loc);
break;
}
case 4:
{
printf(" Elements in the List: ");
display();
break;
}
case 5:
{
display();
printf(" Total number of Elements in the List: %d",count());
break;
}
case 6:
{
int num;
printf(" Enter the number to be deleted from List: ");
num = GetInteger();
delnode(num);
break;
}
case 7:
{
reverse();
display();
break;
}
case 8:
{
struct node *temp;

while( Head!=NULL)
{
temp = Head->next;
free(Head);
Head=temp;
}
exit(0);
}
default:
{
printf(" Wrong Option choosen");
}
}/* end of while */
}/* end of main */
}

Explanation / Answer

Please rate...


#include<stdio.h>


//function prototypes
void delnode();
void addbeg(int num);
void display(void);
int count(void);
void reverse(void);


//Structure containing a data part & a link part

struct node
{
int data;
struct node *next;
}*Head;


// Deletes a node with a particular data value from the list.
// Searches the list for the value num and if found deletes that node.
// Otherwise displays an element not found message
void delnode()
{
struct node *prev_ptr;
struct node *cur_ptr;

cur_ptr=Head;
prev_ptr = NULL;

while(cur_ptr != NULL)
{
if(cur_ptr==Head)
{
Head=cur_ptr->next;
free(cur_ptr);
return;
}
}
printf(" Element is not found in the List");
}


// Adds a node at the beginning of the list

void addbeg(int num)
{
struct node *temp;

temp=(struct node *)malloc(sizeof(struct node));
temp->data = num;

if ( Head == NULL)
{
Head=temp;
Head->next=NULL;
}
else
{
temp->next=Head;
Head=temp;
}
}

// Displays the values of all nodes in the list
void display()
{
struct node *cur_ptr;

cur_ptr=Head;

if(cur_ptr==NULL)
{
printf(" List is Empty");
return;
}

//traverse the entire linked list

while(cur_ptr!=NULL)
{
printf(" -> %d ",cur_ptr->data);
cur_ptr=cur_ptr->next;
}
printf(" ");
}

// Counts the number of items in the list and returns that number

int count()
{
struct node *cur_ptr;
int c=0;

cur_ptr=Head;

while(cur_ptr!=NULL)
{
cur_ptr=cur_ptr->next;
c++;
}
return(c);
}



//This function reverses the order of nodes in the list

void reverse()
{
struct node *prev_ptr, *cur_ptr, *temp;

cur_ptr=Head;
prev_ptr=NULL;

while(cur_ptr != NULL)
{
temp=prev_ptr;
prev_ptr=cur_ptr;

cur_ptr=cur_ptr->next;
prev_ptr->next=temp;
}

Head=prev_ptr;
}

int main()
{
int i;
Head=NULL;

while(1)
{
printf(" *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*");
printf(" For FIFO");
printf(" 1. Enqueue");
printf(" 2. Dequeue");
printf(" For FILO");
printf(" 3. Delete");
printf(" 4. Print the Elements in the List");
printf(" 5. Print number of elements in the List");
printf(" 6. Exit");
printf(" *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*");
printf(" Choose Option: ");
scanf("%d",&i);

switch(i)
{
case 1:
{
int num;
printf(" Enter the Number to insert: ");
scanf("%d",&num);
addbeg(num);
break;
}
case 2:
{
    reverse();
    delnode();
    reverse();
    break;
}
case 3:
{
    delnode();
    break;
}
case 4:
{
printf(" Elements in the List: ");
display();
break;
}
case 5:
{
display();
printf(" Total number of Elements in the List: %d",count());
break;
}
case 6:
{
struct node *temp;
while( Head!=NULL)
{
temp = Head->next;
free(Head);
Head=temp;
}
exit(0);
}
default:
{
printf(" Wrong Option choosen");
}
}/* end of while */
}/* end of main */
}