Data Structure

Single Linked List Program



#include<stdio.h>

#include<stdlib.h>

#include<conio.h>
void InsertAtBeginning(int value);
void InsertAtEnd(int value);
void InsertAtPosition(int value,int position);
void RemoveAtBeginning();
void RemoveAtEnd();
void RemoveAtPosition(int position);
void display();
int CheckEmpty();
struct Node
{
 int data;
 struct Node *next;

}
* head=NULL;
int main()
{
 int value,choice;
 char c;
 do
{
 printf(“Entern1-Insertn2-Removen3-Displayn”);
 scanf(“%d”,&choice);
 switch(choice)
{
 case 1:
{
 int x;
 printf(“Entern1-Insert at Beginningn2-Insert at Endn3-Insert at Positionn”);
 scanf(“%d”,&x);
 printf(“Enter Value to be Insertedn”);
 scanf(“%d”,&value);
 switch(x)
{
 case 1:
{
 InsertAtBeginning(value);
 break;

}
 case 2:
{
 InsertAtEnd(value);
 break;

}
 case 3:
{
 int position;
 printf(“Enter position to insert a value(counted from 0)n”);
 scanf(“%d”,&position);
 InsertAtPosition(value,position);
 break;

}
 default :
{
 printf(“Enter Valid Choicen”);
 break;

}

}
 break;

}
 case 2:
{
 int x;
 printf(“Entern1-Delete at Beginningn2-Delete At Endn3-Delete at Positionn”);
 scanf(“%d”,&x);
 switch(x)
{
 case 1:
{
 RemoveAtBeginning();
 break;

}
 case 2:
{
 RemoveAtEnd();
 break;

}
 case 3:
{
 int position;
 printf(“Enter position to be removed starting from 1n”);
 scanf(“%d”,&position);
 RemoveAtPosition(position);
 break;

}
 default :
{
 printf(“Enter Valid Choicen”);
 break;

}

}
 break;

}
 case 3:
{
 display();
 break;

}
 default:
{
 printf(“Enter Valid Choicen”);
 break;

}

}
 printf(“Enter ‘Y’ to continue else any lettern”);
 fflush(stdin);
 c=getche();
 printf(“n”);

}
while(c==’Y’ || c==’y’);
 return(0);

}
void InsertAtBeginning(int value)
{
 struct Node *newNumber;
 newNumber = (struct Node*)malloc(sizeof(struct Node));
 newNumber->
data = value;
 if(head == NULL)
{
 newNumber->
next = NULL;
 head = newNumber;

}
 else
{
 newNumber->
next = head;
 head = newNumber;

}
 printf(“Given Number %d is inserted at beginning Successfullyn”,value);

}
void InsertAtEnd(int value)
{
 struct Node *newNumber;
 newNumber = (struct Node*)malloc(sizeof(struct Node));
 newNumber->
data = value;
 newNumber->
next=NULL;
 if(head==NULL)
{
 head=newNumber;

}
 else
{
 struct Node *temp;
 temp=head;
 while(temp->next!=NULL)
{
 temp=temp->
next;

}
 temp->
next=newNumber;

}

}
void InsertAtPosition(int value,int position)
{
 struct Node *newNumber,*temp;
 int count,flag;
 newNumber = (struct Node*)malloc(sizeof(struct Node));
 newNumber->
data = value;
 temp=head;
 flag=CheckEmpty();
 if(flag==1)
{
 int flag1=0;
 count=0;
 while(temp!=NULL)
{
 if(count==position-1)
{
 flag1=1;
 newNumber->
next=temp->
next;
 temp->
next=newNumber;

}
 else
{
 temp=temp->
next;

}
 count++;

}
 if(flag1==0)
{
 printf(“Entered Position Not availablen”);

}
 else
{
 printf(“Given number %d is inserted at position %d successfullyn”,value,position);

}

}
 else
{
 printf(“List is Emptyn”);

}

}
void RemoveAtBeginning()
{
 int flag=CheckEmpty();
 if(flag==1)
{
 struct Node *temp;
 temp=head;
 if(temp->next==NULL)
{
 head=NULL;
 free(temp);

}
 else
{
 head=temp->
next;
 free(temp);

}
 printf(“Deleted Successfullyn”);

}
 else
{
 printf(“List is Emptyn”);

}

}
void RemoveAtEnd()
{
 int flag=CheckEmpty();
 if(flag==1)
{
 if(head->next==NULL)
{
 head=NULL;

}
 else
{
 struct Node *temp=head,*temp1;
 while(temp->next!=NULL)
{
 temp1=temp;
 temp=temp->
next;

}
 temp1->
next=NULL;
 free(temp);

}

}
 else
{
 printf(“List Empty.Try again!n”);

}

}
void RemoveAtPosition(int position)
{
 int flag=CheckEmpty();
 if(flag==1)
{
 int count=0,flag1=0,i;
 struct Node *temp=head;
 if(position==1)
{
 head=temp->
next;
 free(temp);
 return;

}
 for(i=0;i<position-2;i++)
{
 temp=temp->
next;

}
 struct Node *temp1=temp->
next;
 temp->
next=temp1->
next;
 free(temp1);

}
 else
{
 printf(“List is emptyn”);

}

}
void display()
{
 int flag=CheckEmpty();
 if(flag==1)
{
 struct Node *temp;
 temp=head;
 while(temp->next!=NULL)
{
 printf(“%d->”,temp->data);
 temp=temp->
next;

}
 printf(“%d”,temp->data);
 printf(“n”);

}
 else
{
 printf(“No List Availablen”);

}

}
int CheckEmpty()
{
 if(head==NULL) return 0;
 else return 1;

}

Stacks Using Arrays

#include<stdio.h>
#include<stdlib.h>
#define size 10void Push();
void Pop();
void Display();
int CheckStackFull();
int CheckStackEmpty();
struct StackArray
{
 int array[size],top;
}
ptr;
int main()
{
 int choice,contin;
 ptr.top=-1;
 do
{
 printf(“Entern1 – Pushn2 – Popn3 – Displayn4 – Exitn”);
 scanf(“%d”,&choice);
 switch(choice) 
{
 case 1: Push();
 break;
 case 2: Pop();
 break;
 case 3: Display();
break;
 case 4: exit(0);
 break;
 default: printf(“Enter Valid Choicen”);
break;
}
 printf(“Enter 1 to continue otherwise any other numbern”);
 scanf(“%d”,&contin);
}
while(contin==1);
 return(0);
}
void Push()
{
 int flag=CheckStackFull();
 if(flag==1) 
{
 int value;
 printf(“Enter value to push into stackn”);
 scanf(“%d”,&value);
 ptr.top+=1;
 ptr.array[ptr.top]=value;
}
 else 
{
 printf(“Stack is Fulln”);
}
}
void Pop()
{
 int flag=CheckStackEmpty();
 if(flag==1) 
{
 printf(“Popped Element is %dn”,ptr.array[ptr.top]);
 ptr.top-=1;
}
 else 
{
 printf(“Stack is Emptyn”);
}
}
void Display()
{
 int i,flag=CheckStackEmpty();
 if(flag==1) 
{
 printf(“Elements present in stackn”);
 for(i=ptr.top;i>-1;i–) printf(“%dn”,ptr.array[i]);
 printf(“n”);
}
 else 
{
 printf(“Stack is Emptyn”);
}
}
int CheckStackFull()
{
 if(ptr.top==size-1) return 0;
 else return 1;
}
int CheckStackEmpty()
{
 if(ptr.top==-1) 
            return 0;
 else 
return 1;
}

Leave a Comment

error: Content is protected !!