Untitled
unknown
plain_text
2 years ago
1.9 kB
11
Indexable
#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 * Dummyhead) { struct node * temp = Dummyhead; int count=0; while(temp!=NULL) { count=count +1; temp=temp->next; } printf("Total node : %d ",count); printf(" \n "); } struct node * addAtFirst(struct node * Dummyhead,int value) { struct node * newNode = malloc (sizeof(struct node)); newNode->data=value; newNode->next=NULL; newNode->next=Dummyhead; Dummyhead=newNode; return Dummyhead; } 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 LINK LIST METHOD TASK 2 //printf("%d>",head->data); //printf("%d>",A->data); //printf("%d>",B->data); // printf("%d>",C->data); // printf("%d>",D->data); //struct node * temp =head; //while(temp!=NULL){ //printf("%d ",temp->data); // temp=temp->next; printList(head); //printList(head); //printList(head); countNode(head); head=addAtFirst(head,5); printList(head); head=addAtFirst(head,2); printList(head); return 0; }
Editor is loading...