Data Structure - Linked List

  • 1.	Singly Linked-List
    
    					    						

    	#include
    	#include
    	#include
    	struct link
    	{
    		int n;
    		struct link *p;
    	};
    	struct link *start = NULL;
    
    	void pushbeg();
    	void pushend();
    	void pushspl();
    	void popbeg();
    	void popend();
    	void popspl();
    	void disp();
    	void search();
    	void count();
    
    	void main()
    	{
    		int n;
    		clrscr();
    		do
    		{
    			printf("\n\n1.pushbegining\n");
    			printf("2.pushending\n");
    			printf("3.pushspecial\n");
    			printf("4.popbegining\n");
    			printf("5.popending\n");
    			printf("6.popspecial\n");
    			printf("7.Display\n");
    			printf("8.search\n");
    			printf("9.Count\n");
    			printf("10.Exit\n\n");
    
    			printf("Enter the choise\t");
    			scanf("%d",&n);
    			switch(n)
    			{
    				case 1: pushbeg();
    						break;
    				case 2: pushend();
    						break;
    				case 3: pushspl();
    						break;
    				case 4: popbeg();
    						break;
    				case 5: popend();
    						break;
    				case 6: popspl();
    						break;
    				case 7: disp();
    						break;
    				case 8: search();
    						break;
    				case 9: count();
    						break;
    				case 10: printf("\nGood bye\n");
    						break;
    				default : printf("Check ur choise");
    						break;
    			}
    		}while(n!=10);
    		getch();
    	}
    	void pushbeg()
    	{
    		struct link *node;
    		int x;
    
    		node=(struct link *)malloc(sizeof(struct link));
    
    		printf("enter the number u want to insert \t");
    		scanf("%d",&x);
    
    		node->n=x;
    		node->p=NULL;
    
    		if(start == NULL)
    		{
    			start = node;
    		}
    		else
    		{
    			node ->p = start;
    			start = node;
    		}
    	}
    	void pushend()
    	{
    		struct link *node,*t;
    		int x;
    
    		node=(struct link *)malloc(sizeof(struct link));
    
    		printf("enter the number u want to insert \t");
    		scanf("%d",&x);
    
    		node->n=x;
    		node->p=NULL;
    
    		if(start == NULL)
    		{
    			start = node;
    		}
    		else
    		{
    			t = start;
    			while(t -> p != NULL)
    			{
    				t = t -> p;
    			}
    			t -> p = node;
    		}
    	}
    	void pushspl()
    	{
    		struct link *node,*t;
    		int x,pos,i=1;
    
    		node=(struct link *)malloc(sizeof(struct link));
    
    		disp();
    
    		printf("\n\nEnter the number u want to insert \t");
    		scanf("%d",&x);
    
    		node->n=x;
    		node->p=NULL;
    
    		if(start == NULL)
    		{
    			start = node;
    		}
    		else
    		{
    			printf("enter the position\t");
    			scanf("%d",&pos);
    			if(pos==1)
    			{
    				pushbeg();
    			}
    			else
    			{
    
    			t = start;
    			while(i+1!=pos)
    			{
    				t = t -> p;
    				if(t == NULL)
    				{
    					printf("The given position is not found\n");
    					return;
    				}
    				i++;
    			}
    			node -> p = t -> p;
    			t -> p = node;
    			}
    		}
    	}
    	void popbeg()
    	{
    		struct link *t;
    		if(start == NULL)
    		{
    			printf("Single link list is Empty \t");
    		}
    		else
    		{
    			disp();
    
    			t = start;
    			printf("\n\n%d is deleted ",start -> n);
    			start = start -> p;
    			free(t);
    		}
    	}
    	void popend()
    	{
    		struct link *t;
    		if(start == NULL)
    		{
    			printf("Single link list is Empty \t");
    		}
    		else
    		{
    			disp();
    
    			t = start;
    			while(t -> p -> p != NULL)
    			{
    				t = t -> p;
    			}
    			printf("\n\n%d is deleted ",t -> p -> n);
    			free(t -> p);
    			t -> p = NULL;
    		}
    	}
    	void popspl()
    	{
    		struct link *t,*q;
    		int pos,i=1;
    
    		if(start == NULL)
    		{
    			printf("Single link list is Empty \t");
    		}
    		else
    		{
    			printf("enter the position of the elment u want to delete\t");
    			scanf("%d",&pos);
    
    			t = start;
    
    			if(pos == 1)
    			{
    				popbeg();
    			}
    			else
    			{
    				disp();
    
    				while(i+1 != pos)
    				{
    					t = t -> p;
    					if(t == NULL)
    					{
    						printf("The given position is not found\n");
    						return;
    					}
    					i++;
    				}
    				q = t -> p;
    				printf("%d is deleted ",q -> n);
    				t -> p = q -> p;
    				free(q);
    			}
    		}
    	}
    	void disp()
    	{
    		struct link *t;
    		if(start == NULL)
    		{
    			printf("Single link list is empty\t");
    		}
    		else
    		{
    			t = start;
    			while(t != NULL)
    			{
    				printf("%d\t",t -> n);
    				t = t -> p;
    			}
    		}
    	}
    	void search()
    	{
    		struct link *z;
    		int x,f=0;
    		if(start==NULL)
    		{
    			printf("Empty");
    		}
    		else
    		{
    			z=start;
    			printf("Enter the number u want to search\t");
    			scanf("%d",&x);
    			while(z!=NULL)
    			{
    				if(z->n==x)
    				{
    					printf("complete");
    					f=1;
    					break;
    				}
    				z=z->p;
    			}
    			if(f==0)
    			{
    				printf("not complete");
    			}
    		}
    	}
    	void count()
    	{
    		struct link *z;
    		int c=0;
    		if(start==NULL)
    		{
    			printf("Empty");
    		}
    		else
    		{
    			z=start;
    			while(z!=NULL)
    			{
    				c++;
    				z=z->p;
    			}
    			printf("no of elements %d",c);
    		}
    	}
    						

  • 2.	Doubly Linked-List
    
    					    						

    	#include
    	#include
    	#include
    	struct dlink
    	{
    		int x;
    		struct dlink *lp;
    		struct dlink *rp;
    	};
    	struct dlink *start = NULL;
    
    	void pushbeg();
    	void pushend();
    	void pushspl();
    	void popbeg();
    	void popend();
    	void popspl();
    	void disp();
    	void disprev();
    
    	void main()
    	{
    		int n;
    		clrscr();
    		do
    		{
    			clrscr();
    			printf("\n\n1.pushbegining\n");
    			printf("2.pushending\n");
    			printf("3.pushspecial\n");
    			printf("4.popbegining\n");
    			printf("5.popending\n");
    			printf("6.popspecial\n");
    			printf("7.Display\n");
    			printf("8.Display\n");
    			printf("9.Exit\n\n");
    
    			printf("etner the choise\t");
    			scanf("%d",&n);
    			switch(n)
    			{
    				case 1: pushbeg();
    					break;
    				case 2: pushend();
    					break;
    				case 3: pushspl();
    					break;
    				case 4: popbeg();
    					break;
    				case 5: popend();
    					break;
    				case 6: popspl();
    					break;
    				case 7: disp();
    					break;
    				case 8: disprev();
    					break;
    				case 9: printf("\nGood bye\n");
    					break;
    			}
    			getch();
    		}while(n!=8);
    		getch();
    	}
    	void pushbeg()
    	{
    		struct dlink *node;
    		int a;
    
    		node=(struct dlink *)malloc(sizeof(struct dlink));
    
    		printf("enter the number u want to insert \t");
    		scanf("%d",&a);
    
    		node->x=a;
    		node->lp=NULL;
    		node->rp=NULL;
    
    		if(start == NULL)
    		{
    			start = node;
    		}
    		else
    		{
    			node ->rp = start;
    			start ->lp = node;
    			start = node;
    		}
    	}
    	void pushend()
    	{
    		struct dlink *node,*t;
    		int a;
    
    		node=(struct dlink *)malloc(sizeof(struct dlink));
    
    		printf("enter the number u want to insert \t");
    		scanf("%d",&a);
    
    		node->x=a;
    		node->lp=NULL;
    		node->rp=NULL;
    
    		if(start == NULL)
    		{
    			start = node;
    		}
    		else
    		{
    			t = start;
    			while(t -> rp != NULL)
    			{
    				t = t->rp;
    			}
    			t ->rp = node;
    			node ->lp = t;
    		}
    	}
    	void pushspl()
    	{
    		struct dlink *node,*t;
    		int a,pos,i=1;
    
    		node=(struct dlink *)malloc(sizeof(struct dlink));
    
    		disp();
    
    		printf("enter the number u want to insert \t");
    		scanf("%d",&a);
    
    		node->x=a;
    		node->lp=NULL;
    		node->rp=NULL;
    
    		if(start == NULL)
    		{
    			start = node;
    		}
    		else
    		{
    			printf("enter the position\t");
    			scanf("%d",&pos);
    
    			t = start;
    			while(i+1!=pos)
    			{
    				t = t->rp;
    				i++;
    			}
    			t -> rp -> lp = node;
    			node -> rp = t -> rp;
    
    			t -> rp = node;
    			node -> lp = t;
    		}
    	}
    	void popbeg()
    	{
    		if(start == NULL)
    		{
    			printf("Double link list is Empty \t");
    		}
    		else
    		{
    			printf("%d is deleted ",start -> x);
    			start = start -> rp;
    			free(start -> lp);
    			start -> lp = NULL;
    		}
    	}
    	void popend()
    	{
    		struct dlink *t;
    		if(start == NULL)
    		{
    			printf("Double link list is Empty \t");
    		}
    		else
    		{
    			t = start;
    			while(t -> rp != NULL)
    			{
    				t = t -> rp;
    			}
    			printf("%d is deleted ",t -> x);
    
    			if(t->lp==NULL)
    			{
    				free(t);
    				start=NULL;
    			}
    			else
    			{
    				t = t -> lp;
    				free(t -> rp);
    				t -> rp = NULL;
    			}
    		}
    	}
    	void popspl()
    	{
    		struct dlink *t;
    		int pos,i=1;
    		if(start == NULL)
    		{
    			printf("Double link list is Empty \t");
    		}
    		else
    		{
    			printf("enter the position of the elment u want to delete\t");
    			scanf("%d",&pos);
    
    			t = start;
    
    			if(pos == 1)
    			{
    				popbeg();
    			}
    			else
    			{
    				while(i != pos)
    				{
    					t = t -> rp;
    					i++;
    				}
    				printf("%d is deleted ",t -> x);
    				t = t -> lp;
    				t -> rp = t -> rp -> rp;
    				free(t -> rp -> lp);
    				t -> rp -> lp = t;
    			}
    		}
    	}
    	void disp()
    	{
    		struct dlink *t;
    		if(start == NULL)
    		{
    			printf("Double link list is empty\t");
    		}
    		else
    		{
    			t = start;
    			while(t != NULL)
    			{
    				printf("%d\t",t -> x);
    				t = t -> rp;
    			}
    		}
    	}
    	void disprev()
    	{
    		struct dlink *t;
    		if(start == NULL)
    		{
    			printf("Double link list is empty\t");
    		}
    		else
    		{
    			t = start;
    			while(t->rp != NULL)
    			{
    				t = t -> rp;
    			}
    			while(t!= NULL)
    			{
    				printf("%d\t",t->x);
    				t = t -> lp;
    			}		
    		}
    	}
    						

  • 3.	Singly - Cirular Linked-List
    
    					    						

    	#include
    	#include
    	#include
    	struct clink
    	{
    		int x;
    		struct clink *p;
    	};
    	struct clink *start = NULL;
    
    	void pushbeg();
    	void pushend();
    	void pushspl();
    	void popbeg();
    	void popend();
    	void popspl();
    	void disp();
    
    	void main()
    	{
    		int n;
    		clrscr();
    		do
    		{
    			clrscr();
    			printf("\n\n1.pushbegining\n");
    			printf("2.pushending\n");
    			printf("3.pushspecial\n");
    			printf("4.popbegining\n");
    			printf("5.popending\n");
    			printf("6.popspecial\n");
    			printf("7.Display\n");
    			printf("8.Exit\n\n");
    
    			printf("etner the choise\t");
    			scanf("%d",&n);
    			switch(n)
    			{
    				case 1: pushbeg();
    					break;
    				case 2: pushend();
    					break;
    				case 3: pushspl();
    					break;
    				case 4: popbeg();
    					break;
    				case 5: popend();
    					break;
    				case 6: popspl();
    					break;
    				case 7: disp();
    					break;
    				case 8: printf("\nGood bye\n");
    					break;
    			}
    			getch();
    		}while(n!=8);
    		getch();
    	}
    	void pushbeg()
    	{
    		struct clink *node,*t;
    		int a;
    
    		node=(struct clink *)malloc(sizeof(struct clink));
    
    		printf("enter the number u want to insert \t");
    		scanf("%d",&a);
    
    		node->x=a;
    		node->p=NULL;
    
    		if(start == NULL)
    		{
    			start = node;
    			start -> p = start;
    		}
    		else
    		{
    			t = start;
    			while(t -> p != start)
    			{
    				t = t -> p;
    			}
    			t -> p = node;
    			node -> p = start;
    			start = node;
    		}
    	}
    	void pushend()
    	{
    		struct clink *node,*t;
    		int a;
    
    		node=(struct clink *)malloc(sizeof(struct clink));
    
    		printf("enter the number u want to insert \t");
    		scanf("%d",&a);
    
    		node->x=a;
    		node->p=NULL;
    
    		if(start == NULL)
    		{
    			start = node;
    			start -> p = start;
    		}
    		else
    		{
    			t = start;
    			while(t -> p != start)
    			{
    				t = t -> p;
    			}
    			t -> p = node;
    			node -> p = start;
    		}
    	}
    	void pushspl()
    	{
    		struct clink *node,*t;
    		int a,pos,i=1;
    
    		node=(struct clink *)malloc(sizeof(struct clink));
    
    		disp();
    
    		printf("\n\nEnter the number u want to insert \t");
    		scanf("%d",&a);
    
    		node->x=a;
    		node->p=NULL;
    
    		if(start == NULL)
    		{
    			start = node;
    		}
    		else
    		{
    			printf("enter the position\t");
    			scanf("%d",&pos);
    
    			t = start;
    			while(i!=pos)
    			{
    				t = t -> p;
    				if(t == start)
    				{
    					printf("The given position is not found\n");
    					return;
    				}
    				i++;
    			}
    			node -> p = t -> p;
    			t -> p = node;
    		}
    	}
    	void popbeg()
    	{
    		struct clink *t,*q;
    		if(start == NULL)
    		{
    			printf("Single Circular link list is Empty \t");
    		}
    		else
    		{
    			disp();
    
    			q = start;
    			printf("\n\n%d is deleted ",start -> x);
    			free(q);
    			if(start == start -> p)
    			{
    				start = NULL;
    			}
    			else
    			{
    				t = start;
    				while(t -> p != start)
    				{
    					t = t -> p;
    				}
    				start = start -> p;
    				t -> p = start;
    			}
    		}
    	}
    	void popend()
    	{
    		struct clink *t;
    		if(start == NULL)
    		{
    			printf("Single Circular link list is Empty \t");
    		}
    		else
    		{
    			disp();
    
    			t = start;
    			while(t -> p -> p != start)
    			{
    				t = t -> p;
    			}
    			printf("\n\n%d is deleted ",t -> p -> x);
    			if(start == t -> p)
    			{
    				start = NULL;
    				return;
    			}
    			free(t -> p);
    			t -> p = start;
    		}
    	}
    	void popspl()
    	{
    		struct clink *t,*q;
    		int pos,i=1;
    
    		if(start == NULL)
    		{
    			printf("Single link list is Empty \t");
    		}
    		else
    		{
    			t = start;
    
    			if(pos == 1)
    			{
    				popbeg();
    			}
    			else
    			{
    				disp();
    
    				printf("\n\nenter the position of the elment u want to delete\t");
    				scanf("%d",&pos);
    
    				while(i+1 != pos)
    				{
    					t = t -> p;
    					if(t == start)
    					{
    						printf("The given position is not found\n");
    						return;
    					}
    					i++;
    				}
    				q = t -> p;
    				printf("%d is deleted ",q -> x);
    				t -> p = q -> p;
    				free(q);
    			}
    		}
    	}
    	void disp()
    	{
    		struct clink *t;
    		if(start == NULL)
    		{
    			printf("Single Circular link list is empty\t");
    		}
    		else
    		{
    			t = start;
    			printf("%d\t",t -> x);
    			while(t -> p != start)
    			{
    				t = t -> p;
    				printf("%d\t",t -> x);
    			}
    		}
    	}
    						

  • 4.	Doubly - Cirular Linked-List
    
    					    						

    	#include
    	#include
    	#include
    	struct dlink
    	{
    		int x;
    		struct dlink *lp;
    		struct dlink *rp;
    	};
    	struct dlink *start = NULL;
    
    	void pushbeg();
    	void pushend();
    	void pushspl();
    	void popbeg();
    	void popend();
    	void popspl();
    	void disp();
    	void disprev();
    
    	void main()
    	{
    		int n;
    		clrscr();
    		do
    		{
    			clrscr();
    			printf("\n\n1.pushbegining\n");
    			printf("2.pushending\n");
    			printf("3.pushspecial\n");
    			printf("4.popbegining\n");
    			printf("5.popending\n");
    			printf("6.popspecial\n");
    			printf("7.Display\n");
    			printf("8.Display\n");
    			printf("9.Exit\n\n");
    
    			printf("etner the choise\t");
    			scanf("%d",&n);
    			switch(n)
    			{
    				case 1: pushbeg();
    					break;
    				case 2: pushend();
    					break;
    				case 3: pushspl();
    					break;
    				case 4: popbeg();
    					break;
    				case 5: popend();
    					break;
    				case 6: popspl();
    					break;
    				case 7: disp();
    					break;
    				case 8: disprev();
    					break;
    				case 9: printf("\nGood bye\n");
    					break;
    			}
    			getch();
    		}while(n!=8);
    		getch();
    	}
    	void pushbeg()
    	{
    		struct dlink *node;
    		int a;
    
    		node=(struct dlink *)malloc(sizeof(struct dlink));
    
    		printf("enter the number u want to insert \t");
    		scanf("%d",&a);
    
    		node->x=a;
    		node->lp=NULL;
    		node->rp=NULL;
    
    		if(start == NULL)
    		{
    			start = node;
    		}
    		else
    		{
    			node ->rp = start;
    			start ->lp = node;
    			start = node;
    		}
    	}
    	void pushend()
    	{
    		struct dlink *node,*t;
    		int a;
    
    		node=(struct dlink *)malloc(sizeof(struct dlink));
    
    		printf("enter the number u want to insert \t");
    		scanf("%d",&a);
    
    		node->x=a;
    		node->lp=NULL;
    		node->rp=NULL;
    
    		if(start == NULL)
    		{
    			start = node;
    		}
    		else
    		{
    			t = start;
    			while(t -> rp != NULL)
    			{
    				t = t->rp;
    			}
    			t ->rp = node;
    			node ->lp = t;
    		}
    	}
    	void pushspl()
    	{
    		struct dlink *node,*t;
    		int a,pos,i=1;
    
    		node=(struct dlink *)malloc(sizeof(struct dlink));
    
    		disp();
    
    		printf("enter the number u want to insert \t");
    		scanf("%d",&a);
    
    		node->x=a;
    		node->lp=NULL;
    		node->rp=NULL;
    
    		if(start == NULL)
    		{
    			start = node;
    		}
    		else
    		{
    			printf("enter the position\t");
    			scanf("%d",&pos);
    
    			t = start;
    			while(i+1!=pos)
    			{
    				t = t->rp;
    				i++;
    			}
    			t -> rp -> lp = node;
    			node -> rp = t -> rp;
    
    			t -> rp = node;
    			node -> lp = t;
    		}
    	}
    	void popbeg()
    	{
    		if(start == NULL)
    		{
    			printf("Double link list is Empty \t");
    		}
    		else
    		{
    			printf("%d is deleted ",start -> x);
    			start = start -> rp;
    			free(start -> lp);
    			start -> lp = NULL;
    		}
    	}
    	void popend()
    	{
    		struct dlink *t;
    		if(start == NULL)
    		{
    			printf("Double link list is Empty \t");
    		}
    		else
    		{
    			t = start;
    			while(t -> rp != NULL)
    			{
    				t = t -> rp;
    			}
    			printf("%d is deleted ",t -> x);
    
    			if(t->lp==NULL)
    			{
    				free(t);
    				start=NULL;
    			}
    			else
    			{
    				t = t -> lp;
    				free(t -> rp);
    				t -> rp = NULL;
    			}
    		}
    	}
    	void popspl()
    	{
    		struct dlink *t;
    		int pos,i=1;
    		if(start == NULL)
    		{
    			printf("Double link list is Empty \t");
    		}
    		else
    		{
    			printf("enter the position of the elment u want to delete\t");
    			scanf("%d",&pos);
    
    			t = start;
    
    			if(pos == 1)
    			{
    				popbeg();
    			}
    			else
    			{
    				while(i != pos)
    				{
    					t = t -> rp;
    					i++;
    				}
    				printf("%d is deleted ",t -> x);
    				t = t -> lp;
    				t -> rp = t -> rp -> rp;
    				free(t -> rp -> lp);
    				t -> rp -> lp = t;
    			}
    		}
    	}
    	void disp()
    	{
    		struct dlink *t;
    		if(start == NULL)
    		{
    			printf("Double link list is empty\t");
    		}
    		else
    		{
    			t = start;
    			while(t->rp != start)
    			{
    				printf("%d\t",t -> x);
    				t = t -> rp;
    			}
    			printf("%d\t",t -> x);
    		}
    	}
    	void disprev()
    	{
    		struct dlink *t;
    		if(start == NULL)
    		{
    			printf("Double link list is empty\t");
    		}
    		else
    		{
    			t = start->lp;
    			while(t->lp != start)
    			{
    				printf("%d\t",t->x);
    				t = t -> lp;
    			}		
    			printf("%d\t",t->x);
    		}
    	}