C programming. Please do not copy from other answer Checkpoint 1 Add another poi
ID: 3728562 • Letter: C
Question
C programming. Please do not copy from other answer
Checkpoint 1
Add another pointer variable to the struct node data-structure as follows: struct node *prev;
Check at: https://www.thecodingdelight.com/doubly-linked-list/
It illustrates the different operations that we want to implement into our doubly linked list and also provides working code in Java, JavaScript and C++ where the C++ will be most useful to implement the different functions in this lab.
Checkpoint 2
Implement the following functions:
(i) insert(int num): Insert num to the front/back of the linked list.
(ii) Display the doubly linked list.
Checkpoint 3
Implement the following functions:
• insertAfter(int num, int index): Insert num after item at a particular index.
Checkpoint 4
• remove(int num): Search from the start of the list for the passed in num and remove it from the list, if found.
• remove(int index): Remove num at a specific index.
Checkpoint 5
Do we have to update the count() function to work with the doubly linked list?
Code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
void append(int num)
{
struct node *temp,*right;
temp= (struct node *)malloc(sizeof(struct node));
temp->data=num;
right=(struct node *)head;
if(right==NULL)
{
add(num)
}
else
{
while(right->next != NULL)
right=right->next;
right->next =temp;
right=temp;
right->next=NULL;
}
}
void add( 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;
}
}
void addafter(int num, int loc)
{
int i;
struct node *temp,*left,*right;
right=head;
for(i=1;i<loc;i++)
{
left=right;
right=right->next;
}
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
left->next=temp;
left=temp;
left->next=right;
return;
}
void insert(int num)
{
int c=0;
struct node *temp;
temp=head;
if(temp==NULL)
{
add(num);
}
else
{
while(temp!=NULL)
{
if(temp->data<num)
return;
if(temp->data<num)
c++;
temp=temp->next;
}
if(c==0)
add(num);
else if(c<count())
addafter(num,++c);
else
append(num);
}
}
int delete(int num)
{
struct node *temp, *prev;
temp=head;
while(temp!=NULL)
{
if(temp->data==num)
{
if(temp==head)
{
head=temp->next;
free(temp);
return 1;
}
else
{
prev->next=temp->next;
free(temp);
return 1;
}
}
else
{
prev=temp;
temp= temp->next;
}
}
return 0;
}
void display(struct node *r)
{
r=head;
if(r==NULL)
{
return;
}
while(r!=NULL)
{
printf("%d ",r->data);
r=r->next;
}
printf(" ");
}
int count()
{
struct node *n;
int c=0;
n=head;
while(n!=NULL)
{
n=n->next;
c++;
}
return c;
}
int main()
{
int i,num;
struct node *n;
head=NULL;
while(1)
{
printf(" List Operations ");
printf("=============== ");
printf("1.Insert ");
printf("2.Display ");
printf("3.Size ");
printf("4.Delete ");
printf("5.Exit ");
printf("Enter your choice : ");
if(scanf("%d",&i)<=0){
printf("Enter only an Integer ");
exit(0);
} else {
switch(i)
{
case 1: printf("Enter the number to insert : ");
scanf("%d",&num);
// insert(num);
append(num);
break;
case 2: if(head==NULL)
{
printf("List is Empty ");
}
else
{
printf("Element(s) in the list are : ");
}
display(n);
break;
case 3: printf("Size of the list is %d ",count());
break;
case 4: if(head==NULL)
printf("List is Empty ");
else{
printf("Enter the number to delete : ");
scanf("%d",&num);
if(delete(num))
printf("%d deleted successfully ",num);
else
printf("%d not found in the list ",num);
}
break;
case 5: return 0;
default: printf("Invalid option ");
}
}
}
return 0;
}
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
struct node *prev;
}*head;
void insert_front(int num){
struct node *new;
new = (struct node *)malloc(sizeof(struct node));
new->prev = NULL;
new->data = num;
if(head == NULL){
head = new;
new->next = NULL;
}
else{
new->next = head;
head = new;
}
}
void insert_back(int num){
struct node *new,*temp;
new = (struct node *)malloc(sizeof(struct node));
new->next = NULL;
new->data = num;
if(head == NULL){
head = new;
new->prev = NULL;
}
else{
temp = head;
while(temp->next != NULL){
temp = temp->next;
}
new->prev = temp;
temp->next = new;
}
}
void display(){
struct node *temp = head;
while(temp!=NULL){
printf("%d ",temp->data);
temp = temp->next;
}
printf(" ");
}
void insert_after(int num,int index){
struct node *new,*temp;
int i = 0;
new = (struct node *)malloc(sizeof(struct node));
new->next = NULL;
new->data = num;
new->prev = NULL;
temp = head;
while(temp->next != NULL && i < index){
i++;
temp = temp->next;
}
if(i == index){
if(temp->next == NULL){
insert_back(num);
}
else{
new->next = temp->next;
new->prev = temp;
temp->next->prev = new;
temp->next = new;
}
}
}
void remove_num(int num){
struct node *temp;
temp = head;
while(temp != NULL){
if(temp->data == num){
printf("%d ",temp->data );
if(temp->prev == NULL){
head = head->next;
if(head != NULL)
head->prev = NULL;
}
else{
temp->prev->next = temp->next;
if(temp->next != NULL)
temp->next->prev = temp->prev;
}
return;
}
temp = temp->next;
}
}
void remove_index(int index){
struct node *temp;
int i = 0;
temp = head;
while(temp != NULL && i < index){
temp = temp->next;
i++;
}
if(i==index && temp != NULL){
printf("%d ",temp->data);
if(temp->prev == NULL){
head = head->next;
head->prev = NULL;
}
else{
temp->prev->next = temp->next;
if(temp->next != NULL)
temp->next->prev = temp->prev;
}
}
}
int main(){
int i,num,index;
struct node *n;
head=NULL;
while(1){
printf(" List Operations ");
printf("=============== ");
printf("1.Insert Front ");
printf("2.Insert Back ");
printf("3.Display ");
printf("4.Insert After ");
printf("5.Remove Number ");
printf("6.Remove at Index ");
printf("7.Exit ");
printf("Enter your choice : ");
if(scanf("%d",&i)<=0){
printf("Enter only an Integer ");
exit(0);
}
else {
switch(i){
case 1: printf("Enter the number to insert : ");
scanf("%d",&num);
insert_front(num);
break;
case 2: printf("Enter the number to insert : ");
scanf("%d",&num);
insert_back(num);
break;
case 3: if(head==NULL){
printf("List is Empty ");
}
else{
printf("Element(s) in the list are : ");
}
display();
break;
case 4: printf("Enter the number to insert : ");
scanf("%d",&num);
printf("Enter the index to insert after : ");
scanf("%d",&index);
insert_after(num,index);
break;
case 5: printf("Enter the number to remove : ");
scanf("%d",&num);
remove_num(num);
break;
case 6: printf("Enter the index to remove : ");
scanf("%d",&index);
remove_index(index);
break;
case 7: return 0;
default: printf("Invalid option ");
}
}
}
return 0;
}
output:
List Operations
===============
1.Insert Front
2.Insert Back
3.Display
4.Insert After
5.Remove Number
6.Remove at Index
7.Exit
Enter your choice : 1
Enter the number to insert : 6
List Operations
===============
1.Insert Front
2.Insert Back
3.Display
4.Insert After
5.Remove Number
6.Remove at Index
7.Exit
Enter your choice : 1
Enter the number to insert : 7
List Operations
===============
1.Insert Front
2.Insert Back
3.Display
4.Insert After
5.Remove Number
6.Remove at Index
7.Exit
Enter your choice : 2
Enter the number to insert : 5
List Operations
===============
1.Insert Front
2.Insert Back
3.Display
4.Insert After
5.Remove Number
6.Remove at Index
7.Exit
Enter your choice : 2
Enter the number to insert : 9
List Operations
===============
1.Insert Front
2.Insert Back
3.Display
4.Insert After
5.Remove Number
6.Remove at Index
7.Exit
Enter your choice : 2
Enter the number to insert : 4
List Operations
===============
1.Insert Front
2.Insert Back
3.Display
4.Insert After
5.Remove Number
6.Remove at Index
7.Exit
Enter your choice : 4
Enter the number to insert : 6
Enter the index to insert after : 2
List Operations
===============
1.Insert Front
2.Insert Back
3.Display
4.Insert After
5.Remove Number
6.Remove at Index
7.Exit
Enter your choice : 3
Element(s) in the list are : 7 6 5 6 9 4
List Operations
===============
1.Insert Front
2.Insert Back
3.Display
4.Insert After
5.Remove Number
6.Remove at Index
7.Exit
Enter your choice : 4
Enter the number to insert : 1
Enter the index to insert after : 0
List Operations
===============
1.Insert Front
2.Insert Back
3.Display
4.Insert After
5.Remove Number
6.Remove at Index
7.Exit
Enter your choice : 3
Element(s) in the list are : 7 1 6 5 6 9 4
List Operations
===============
1.Insert Front
2.Insert Back
3.Display
4.Insert After
5.Remove Number
6.Remove at Index
7.Exit
Enter your choice : 5
Enter the number to remove : 6
6
List Operations
===============
1.Insert Front
2.Insert Back
3.Display
4.Insert After
5.Remove Number
6.Remove at Index
7.Exit
Enter your choice : 3
Element(s) in the list are : 7 1 5 6 9 4
List Operations
===============
1.Insert Front
2.Insert Back
3.Display
4.Insert After
5.Remove Number
6.Remove at Index
7.Exit
Enter your choice : 5
Enter the number to remove : 5
5
List Operations
===============
1.Insert Front
2.Insert Back
3.Display
4.Insert After
5.Remove Number
6.Remove at Index
7.Exit
Enter your choice : 3
Element(s) in the list are : 7 1 6 9 4
List Operations
===============
1.Insert Front
2.Insert Back
3.Display
4.Insert After
5.Remove Number
6.Remove at Index
7.Exit
Enter your choice : 6
Enter the index to remove : 1
1
List Operations
===============
1.Insert Front
2.Insert Back
3.Display
4.Insert After
5.Remove Number
6.Remove at Index
7.Exit
Enter your choice : 3
Element(s) in the list are : 7 6 9 4
List Operations
===============
1.Insert Front
2.Insert Back
3.Display
4.Insert After
5.Remove Number
6.Remove at Index
7.Exit
Enter your choice : 6
Enter the index to remove : 2
9
List Operations
===============
1.Insert Front
2.Insert Back
3.Display
4.Insert After
5.Remove Number
6.Remove at Index
7.Exit
Enter your choice : 3
Element(s) in the list are : 7 6 4
List Operations
===============
1.Insert Front
2.Insert Back
3.Display
4.Insert After
5.Remove Number
6.Remove at Index
7.Exit
Enter your choice : 7
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.