Question

Write a program in C that the add a node at the end of a singly...

Write a program in C that the add a node at the end of a singly linked list       [6]
b)   Explain what is referred to as complexity of an algorithm               [2]

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

#include<stdio.h>
#include<stdlib.h>
struct node
{
   int data;
   struct node *next;  
};

//method to add node at the end of the singly linked list
void addAtEnd(struct node *head,int data)
{
   struct node *temp=head;
   if(head==NULL)//if list is empty
   {
       head = (struct node*)malloc(sizeof(struct node));
       head->data=data;
       head->next=NULL;
   }else
   {
       //traversing upto end to insert
       while(temp->next!=NULL)
       {
       temp=temp->next;  
       }  
       temp->next = (struct node*)malloc(sizeof(struct node));
       temp->data=data;
       temp->next=NULL;
   }
}
//b
//complexity is O(n)//n is number of nodes in list
int main()
{
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a program in C that the add a node at the end of a singly...
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
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