1.
struct node
{
int data;
struct node *next;
};
struct node *head = NULL;
void addFirst(struct node **head, int val)
{
struct node *newNode = malloc(sizeof(struct node));
newNode->data = val;
}
void addFirst(struct node **head, int val)
{
struct node *newNode = malloc(sizeof(struct node));
newNode->data = val;
newNode->next = *head;
}
2.
Algorithm to delete first node of Singly Linked List
Input: head of the linked list
Begin:
If (head != NULL) then
Delete ← head
head ← head.next
unalloc (toDelete)
End if
End
3.
struct node* temp = head;
int count=0;
while(temp != NULL){
temp = temp->next;
count++;
}
printf("\n Total no. of nodes is %d",count);
}
4.
struct node
{
int data;
struct node *next;
};
struct node *head = NULL;
void addLast(struct node **head, int val)
{
struct node *newNode = malloc(sizeof(struct node));
newNode->data = val;
newNode->next = NULL;
}
void addLast(struct node **head, int val)
{
struct node *newNode = malloc(sizeof(struct node));
newNode->data = val;
newNode->next = NULL;
if(*head == NULL)
*head = newNode;
}
5.
typedef struct node{
int value;
struct node *next;
}node;
void printList(node *head){
node *tmp = head;
while(tmp != NULL){
if(tmp->next == NULL){
printf("%d", tmp->value);
}else{
printf("%d, ", tmp->value);
}
tmp = tmp->next;
}
}
Comments
Leave a comment