Table of Contents
Single Link List in C Language
Double Link List in C Language
Single Link List in C Language
Single Link List in C language
(Code without Any Error)
#include
#include
struct node
{
int info;
struct node *link;
};
struct node *START=NULL;
struct node * CreateNode()
{
struct node *n;
n=(struct node *)malloc(sizeof(struct node));;
return(n);
}
void insertNode()
{
struct node *temp,*t;
temp=CreateNode();
printf("Enter the number:");
scanf("%d",&temp->info);
temp->link=NULL;
if(START==NULL)
START=temp;
else{
t=START;
while(t->link!=NULL)
{
t=t->link;
}
t->link=temp;
}
}
void deleteNode()
{
struct node *r;
if(START==NULL)
printf("List is empty");
else{
r=START;
START=START->link;
free(r);
}
}
void viewList()
{
struct node *t;
if(START==NULL)
printf("List is empty");
else
{
t=START;
while(t!=NULL)
{
printf("%d \t",t->info);
t=t->link;
}
}
}
int menu()
{
int ch;
printf("\n1: Add value to the list");
printf("\n2: Delete first value");
printf("\n3: View List");
printf("\n4:Exit");
printf("\nEnter the choice");
scanf("%d",&ch);
return(ch);
}
void main()
{
while(1)
{
switch(menu())
{
case 1:
insertNode();
break;
case 2:
deleteNode();
break;
case 3:
viewList();
break;
case 4:
exit(0);
default:
printf("\nvalid choice");
}
}
}
Double Link List in C Language
Double Link List in C Language
(Code without Any Error)
#include<stdio.h>
struct node{
int info;
struct node *prev,*next;
};
struct node *start=NULL;
void insertAsFirstNode()
{
struct node *n;
n=(struct node *)malloc(sizeof(struct node));
printf("Enter the number :");
scanf("%d",&n->info);
n->prev=NULL;
n->next=NULL;
if(start==NULL)
start=n;
else
{
start->prev=n;
n->next=start;
start=n;
}
}
void deleteFirstNode()
{
struct node *r;
if(start==NULL)
printf("List is Empty");
else
{
r=start;
start=start->next;
start->prev=NULL;
free(r);
}
}
void viewList()
{
struct node *t;
if (start==NULL)
printf("List is Empty!");
else
{
t=start;
while(t!=NULL)
{
printf("%d\t",t->info);
t=t->next;
}
}
}
int menu()
{
int a;
printf("\n 1:Add value to the list");
printf("\n 2:Delete First Node");
printf("\n 3:ViewList ");
printf("\n 4: Exit");
printf("\n\nEnter the choice :!");
scanf("%d",&a);
return(a);
}
main()
{
while(1)
{
switch(menu())
{
case 1:
insertAsFirstNode();
break;
case 2:
deleteFirstNode();
break;
case 3:
viewList();
break;
case 4:
exit(1);
default:
printf("Wrong input!");
}
}
}