Please answer with link list. wnloads/HW3.pdf Homework 3 Due October 10th, 2018
ID: 3756175 • Letter: P
Question
Please answer with link list.
wnloads/HW3.pdf Homework 3 Due October 10th, 2018 You will need to submit a written report with part of your codes and your source code via Blackboard. You also need to bring in a hard copy of the report with part of your codes to the class on the day that this is due. Name your header file as LastName(3 to 5 letters) FirstNameInitial HW3.h and your implementation file as LastName(3 to 5 letters) FirstNamelnitial HW3.cpp Note: You can only use iostream, cassert, cctype, cmath, cstdio, and cstdlib. 1. Create a class Node a. Create a Node with data, i.e. constructor b. The basic class Node functions, i.e. set, get, link). etc. 2. Create a singly linked list. Create class/functions that a. Insert a Node in the front b. Delete first Node c. Insert Node at the end d. Delete last Node e. Return the size of the list f. Delete all Node g. Print all Node h. Insert a Node at position i i. Delete a Node at position i Overload your insert functions so it can directly insert data k. Get the Node at position i, i.e. locate 1. Get the Node with target data, i.e. search m. Check if there a cycle n. Swap two Node at position i and i+1, change the link and not the data o. Swap two Node at position i and j, change the link and not the data p. Reverse the whole list q. (Extra Credit) Sort without creating a new list Your report should consist of, but not limited to: .Briefly explain what this function does . Briefly explain how this function works, if applicable . Pre- and Post-condition, if applicable . Worst case time complexity Example structures of the report I. An overview of this homework 2. Code for question I 3. Explanation of question 1 4. Code for question2 5. Explanation of question 2Explanation / Answer
2 . Solution for question is as follows:
#include<studio.h>
#include<conio.h>
strict node
{
int data;
strict node* next;
};
int x=0, c=0;
strict node* newnode;
strict node* start=NULL;
struct node* temp;
struct node* prev;
struct node* getnode();
void insertbeg();
void insertpos();
void inserted();
void deletebeg();
void deleteend();
void deletepos();
int size();
void getnodeatpos();
void main()
{
int Ch;
printed("1.insert at beginning 2.insert at a position 3.insert at end 4.delete at beginning 5.delete at end 6.delete at position 7.size 8.get node at particular pos 9.search for a particular data ");
while(1)
{
printed("enter the choice");
scandal("%d",&ch);
switch(ch)
{
case 1 : insertbeg();
break;
case 2 : insertpos();
break;
case 3 : insertend();
break;
case 4 : deletebeg();
break;
case 5 : deleteend();
break;
case 6 : deletepos();
break;
case 7 : x= size();
printf ("the size of list is %d " ,x);
break;
case 8 : getnodeatpos();
break;
case 9: search();
break;
default: exit(0);
}
getch();
}
}
struct node *getnode ()
{
newnode = (struct node*) malloc(sizeof(struct node));
printed("enter the data");
scanf("%d", &newnode->data);
newnode->next= NULL;
return newnode;
}
void insertbeg()
{
newnode= getnode();
if(start==NULL)
start=newnode;
else
{
temp=start;
start= newnode;
newnode ->next = temp;
}
}
void insertend()
{
newnode= getnode();
if( start== NULL)
start= newnode;
else
{
temp= start;
while( temp -> next != NULL)
{
temp = temp -> next;
}
temp-> next= newnode;
}
}
void insertpos()
{
int i, pos;
newnode= getnode ();
if( start== NULL)
start= newnode();
else
{
printed("enter the position");
scanf(%d",&pos);
prev=start;
temp= start;
for(i = 1; i<pos ; i++)
{
prev = temp;
temp= temp -> next;
}
prev-> next = newnode;
newnode->next= temp;
}
}
void deletebeg()
{
if(start==NULL)
printed("empty");
else
{
temp= start;
start= temp-> next;
free(temp);
}
}
void deleteend()
{
if(start==NULL)
printed("empty");
else
{
prev= start;
temp= start;
while(temp->next!= NULL)
{
prev= temp;
temp=temp->next;
}
prev->next= NULL;
free( temp);
}
}
void deletepos()
{
if(start==NULL)
printed("empty");
else
{
int i,pos;
printed("enter the position");
scanf("%d", &pos);
temp=start;
prev= start;
for(i=1;i<pos ;i ++)
{
prev=temp;
temp= temp->next;
}
prev -> next = temp-> next;
temp-> next = NULL;
free(temp);
}
}
int size()
{
c=0;
if(start== NULL)
printf("size is 0");
else
{
temp=start;
while(temp->next != NULL)
{
c++;
temp=temp->next ;
}
c++;
}
return c;
}
void getnodeatpos()
{
int pos,count, c=0;
count= size();
printf("enter the position");
scanf("%d", &pos);
if(start== NULL )
printf("empty");
else{
temp=start;
while(temp -> next != NULL)
{
c++;
if(c== pos)
printf("%d", temp->data);
temp = temp->next;
}
if( pos==count)
printf ("%d", temp->data);
else if( pos>count)
printf("the position is not avilable") ;
else
printf("the position is invalid ") ;
}
}
void search()
{
int y;
printf("enter the data you want to search ") ;
scanf("%d", &y);
if(start== NULL)
printf("empty");
else
{
temp= start;
while( temp-> next != NULL)
{
count++;
if(temp->data == y)
{
printf("%d" , temp-> data);
printf("the position of node required is %d" ,count);
}
temp=temp->next ;
}
count++;
if(temp->data == y)
{
printf("%d" temp-> data);
printf("the position of node required is %d ", count);
}
}}
This program is done using linked list concept . The single linked list is used here. Here if we want to reverse the list, it is not possible in case if single linked list . Reversing can be done in case of double linked list. Linked list takes very less time to execute and performs more operations compared to an array.