Link List programs in C
Index
- single link list creation
- insertion and delete in single link list
1.single link list creation
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
}*start=NULL;
void creat()
{
char ch;
do
{
struct node *new_node,*current;
new_node=(struct node *)malloc(sizeof(struct node));
printf("nEnter the data : ");
scanf("%d",&new_node->data);
new_node->next=NULL;
if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
current->next=new_node;
current=new_node;
}
printf("nDo you want to creat another : ");
ch=getche();
}while(ch!='n');
}
-------------------------------------------------------------------------------------
void display()
{
struct node *new_node; printf("The Linked List : n");
new_node=start;
while(new_node!=NULL)
{
printf("%d\n",new_node->data);
new_node=new_node->next;
}
printf("NULL");
}
void main()
{
create();
display();
}
------------------------------------------------------------------------------------- 2. insertion and delete in single link list
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*first=NULL;
void insert()
{
struct node *temp;
struct node *nn=(struct node*)malloc(sizeof(struct node));
printf("enter the data\n");
scanf("%d",&nn->data);
temp=first;
while(temp->next!=first)
temp=temp->next;
temp->next=nn;
nn->next=NULL;
}
void display()
{
struct node *temp;
temp=first;
if(temp==NULL)
{
printf("no elements\n");
return;
}
printf("elements in linked list are\n");
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}
boolean insertAtBeginning(node **head, int ele) {
node *ptr;
ptr = (node *)malloc(sizeof(node));
ptr->info = ele;
if(*head == NULL) {
ptr->next = NULL;
}
else {
ptr->next = *head;
}
*head = ptr;
return true;
}
boolean insertAtEnd(node **head, int e) {
node *ptr, *location;
ptr = (node *)malloc(sizeof(node));
ptr->info = e;
ptr->next = NULL;
if(*head == NULL) {
*head = ptr;
}
else {
location = *head;
while(location->next != NULL) {
location = location->next;
}
location->next = ptr;
}
return true;
}
void deletion()
{
struct node *temp;
temp=first;
first=first->next;
temp->next=NULL;
free(temp);
}
boolean deleteFromBeginning(node **head, int *element) {
node *delNode;
if(*head == NULL)
return false;
else {
delNode = *head;
*element = (*head)->info;
*head = (*head)->next;
free(delNode);
return true;
}
}
boolean deleteFromEnd(node **head, int *num) {
node *deleteNode, *loc;
if(*head == NULL)
return false;
else if((*head)->next == NULL) {
*head = NULL;
return done;
}
else {
loc = *head;
while(loc->next->next != NULL)
loc = loc->next;
deleteNode = loc->next;
*num = deleteNode->info;
loc->next = NULL;
free(deleteNode);
return true;
}
}
boolean deleteList(node **list) {
node *ptr;
while(*list != NULL) {
ptr = *list;
*list = (*list)->next;
free(ptr);
}
return done;
}
int main()
{
int op;
do
{
printf("1.insertion\n2.deletion\n3.display\n4.insertAtBeginning\n5.insertAtEnd\n6.deleteFromEnd\n7.deleteList/n8.exit\n");
printf("enter option\n");
scanf("%d",&op);
switch(op)
{
case 1:insert();
break;
case 2:deletion();
break;
case 3:display();
break;
case 4:insertAtBeginning();
break;
case 5:insertAtEnd();
break;
case 6:deleteFromEnd();
break;
case 7:deleteList();
break;
default:
printf("you have enter wrong input\n");
}
}while(op!=6);
}
No comments:
Post a Comment