#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
int main()
{
struct node *head = NULL;
append(&head,8);
append(&head,7);
push(&head,3);
push(&head,4);
append(&head,5);
insertAfter(head->next->next,1);
printf("Linked List is:\n");
print(head);
return 0;
}
void push(struct node** ref, int newData)
{
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = newData;
newNode->next = (*ref);
(*ref) = newNode;
}
void insertAfter(struct node* prev,int newData)
{
if(prev == NULL)
{
printf("the given previous node can't be null");
return 0;
}
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = newData;
newNode->next = prev->next;
prev->next = newNode;
}
void append(struct node** ref, int newData)
{
struct node *newNode = (struct node*)malloc(sizeof(struct node));
struct node *last = *ref;
newNode->data = newData;
newNode->next = NULL;
if(*ref == NULL)
{
(*ref)=newNode;
return;
}
while(last->next != NULL)
{
last=last->next;
}
last->next = newNode;
return 0;
}
void print(struct node *n)
{
while(n!= NULL)
{
printf("%d",n->data);
n=n->next;
}
}
Comments
Post a Comment