Document the data members of the ListNode and List classes. Remember to include
ID: 3531656 • Letter: D
Question
Document the data members of the ListNode and List classes. Remember to include the class invariant.
Implement the ListNode and List classes, placing your implementation in the ListLinked.cpp
Test your implementation of the List ADT using the test program in the file test5.cpp. This program allows you to interactively test your implementation of the List ADT using the following commands.
Test case Commands Expected result Checked
Insert at end +a +b +c +d a b c d
Travel from beginning < N N a b c d
Travel from end > P P a b c d
Delete middle data item
Explanation / Answer
Singly linked list!!
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct sll
{
int data;
struct sll *next;
}node;
int n;
node *head;
void create();
void insert();
void delete();
void display();
int main()
{
int choice;
do
{
printf(" ************* singly linked list ******************* ");
printf(" 1.create 2.insert 3.delete 4.display 0.exit");
printf(" enter u r choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
delete();
break;
case 4:
display();
break;
default:
exit(0);
}
}while(choice<=4); }
void create()
{
int i;
node *new,*temp;
printf(" enter the number of datas");
scanf("%d",&n);
head=(node*)malloc(sizeof(node));
temp=head;
printf(" enter the datas: ");
scanf("%d",&head->data);
for(i=2;i<=n;i++)
{
new=(node*)malloc(sizeof(node));
scanf("%d",&new->data);
temp->next=new;
temp=temp->next;
}
temp->next=NULL;
display();
}
void display()
{
node *temp;
temp=head;
do
{
printf("%d=> ",temp->data);
temp=temp->next;
}
while(temp!=NULL);
getch();
}
void insert()
{
node *temp,*new;
int loc,data,i;
printf(" enter the data: ");
scanf("%d",&data);
printf(" enter the location: ");
scanf("%d",&loc);
new=(node*)malloc(sizeof(node));
if(loc==1)
{
new->data=data;
new->next=head;
head=new;
}
else
{
temp=head;
for(i=2;(i<loc&&(temp->next!=NULL));i++)
temp=temp->next;
new->data=data;
new->next=temp->next;
temp->next=new;
}
n++;
printf(" After Insert ");
display();
}
void delete()
{
node *new,*temp,*prev;
int n;
printf(" enter the no: ");
scanf("%d",&n);
temp=head;
while(temp->data!=n)
{
prev=temp;
temp=temp->next;
}
prev->next=temp->next;
free(temp);
printf(" After delete ");
display();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.