Learning coding means GreatToCode Be more than a Coder ! Greattocode , Join GreatToCode Community,1000+ Students Trusted On Us .If You want to learn coding, Then GreatToCode Help You.No matter what It Takes !


CODE YOUR WAY TO A MORE FULFILLING And HIGHER PAYING CAREER IN TECH, START CODING FOR FREE Camp With GreatToCode - Join the Thousands learning to code with GreatToCode
Interactive online coding classes for at-home learning with GreatToCode . Try ₹Free Per Month Coding Classes With The Top Teachers . Linklist C programming Code |

Linklist C programming Code |

Singly linked list


#include <stdio.h>
#include <stdlibh>

struct node
{
	int data;
	struct node *next;
};
struct node *start,*prev,*curr,*temp;

struct node *create()
{
	int value;
	start=prev=NULL;
	
	printf("\nEnter the value -Terminate by -999\n");
	scanf("%d",&value);
	while(value != -999)
	{
	   curr = (struct node *)malloc(sizeof(struct node));
	   curr->data=value;
	   curr->next=NULL;
	   if (start == NULL)
		start=prev=curr;
	    else
	     {
		prev->next=curr;
		prev=curr;
	     }
	    scanf("%d",&value);
	}
	return(start);
}

void display()
{
	temp=start;
	printf("\nROOT->");
	while (temp != NULL)
	{
	  printf("%d->",temp->data);
	  temp=temp->next;
	}
	 printf("NULL");
}

void main()
{
	create();
	display();
	
}


Singly linked list - Menu Driven



#include <stdio.h>
#include <stdlib.h>


struct node
{
	int data;
	struct node *next;
};
struct node *start,*prev,*curr,*temp;

struct node *create()
{
	int value;
	start=prev=NULL;
	
	printf("\nEnter the value -Terminate by -999\n");
	scanf("%d",&value);
	while(value != -999)
	{
	   curr = (struct node *)malloc(sizeof(struct node));
	   curr->data=value;
	   curr->next=NULL;
	   if (start == NULL)
		start=prev=curr;
	    else
	     {
		prev->next=curr;
		prev=curr;
	     }
	    scanf("%d",&value);
	}
	return(start);
}

void display()
{
	temp=start;
	printf("\nROOT->");
	while (temp != NULL)
	{
	  printf("%d->",temp->data);
	  temp=temp->next;
	}
	 printf("NULL");
}

void odd_display()
{
	printf("\nOdd Display of the list is : \n");
	temp=start;
	printf("\nROOT->");
	while (temp != NULL)
	{
	 if(temp->data %2 != 0)
	  printf("%d->",temp->data);
	  temp=temp->next;
	}
	 printf("NULL");
}


void even_display()
{
       printf("\nEven Display of the list is : \n");
	temp=start;
	printf("\nROOT->");
	while (temp != NULL)
	{
	 if(temp->data %2 == 0)
	  printf("%d->",temp->data);
	  temp=temp->next;
	}
	 printf("NULL");
}

void count()
{
	int count=0;
	temp=start;
	while(temp!=NULL)
	{
		count++;
		temp=temp->next;
	}
	printf("\nTotal number of nodes = %d",count);
}

void main()
{
       int choice;
       
	do
	{
	printf("\n MENU Options \n");
	printf("\n1. CREATE ");
	printf("\n2. DISPLAY ");
	printf("\n3. ODD DISPLAY ");
	printf("\n4. EVEN DISPLAY ");
	printf("\n5. Count Nodes");
	printf("\n6. EXIT ");
	printf("\n Enter the choice :  ");
	scanf("%d",&choice);
	switch(choice)
	{
	case 1 :  create(); break;
	case 2 :  display(); break;
	case 3 : odd_display(); break;
	case 4 : even_display(); break;
	case 5 : count();break;
	}
	}while (choice !=6);
	
}

1.Singly linked list and searching the key element



#include <stdio.h>
#include <stdlib.h>


struct node
{
	int data;
	struct node *next;
};
struct node *start,*prev,*curr,*temp;

struct node *create()
{
	int value;
	start=prev=NULL;
	printf("\nEnter the value -Terminate by -999\n");
	scanf("%d",&value);
	while(value != -999)
	{
	   curr = (struct node *)malloc(sizeof(struct node));
	   curr->data=value;
	   curr->next=NULL;
	   if (start == NULL)
		start=prev=curr;
	    else
	     {
		prev->next=curr;
		prev=curr;
	     }
	    scanf("%d",&value);
	}
	return(start);
}

void display()
{
	temp=start;
	printf("\nROOT->");
	while (temp != NULL)
	{
	  printf("%d->",temp->data);
	  temp=temp->next;
	}
	 printf("NULL");
}

void search()
{
	int i=0,key;
	temp=start;
	printf("\nEnter the key element : ");
	scanf("%d",&key);
	while(temp!=NULL)
	{
		i++;
		if(temp->data == key)
		{
			printf("\nElement found at %d position",i);
			getch();
			exit(0);
		}
		temp=temp->next;
	}
	printf("\nElement not found");
}

void main()
{
	create();
	display();
	search();
	
}

2.Singly linked list and searching the key element


#include <stdio.h>
#include <stdlib.h>


struct node
{
	int data;
	struct node *next;
};
struct node *start,*prev,*curr,*temp;

struct node *create()
{
	int value;
	start=prev=NULL;
	
	printf("\nEnter the value -Terminate by -999\n");
	scanf("%d",&value);
	while(value != -999)
	{
	   curr = (struct node *)malloc(sizeof(struct node));
	   curr->data=value;
	   curr->next=NULL;
	   if (start == NULL)
		start=prev=curr;
	    else
	     {
		prev->next=curr;
		prev=curr;
	     }
	    scanf("%d",&value);
	}
	return(start);
}

void display()
{
	temp=start;
	printf("\nROOT->");
	while (temp != NULL)
	{
	  printf("%d->",temp->data);
	  temp=temp->next;
	}
	 printf("NULL");
}

void search()
{
	int i=0,key;
	temp=start;
	printf("\nEnter the key element : ");
	scanf("%d",&key);
	while(temp!=NULL)
	{
		i++;
		if(temp->data == key)
		{
			printf("\nElement found at %d position",i);
			getch();
			break;
			//exit(0);
		}
		temp=temp->next;
	}
	printf("\nElement not found");
}

struct node *insert()
{
	int pos,i=0;
	printf("\nEnter Position : ");
	scanf("\t%d",&pos);
	curr = (struct node*) malloc(sizeof(struct node));
	printf("\nEnter Data : ");
	scanf("%d",&curr->data);
	curr-> next = NULL;
	if(pos == 1)
	{
		curr-> next = start;
		start = curr;
	}
	else
	{
		for(temp = start,i=1 ;(i < pos-1)&&(temp!=NULL) ;temp = temp->next,i++);
		curr->next = temp->next;
		temp->next = curr;
	}
	return(start);
}

struct node *delete()
{
	struct node *temp1;
	int pos,i;
	printf("\n Enter Position :");
	scanf("%d",&pos);
	if(pos == 1)
	{
		temp = start;
		start = start->next;
		free(temp);
	}
	else
	{
		for(temp = start,i=1;(i < pos -1)&&(temp!=NULL);temp = temp ->next,i++);
		temp1 = temp -> next;
		temp->next = temp1->next;
		free(temp1);
	}
	return(start);
}

void main()
{
	int choice;
	
	do
	{
	printf("\n MENU Options \n");
	printf("\n1. CREATE ");
	printf("\n2. DISPLAY ");
	printf("\n3. SEARCH ");
	printf("\n4. INSERT NODE ");
	printf("\n5. DELETE NODE");
	printf("\n6. EXIT ");
	printf("\n Enter the choice :  ");
	scanf("%d",&choice);
	switch(choice)
	{
	case 1 :  create(); break;
	case 2 :  display(); break;
	case 3 :  search(); break;
	case 4 :  insert(); break;
	case 5 : delete();break;
	}
	}while (choice !=6);
	
}


3.Singly linked list and searching the key element



#include <stdio.h>
#include <stdlib.h>


struct node
{
	int data;
	struct node *next;
};
struct node *start,*prev,*curr,*temp;

struct node *create()
{
	int value;
	start=prev=NULL;
	
	printf("\nEnter the value -Terminate by -999\n");
	scanf("%d",&value);
	while(value != -999)
	{
	   curr = (struct node *)malloc(sizeof(struct node));
	   curr->data=value;
	   curr->next=NULL;
	   if (start == NULL)
		start=prev=curr;
	    else
	     {
		prev->next=curr;
		prev=curr;
	     }
	    scanf("%d",&value);
	}
	return(start);
}

void display()
{
	temp=start;
	printf("\nROOT->");
	while (temp != NULL)
	{
	  printf("%d->",temp->data);
	  temp=temp->next;
	}
	 printf("NULL");
}

/*
struct node *reverse(struct node *f)
{
	struct node *s,*t;
	if(f!=NULL)
	s=f->next;
	if(s!=NULL)
		t=s->next;
	f->next=NULL;
	while(s!=NULL)
	{
		s->next=f;
		f=s;
		s=t;
		if(t!=NULL)
		t=t->next;
	}
	s->next=f;
	printf("\nList reversed....");
	return(f);
}

struct node *reverse(struct node *f)
{
	struct node *s,*t;

	if( f != NULL)
		s = f->next;
	if( s != NULL)
		t = s->next;
	f->next = NULL;
	while ( s != NULL)
	{
		s->next = f;
		f = s;
		s = t;
		if( t != NULL)
			t = t->next;
	}
	return(f);
} // reverese

*/

void reverse()
{
	struct node *p,*c;

	if( start != NULL)
	{
		p =start;
		c = start->next;
		start=start->next;
		p->next = NULL;
	}
	while ( start != NULL)
	{
		start=start->next;
		c->next = p;
		p=c;
		c=start;
	}
	start=p;
	printf("\nSuccesfully reversed....");
} // reverese


void main()
{
	create();
	display();
	reverse();
	display();
	
}


Doubly circular linklist



#include <stdio.h>
#include <stdlib.h>

#define new_node (struct node*)malloc(sizeof(struct node))

struct node
{
	int data;
	struct node *prev;
	struct node *next;
};
struct node *create_list();
struct node *insert_before_n(struct node *f);
struct node *insert_before_data(struct node *f);
struct node *delete_n(struct node *f);
struct node *delete_data(struct node *f);

void main()
{
	struct node *f;
	int option;

	
	f = NULL;
	do
	{
		printf("\n 1. create list");
		printf("\n 2. print forward");
		printf("\n 3. print backward");
		printf("\n 4. insert after n ");
		printf("\n 5. insert after data ");
		printf("\n 6. insert before n ");
		printf("\n 7. insert before data ");
		printf("\n 8. delete_n");
		printf("\n 9. delete_data");
		printf("\n 10. search");
		printf("\n 11. exit");
		printf("\nSelect proper option :");
		scanf("%d",&option);
		switch(option)
		{
			case 1 : release(f); f = create_list(f); break;
			case 2 : print_forward(f);break;
			case 3 : print_backward(f);break;
			case 4 : insert_after_n(f);break;
			case 5 : insert_after_data(f);break;
			case 6 : f = insert_before_n(f);break;
			case 7 : f = insert_before_data(f);break;
			case 8 : f = delete_n(f);break;
			case 9 : f = delete_data(f);break;
			case 10 : search(f);break;
			case 11 : exit(0);
		}//switch
	}while(1);
} // main

int release ( struct node *f)
{
	struct node *t,*t1;
	if( f == NULL)
		return;

		t = f->next;
	while(  t != f)
	{
		t1 = t;
		t = t->next;
		free(t1);
	}
	free(t);
	return;
}//release

struct node *create_list( )
{
	struct node *f,*p,*c;
	int tdata;
	f = p = c = NULL;
	printf("\nEnter data (0 to exit ) : ");
	scanf("%d",&tdata);
	while( tdata != 0 )
	{
		c = new_node;
		if( c== NULL)
		{
			printf("\nInsuff mem");
			exit(0);
		}
		c->prev = NULL;
		c->next = NULL;
		c->data = tdata;
		if ( f == NULL)
			f = c;
		else
		{
			p->next= c;
			c->prev = p;
		}
		p = c;
		printf("\nEnter data (0 to exit ) : ");
		scanf("%d",&tdata);
	} // while
	f->prev = c;
	c->next = f;
	return(f);
} // create list



int print_forward(struct node *f)
{
	struct node *t;
	if( f == NULL)
	{
		printf("\nList is empty");
		return;
	}
	printf("%4d",f->data);
	t = f->next;
	while ( t != f)
	{
		printf("%4d",t->data);
		t = t->next;
	} //while
	return;
} // print forward

int print_backward(struct node *f)
{
	struct node *t;
	if( f == NULL)
	{
		printf("\nList is empty");
		return;
	}
	t = f->prev;
	while(t != f )
	{
		printf("%4d",t->data);
		t = t->prev;
	}//while
	printf("%4d",t->data);
	return;
} // print back

int insert_after_n(struct node *f)
{
	struct node *t,*c,*t1;
	int n,i;

	if( f == NULL)
	{
		printf("\nList is empty");
		return;
	}
	printf("\nEnter n : ");
	scanf("%d",&n);
	if( n == 1 )
		t = f;
	else if ( n > 1 )
	{
		t = f;
		for(i=1;i<=n-1;i++)
		{
			t = t->next;
			if ( t == f)
			{
				printf("\nInvalid node number :");
				return;
			}
		} // for
	} //else
	t1 = t->next;
	c = new_node;
	printf("\nEnter data : ");
	scanf("%d",&c->data);
	c->next= t1;
	c->prev = t;
	t1->prev = c;
	t->next = c;
	return;
} // insert after n



int insert_after_data(struct node *f)
{
	struct node *t,*c,*t1;
	int sdata;

	if( f == NULL)
	{
		printf("\nList is empty");
		return;
	}
	printf("\nEnter sdata : ");
	scanf("%d",&sdata);
	if( f->data == sdata )
		t = f;
	else
	{
		t = f->next;
		while( t->data != sdata && t != f)
			t = t->next;
		if ( t == f)
		{
			printf("\nInvalid sdata");
			return;
		}
	}
	t1 = t->next;
	c = new_node;
	printf("\nEnter data : ");
	scanf("%d",&c->data);
	c->next= t1;
	c->prev = t;
	t1->prev = c;
	t->next = c;
	return;
} // insert after n



struct node *insert_before_n(struct node *f)
{
	struct node *t,*c,*p;
	int n,i;

	if( f == NULL)
	{
		printf("\nList is empty");
		return(f);
	}
	printf("\nEnter n : ");
	scanf("%d",&n);
	if( n == 1 )
		t = f;
	else
	{
		t = f;
		for(i=1;i<=n-1;i++)
		{
			t = t->next;
			if ( t == f)
			{
				printf("\nInvalid node number :");
				return(f);
			} //if
		} // for
	} //else
	p = t->prev;
	c = new_node;
	printf("\nEnter data : ");
	scanf("%d",&c->data);
	c->next= t;
	c->prev = p;
	t->prev = c;
	p->next = c;
	if( f == t )
		f = c;
	return(f);
} // insert before n


struct node *insert_before_data(struct node *f)
{
	struct node *t,*c,*p;
	int sdata;

	if( f == NULL)
	{
		printf("\nList is empty");
		return(f);
	}
	printf("\nEnter sdata : ");
	scanf("%d",&sdata);
	if( f->data == sdata)
		t = f;
	else
	{
		t = f->next;
		while ( t->data != sdata && t != f)
			t = t->next;
			if ( t == f)
			{
				printf("\nInvalid sdata :");
				return(f);
			} //if
	} // else
	p = t->prev;
	c = new_node;
	printf("\nEnter data : ");
	scanf("%d",&c->data);
	c->next= t;
	c->prev = p;
	t->prev = c;
	p->next = c;
	if( sdata == f->data )
		f = c;
	return(f);
} // insert befoer data



struct node *delete_n(struct node *f)
{
	struct node *t,*t1,*t2;
	int n,i;

	if( f == NULL)
	{
		printf("\nList is empty");
		return(f);
	}
	printf("\nEnter n : ");
	scanf("%d",&n);
	if( n == 1 )
	{
		if( f->next == f )
		{
			free(f);
			return(NULL);
		}
		else
			t = f;
	}
	else
	{
		t = f;
		for(i=1;i<=n-1;i++)
		{
				t = t->next;
				if ( t == f)
				{
					printf("\nInvalid node number :");
					return(f);
				}
		} // for
	} //else
	t1 = t->prev;
	t2 = t->next;
	t2 = t->next;
		t1->next = t2;
		t2->prev = t1;
	if( f == t )
		f = f->next;
	free(t);
	return(f);
} // delete_n

struct node *delete_data(struct node *f)
{
	struct node *t,*t1,*t2;
	int sdata;

	if( f == NULL)
	{
		printf("\nList is empty");
		return(f);
	}
	printf("\nEnter sdata : ");
	scanf("%d",&sdata);
	if( f->data == sdata )
	{
		if( f->next == f )
		{
			free(f);
			return(NULL);
		}
		else
			t = f;
	}
	else
	{
		t = f->next;
		while( t->data != sdata && t != f)
				t = t->next;
		if ( t == f)
		{
			printf("\nInvalid sdata:");
			return(f);
		}
	} // else
	t1 = t->prev;
	t2 = t->next;
	if( sdata == f->data )
		f = f->next;
	t1->next = t2;
	t2->prev = t1;
	free(t);
	return(f);
} // delete_data

int search ( struct node *f)
{
	struct node *t,*t1,*t2;
	int sdata;

	if( f == NULL)
	{
		printf("\nList is empty");
		return;
	}
	printf("\nEnter sdata : ");
	scanf("%d",&sdata);
	if( f->data == sdata )
	{
		printf("%4d",f->data);
		return;
	}
	else
	{
		t = f->next;
		while( t->data != sdata && t != f)
				t = t->next;
		if ( t == f)
		{
			printf("\nInvalid sdata:");
			return;
		}
		else
		{
			printf("%4d",t->data);
			return;
		}
	} // else
}

Queue .C

#include <stdio.h>
#include <stdlib.h>
#define max 3


void main()
{
	int queue[max],data;
	int front,rear,reply,option;

	clrscr();
	//... init queue
	front = rear = 0;
	do
	{
		printf("\n 1. insert queue");
		printf("\n 2. delete queue");
		printf("\n 3. exit");
		printf("\nSelect proper option :");
		scanf("%d",&option);
		switch(option)
		{
			case 1 ://insert
				printf("\nEnter data : ");
				scanf("%d",&data);
				reply = insertq(queue,&front,&rear,&data);
				if ( reply == - 1)
					printf("Que is full");
				else
					printf("Inserted data in queue");
				break;
			case 2 : //dele
				reply = delq(queue,&front,&rear,&data);
				if ( reply == -1 )
					printf("Queue is empty ");
				else
					printf("Deleted data is : %d", data);
				break;
			case 3 : exit(0);
		} //switch
	} while(1);
} // main


int insertq ( int queue[max], int *front, int *rear , int *data)
{
	if (  (*rear + 1 ) % max == *front)
		return(-1);
	else
	{
		*rear = (*rear + 1) % max;
		queue[*rear] = *data;
		return(1);
	} // else
} // insert



int delq( int queue[max], int *front, int *rear , int *data)
{
	if ( *front ==  *rear)
		return(-1);
	else
	{
		*front = (*front + 1 ) % max;
		*data = queue[*front];
		return(1);
	} // else
} // insert


QUEUE



Post a Comment

0 Comments

•Give The opportunity to your child with GreatToCode Kid's • Online Coding Classes for Your Kid • Introduce Your kid To the world's of coding
•Fuel You Career with our 100+ Hiring Partners, Advance Your Career in Tech with GreatToCode. •Join The Largest Tech and coding Community and Fast Forward Your career with GreatToCode. •10000+ Learner's+ 90 % placement Guarantee. • Learning Coding is Better with the GreatToCode community .
•Greattocode Kid's •GreatToCode Career •GreatToCode Interview •GreatToCode Professional •GreatToCode for schools •GreatToCode For colleges •GreatToCods For Businesses.
Are you ready to join the millions of people learning to code? GreatToCode Pass is your one-stop-shop to get access to 1000+ courses, top-notch support, and successful job placement. What are you waiting for? Sign up now and get your future in motion with GreatToCode Pass.