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.14_1148]
Rating: 6.9/10 (9 votes cast)
VN:F [1.9.14_1148]
Rating: +1 (from 1 vote)
Linked List Implementation in C with many options, 6.9 out of 10 based on 9 ratings

Related posts:

  1. Implementation of stack using linked list
  2. C Program of Double Linked List
  3. C Program for Circular Linked List
  4. Various Linked List Operations
  5. queue operations using linked list

About divya

This is Divya Sundaram, completed Engineering in computer Science from Anna university of technology,India.Currently working in a leading reputed IT concern as a Programmer Analyst role .I am Interested in new ideas and innovatives in Information technology.A keen interest to explore the latest news of NASA . Spending more time to design business cards,websites,post cards,ads,blogging.

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

Leave a Reply

Required fields are marked *

*