Linked List Implementation in C with many options

This program include Singly Linked List implementation in C with options such as insert at beginning, any position, delete at given position, reverse the contents, search the given content, display, append the contents.

#include<stdio.h>
#include<malloc.h>

void append();
int del(int);
void display();
int insert();
int insertn();
int search(int);
void rev();

int num,loc;
char name[20];

struct node
{
	int a;
	char n[20];
	struct node *next;
};
struct node *first,*last;

//first=last=NULL;
int main()
{
	int ch;
	do
	{
		printf("\n Enter the choice \n 1.append 2.display 3.Insert 4.delete 5.insert at any position 6.Search 7.reverse 8.exit \n");
		scanf("%d",&ch);
		switch(ch)
		{
			case 1:
				append();
				break;
			case 2:
				display();
				break;
			case 3:
				insert();
				break;
			case 4:
				printf("\n enter the value \n");
				scanf("%d",&num);
				del(num);
				break;
			case 5:
				printf("\n enter the location \n");
				scanf("%d",&loc);
				insertn(loc);
				break;
			case 6:
				printf("\n enter the value to find \n");
				scanf("%d",&num);
				search(num);
				break;
			case 7:
				rev(first);
				break;
			default:
				break;
		}
	}while(ch!=8);
}
int insert()
{
	struct node *temp;
	temp=(struct node *)malloc(sizeof(struct node));
	printf("\n enter val\n");
	scanf("%d",&temp->a);
	printf("\n enter the name \n");
	scanf("%s",temp->n);
	if(first==NULL)
	{
		first=temp;
		first->next=NULL;
	}
	else
	{
		temp->next=first;
		first=temp;
	}
	display();
}

void append()
{
	struct node *newnode;
	newnode=(struct node*)malloc(sizeof(struct node));
	if(newnode==NULL)
	{
		printf("\n memory not allocated");
	}
	printf("\n enter the val\n");
	scanf("%d",&newnode->a);
	printf("\n enter the name\n");
	scanf("%s",newnode->n);
	newnode->next=NULL;
	if(first==NULL)
	{
		first=newnode;
	}
	else
	{
		last->next=newnode;
	}
	last=newnode;

}
int del(int num)
{
	struct node *temp,*m;
	temp=first;

	while(temp!=NULL)
	{
		if(temp->a==num)
		{
			if(temp==first)
			{
				first=temp->next;
				free(temp);
				return;
			}
			else
			{
				m->next=temp->next;
				free(temp);
				return;
			}
		}
		else
		{
			m=temp;
			temp=temp->next;
		}
	}
}
//insert at any position
int insertn(int loc)
{
	int i;
	struct node *temp,*t,*pre;
	pre=first;
	if(loc==1)
	{
		insert();
		return;
	}
	else
	{
		for(i=1;i<loc;i++)
		{
			t=pre;
			pre=pre->next;
		}
		temp=(struct node *)malloc(sizeof(struct node));
		printf("\n enter the value\n");
		scanf("%d",&temp->a);
		printf("\n enter the name\n");
		scanf("%s",temp->n);

		t->next=temp;
		t=temp;
		t->next=pre;
		pre=pre->next;
	}
	display();
}
int search(int num)
{
	struct node *temp;
	temp=first;
	while(temp!=NULL)
	{
		if(temp->a==num)
		{
			printf("%d is  exist",num);
			printf("\t its name is %s",temp->n);
			break;
		}
		else
			temp=temp->next;

	}
	if(temp->a!=num)
	{

		printf("not exist");
	}
}
void rev(struct node *st)
{
	struct node *temp,*n,*s;
	temp=st;
	n=NULL;
	while(temp!=NULL)
	{
		s=n;
		n=temp;
		temp=temp->next;
		n->next=s;
	}
	first=n;
	display();
}

void display()
{
	struct node *temp;
	temp=(struct node*)malloc(sizeof(struct node));
	temp=first;
	while(temp!=NULL)
	{
		printf("%d\t%s\t",temp->a,temp->n);
		temp=temp->next;
	}
}
VN:F [1.9.11_1134]
Rating: 4.8/10 (5 votes cast)
VN:F [1.9.11_1134]
Rating: 0 (from 0 votes)

23. December 2011 by divya
Categories: C Programming, Data Structures | Tags: | Leave a comment

← Older posts