CP Assignment unit IV

Linked List:

in the assignment 4 differnt programs were asked like to create a linked list, to add an element in the linked list, to insert an element in the linked list, to delete an element, to traverse linked list.

instead of making different programs we have made one single program:

CODE:

#include<stdio.h>
#include<malloc.h>
int node_count=0;
struct node
{
 int data;
 struct node *next;
};

void display(struct node *sp)
 {
  int i;
  struct node *temp;
  temp=sp;
  for(i=1;i<=node_count;i++)
  {
   printf("\nnode %d\tcontains\t",i);
   printf("data %d\t\t",temp->data);
   printf("address of next node %u",temp->next);
   temp=temp->next;
  }
 }
struct node *add_at_start(struct node *temp)
 {
  struct node *new_node;
  new_node=(struct node*)malloc(sizeof(struct node));
  printf("Enter the data element");
  scanf("%d",&new_node->data);
  new_node->next=temp;
  node_count++;
  return new_node;
 }
 struct node *add_at_end(struct node *last)
 {
  struct node *new_node;
  new_node=(struct node*)malloc(sizeof(struct node));
  printf("Enter the data element");
  scanf("%d",&new_node->data);
  last->next=new_node;
  new_node->next=NULL;
  node_count++;
  return new_node;
 }
 void insert_node(int n1,int n2,struct node *sp)
 {
 int i;
 struct node *new_node,*node_n1=sp;
    if(n1<node_count&&n2<=node_count&&n1==n2-1)
     {
      for(i=1;i<=n1-1;i++)
      {
	node_n1=node_n1->next;
      }
      new_node=(struct node*)malloc(sizeof(struct node));
      printf("Enter the data element");
      scanf("%d",&new_node->data);
      new_node->next=node_n1->next;
      node_n1->next=new_node;
      node_count++;

     }
     else
      printf("\nInvalid Input\n");
 }
 void delete_node(int n1,struct node *sp)
 {
 int i;
 struct node *node_n1,*prev_node=sp;
  if(n1<=node_count)
   {
    for(i=1;i<=n1-2;i++)
    {
     prev_node=prev_node->next;
    }
    node_n1=prev_node->next;
    prev_node->next=node_n1->next;
    node_count--;
   }
   else
   printf("\n Invalid node\n");
 }
void main()
{
 int ch,n1,n2;
 struct node *sp,*last;
 clrscr();
 printf("\n***************************LINKED LIST IMPLEMENTATION*************************\n");
 //create first node
 last=(struct node*)malloc(sizeof(struct node));
 printf("\nEnter data element of first node ");
 scanf("%d",&last->data);
 sp=last;
 last->next=NULL;
 node_count++;
  while(1)
   {printf("\n\nNo. of nodes in linked list at present is %d\n",node_count);
    printf("Enter any choice\n");
    printf("1.Add a node\n");
    printf("2.Insert a node\n");
    printf("3.Delete a node\n");
    printf("4.Display the linked list\n");
    printf("5.Exit\n");
    scanf("%d",&ch);
     switch(ch)
     {
      case 1: printf("Where do you want to add a node?(Enter the appropriate choice)\n");
	      printf("1.Add at the start(as 1st node)\n");
	      printf("2.Add at the end\n");
	      scanf("%d",&ch);
	       if(ch==1)
		sp=add_at_start(sp);
	       else
		last=add_at_end(last);
	       break;
      case 2:printf("\nBetween which two nodes would you like to enter a node?(enter the node nos.) ");
	     scanf("%d%d",&n1,&n2);
	     insert_node(n1,n2,sp);
	     break;
      case 3: printf("\nWhich node do you want to delete?(enter the node no.) ");
	      scanf("%d",&n1);
	      delete_node(n1,sp);
	      break;
      case 4:display(sp);
	      break;
      case 5:exit();
      default:printf("\nInvalid Input\n");
     }
   }
 getch();
}

Leave a comment