Singly Linked List
Singly Linked List
• Break;
• } }}
• void insertatfirst()
• {
• struct node *ptr;
• int value;
• ptr = (struct node *) malloc(sizeof(struct node *));
• if(ptr == NULL) {
• printf(“\n Insert not allowed"); }
• else {
• printf("\nEnter value\n");
• scanf("%d",&value);
• ptr->data = value;
• ptr->next = head;
• head = ptr;
• printf("\nNode inserted");
• } }
• void insertatlast() { • ptr -> next = NULL;
• struct node *ptr,*temp; • head = ptr;
• int value;
• printf("\nNode inserted"); }
• ptr = (struct node*)malloc(sizeof(struct node));
• else {
• if(ptr == NULL) {
• printf(“\nInsert not allowed");
• temp = head;
• } • ptr1 = ptr;
• else if(head -> next == NULL) { • ptr = ptr ->next;
• head = NULL; • }
• free(head);
• ptr1->next = NULL;
• printf("\nOnly node of the list deleted ...\n");
• free(ptr);
• }
• • printf("\nDeleted Node from the last ...\n");
• } }
• void deletesp()
• {
• struct node *ptr,*ptr1;
• int loc,i;
• printf("\n Enter the location of the node after which you want to perform deletion \n");
• scanf("%d",&loc);
• ptr=head;
• for(i=0;i<loc;i++) {
• ptr1 = ptr;
• ptr = ptr->next;
• if(ptr == NULL)
• {
• printf("\nCan't delete");
• return;
• } }
• ptr1 ->next = ptr ->next;
• free(ptr);
• printf("\nDeleted node %d ",loc+1);
• }
• void search()
• {
• struct node *ptr;
• int item,i=0,flag;
• ptr = head;
• if(ptr == NULL) {
• printf("\nEmpty List\n");
• }
• else {
• printf("\nEnter item which you want to search?\n");
• scanf("%d",&item);
• while (ptr!=NULL)
• { if(ptr->data == item)
• {
• printf("item found at location %d ",i+1);
• flag=0; }
• else {
• flag=1; }
• i++;
• ptr = ptr -> next;
• }
• if(flag==1) {
• printf("Item not found\n");
• } } }
• void display()
• {
• struct node *ptr;
• ptr = head;
• if(ptr == NULL)
• {
• printf("Nothing to print");
• }
• else
• {
• printf("\nprinting values . . . . .\n");
• while (ptr!=NULL)
• {
• printf("\n%d",ptr->data);
• ptr = ptr -> next;
• } }}