Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.5 kB
0
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
struct node{
    int data;
    struct node *next;
};

void printlist(struct node *dummyHead)
{
    struct node *temp=dummyHead;
    while(temp != NULL){
    printf("%d " ,temp->data);
    temp= temp->next;
}
}

void countNode(struct node *temphead)
{
struct node *temp=temphead;
int count=0;

while(temp != NULL){
count=count+1;
temp= temp->next;
}
printf("Total nodes : %d " , count);
printf("/n");
}

struct node * addAtFirst(struct node * dummpyHead){
    struct node * newNode = malloc(sizeof(struct node));
    newNode->data=60;
    newNode->next=NULL;
    newNode->next=dummpyHead;
    dummpyHead=newNode;
    return dummpyHead;
}
int main()
{
struct node *head = malloc(sizeof(struct node));
head->data=10;
head->next= NULL;

struct node *A = malloc(sizeof(struct node));
A->data=20;
A->next= NULL;
head->next= A;

struct node *B = malloc(sizeof(struct node));
B->data=30;
B->next= NULL;
A->next= B;

struct node *C = malloc(sizeof(struct node));
C->data=40;
C->next= NULL;
B->next=C;

struct node *D = malloc(sizeof(struct node));
D->data=50;
D->next= NULL;
C->next= D;

//printing the linklist
printf("%d ",head->data);
printf("%d ",A->data);
printf("%d ",B->data);
printf("%d ",C->data);
printf("%d ",D->data);

printlist(head);
countNode(head);
head = saddAtFirst(head,5);
printlist(head);
head = saddAtFirst(head,-10);
printlist(head);
    return 0;
}