Question

how to delete a node from a linked list, at beginning middle or end. show the...

how to delete a node from a linked list, at beginning middle or end. show the functions for each and explain each step. in C

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* Linked List Maintained for deletion cases as per your choice */
#include<stdio.h>
#include<conio.h>
struct list
{
int data;//Input is of type Integer
struct list *next;//pointer of type struct list
};
main()
{
struct list *head,*ptr,*ptr1,*temp,*temp1,*temp2,*temp3,*pos;
char ch,i;
clrscr();
printf("How many nodes you want in your Linked List : ");
scanf("%d",&ch);
printf("Enter 1 Value in Linked List :\n");
ptr=(struct list*)malloc(sizeof(struct list*));
scanf("%d",&ptr->data);
ptr->next=NULL;
head=ptr;
temp3=ptr;
temp=ptr;
temp1=ptr;
temp2=ptr;
for(i=1;i<=ch-1;i++)
{
ptr=createlinked_list(ptr,i);
}
printf("The data in the nodes are as follows : \n ");
while(temp!=NULL)
{
printf("\t %d",temp->data);
temp=temp->next;
}
printf("\n Performing Deletion at beginning : \n ");
head=deletion_atbeg(head,temp1);
printf("The result after deletion at beginning : \n");
while(head!=NULL)
{
printf("\t %d",head->data);
head=head->next;
}
printf("\n Enter position which you want to delete : \n ");
scanf("%d",&pos);
deletion_atmid(temp3);
//printf("%d",temp3->next->data);
head=deletion_atend(temp2,ch);
printf("\n After Deletion at end : \n ");
while(head!=NULL)
{
printf("\t %d",head->data);
head=head->next;
}
getch();
}
deletion_atmid(struct list *start,int k)
{
struct list *temp5;
temp5=start;
start->next=start->next->next;
//printf("%d",start->next->data);
printf("\n The Data after your choice deletion is as follows : ");
while(temp5!=NULL)
{
printf("\n \t%d",temp5->data);
temp5=temp5->next;
}
}
deletion_atbeg(struct list *temp_head,struct list *fnode)
{
temp_head=fnode->next;
return temp_head;
}
deletion_atend(struct list *lnode,int k)
{
struct list *temp=lnode;
int j;
while(lnode->next->next!=NULL)
{
lnode=lnode->next;
}
lnode->next=NULL;
return temp;
}
createlinked_list(struct list *temp,int k)
{
struct list *temp1;
temp1=(struct list*)malloc(sizeof(struct list*));
printf("Enter %d value in Linked List : \n ",k+1);
scanf("%d",&temp1->data);
temp->next=temp1;
temp1->next=NULL;
return temp1;
}

Add a comment
Know the answer?
Add Answer to:
how to delete a node from a linked list, at beginning middle or end. show the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ How would you delete a node in a linked list? from beginning, end, and a...

    C++ How would you delete a node in a linked list? from beginning, end, and a node somewhere in the middle without breaking the links to the next or previous nodes

  • Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list -...

    Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list - Reference: chapter 18: Create an array of 15 student records that should not be sorted Create a liked list of 15 student record nodes. Each node is a node of one student record from the above unsorted array. The list of student records should be sorted by student ID. (Insert function without sort function to create a linked list.) (If you insert correctly, the...

  • 1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is...

    1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is given below. struct node { int info; struct node *next; }; typedef struct node node; You can assume that all the nodes in the linked list are distinct and each node appears in the list at most once. Prototype of the functions are given below. node *delete(node *head, int k) node *recursivedelete(node *head, int k) • delete deletes the node with info k from...

  • implement delete node function in c++ language I just need a basic doubly linked list code for the delete node portion of the code // Delete node containing word from list if it is present void...

    implement delete node function in c++ language I just need a basic doubly linked list code for the delete node portion of the code // Delete node containing word from list if it is present void delNode (DLList list, char *str) ( // Delete node containing word from list if it is present void delNode (DLList list, char *str) (

  • In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class...

    In C++ - Learn how to implement linked lists Part 1 Node and Linked List Class (50 pts): Create node with public properties: Block type block and block ptr next. Create a linked list class that uses the node you generated without an add or delete method with a head and optional tail and counter. Make a driver that generates a node to test your implementation. Part 2 Add Method (30 pts): Create an add method in your linked list...

  • Exercise-2: 15 points Develop a node class and a doubly list class. The node class should have tw...

    Exercise-2: 15 points Develop a node class and a doubly list class. The node class should have two state variables namely data and nextNode. The doubly list class should contain the following methods: Middlelnsert- insert a node somewhere in the middle of the list Startinsert-insert a node at start of the Linked list Endinsert- insert a node at the end of the Linked list Delete-delete a node Traverse-prints all the node's data Reverse-reverses the linked list . . Note: Choose...

  • Exercise-1:15 points Develop a node class and a singly list class. The node class should have two...

    Exercise-1:15 points Develop a node class and a singly list class. The node class should have two state variables namely data and nextNode. The singly list class should contain the following methods . Middlelnsert-insert a node somewhere in the middle of the list . Startinsert-insert a node at start of the Linked list Endinsert-insert a node at the end of the Linked list . Delete-delete a node Traverse-prints all the node's data Reverse -reverses the linked list Note: Choose appropriate...

  • implement a doubly-linked list in C. Each node in the linked list should contain a string,...

    implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...

  • This is a recursive function to delete all even nodes from a linked list. node *deleteEven(node...

    This is a recursive function to delete all even nodes from a linked list. node *deleteEven(node *head) if (head == NULL)    return head;    if (head->data % 2 == 0) { node *temp = head; free(temp); return deleteEven(head->next); } else { head->next = deleteEven(head->next); return head; } } What is the purpose of heaving to set "head->next = deleteEven(head->next);" instead of just having "head = head->next;" in one of the lines above and just calling "deleteEven(head->next);"?

  • This is a code for linked list, it is about adding a node in the middle...

    This is a code for linked list, it is about adding a node in the middle of a list, I am really confused why int i = 2? can't it be 0? Add to the Middle • Allocate memory and store data for new node Traverse to node just before the required position of new node Change next pointers to include new node in between struct node *newNode; newNode = malloc(sizeof(struct node)); newNode->data = 4; struct node *temp head; for(int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT