Monday, August 27, 2007

Link list programs in C (single, doubly and circular link list)

Link list programs in C (single, doubly and circular link list)
--------------------------------------------------------------------------------------



Singly link list
=================


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

struct list
{
int info;
struct list *next;
};
typedef struct list node;

void main ()
{
node *first;
int choice;
node *create (node *);
node *insert (node *);
node *del (node *);
void display (node *);

clrscr ();

while (1)
{
printf("\n\nYou have following choices:\n");
printf("1. Create List\n");
printf("2. Insert a node in the List\n");
printf("3. Delete a node from the List\n");
printf("4. Display the List\n");
printf("5. Termenate the Program\n");
printf("Enter your choice: ");
scanf ("%d",&choice);
switch (choice)
{
case 1:
first = create(first);
break;
case 2:
first = insert (first);
break;
case 3:
first = del (first);
break;
case 4:
display (first);
break;
case 5:
exit (0);
}
}
}

node *create (node *first)
{
node *new1 = NULL,*l;
int i;
first = NULL;
printf("\n\nTHE INPUT IS:\n");
printf("-------------\n\n");
while(1)
{
printf("\nEnter -1 to break.....\n");
printf("Enter the node you want to enter in ascending order: ");
scanf("%d",&i);

if (i == -1)
break;

else
{
new1 = ((node *) malloc (sizeof (node)));
new1 -> info = i;
if(first == NULL)
{
first = l = new1;
}
else
{
l -> next = new1;
l = new1;
}
l -> next = NULL;
}
}
return (first);
}

void display (node *first)
{
printf("\n\nTHE OUTPUT IS:\n");
printf("--------------\n\n");
while (first != NULL)
{
printf("%d -> ",first -> info);
first = first -> next;
}
printf("NULL");
}

node *insert (node *first)
{
node *l, *new1;
l = first;
new1 = (node *) malloc (sizeof (node *));
printf("Enter the node: ");
scanf("%d",&new1 -> info);
if (first == NULL)
{
first = new1;
first -> next = NULL;
}
else
{
if (new1 -> info < first -> info)
{
new1 -> next = first;
first = new1;
}
else
{
while (l -> next -> info < new1 -> info && l -> next != NULL)
l = l -> next;
new1 -> next = l -> next;
l -> next = new1;
l = new1;
}
}
display (first);
return (first);
}

node *del (node *first)
{
int n;
node *l = first, *new1, *temp;
printf("Enter the information: ");
scanf("%d",&n);
if (first == NULL)
printf("\nLIST IS EMPTY\n");
else
{
if (first -> info == n)
{
temp = first;
first = first -> next;
free (temp);
}
else
{
while (l -> next -> info != n && l -> next != NULL)
l = l -> next;
if (l -> next == NULL)
printf("\nINFORMATION IS NOT PRESENT\n");
else
{
temp = l -> next;
l -> next = l -> next -> next;
free(temp);
}
}
}
display (first);
return (first);
}

Doubly Link List
===============

#include <stdio.h>
#include <alloc.h>
#include <conio.h>

struct Double
{
int info;
struct Double *next;
struct Double *previous;
};
typedef struct Double node;

node *create(node *);
node *insert(node *);
node *del(node *);
void display (node *);

node *first = NULL;

void main ()
{
int choice;

clrscr ();

while (1)
{
printf("\n\nYou have following choices:\n");
printf("1. Create List\n");
printf("2. Insert a node in the List\n");
printf("3. Delete a node from the List\n");
printf("4. Display the List\n");
printf("5. Termenate the Program\n");
printf("Enter your choice: ");
scanf ("%d",&choice);
switch (choice)
{
case 1:
first = create(first);
break;
case 2:
first = insert (first);
break;
case 3:
first = del (first);
break;
case 4:
display (first);
break;
case 5:
exit (0);
}
}
}

node *create (node *first)
{
node *new1,*l = NULL;
int i;
//first -> next = first -> previous = NULL;
while (1)
{
printf("Enter the information(-1 to Exit): ");
scanf("%d",&i);
if (i == -1)
break;
new1 = (node *)malloc(sizeof(node));
new1 -> info = i;
if (first == NULL)
{
first = l = new1;
first -> next = first -> previous = NULL;
}
else
{
new1 -> previous = l;
l -> next = new1;
l = new1;
}
l -> next = NULL;
}
return (first);
}

void display (node *first)
{
node *l;
l = first;
printf("\n\n\t\tDOUBLY LINK LIST:\n");
printf("\t\t-----------------\n");
printf("NULL");
for (l = first ; l != NULL ; l = l -> next)
printf(" <- %d -> ",l -> info);
printf("NULL");
}

node *insert (node *first)
{
node *new1,*l=NULL;
new1 = (node *)malloc(sizeof(node));
printf("\n\nEnter the node you want to insert: ");
scanf("%d",&new1->info);
if (first == NULL)
{
printf("Empty list. New list will be created.");
first = new1;
first->next = NULL;
first->previous = NULL;
}
else
{
if (new1->info < first->info)
{
new1->next = first;
new1->previous = NULL;
first->previous = new1;
first = new1;
}
else
{
l = first;
while (l->next->info < new1->info && l->next != NULL)
l = l->next;
new1->next = l->next;
l->next = new1;
new1->previous = l;
l = new1;
}
}
display (first);
return (first);
}

node *del (node *first)
{
int del_info;
node *l=NULL,*temp;
printf("\n\nEnter the information to be deleted: ");
scanf("%d",&del_info);
if (first == NULL)
printf("The List is Empty.");
else
{
if (first->info == del_info)
{
temp = first;
first = first->next;
first->previous = NULL;
free(temp);
}
else
{
l = first;
while (l->info < del_info && l->next != NULL)
l = l->next;
l->previous->next = l->next;
l->next->previous = l->previous;
}
}
display (first);
return (first);
}

Circular link list
=================


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

///////////////////////STRUCTURE DEFINITION////////////////////////////////
struct cir_link_list
{
int info;
struct cir_link_list *next;
};
typedef struct cir_link_list node;

///////////////////////VARIABLE DECLARATION////////////////////////////////
node *first=NULL;
int info;

///////////////////////FUNCTION DECLARATION////////////////////////////////
node *create (node *);
void display (node *);
node *insert (node *);
node *del (node *);
int count = 0;

///////////////////////VOID MAIN () DEFINITION/////////////////////////////
void main ()
{
int choice;

clrscr ();

while (1)
{
printf("\n\nYou have following choices:\n");
printf("1. Create List\n");
printf("2. Insert a node in the List\n");
printf("3. Delete a node from the List\n");
printf("4. Display the List\n");
printf("5. Termenate the Program\n");
printf("Enter your choice: ");
scanf ("%d",&choice);
switch (choice)
{
case 1:
first = create(first);
break;
case 2:
first = insert (first);
break;
case 3:
first = del (first);
break;
case 4:
display (first);
break;
case 5:
exit (0);
}
}
}

///////////////////////CREATION OF CIRCULAR LINK LIST//////////////////////
node *create (node *first)
{
int i;
node *new1,*l=NULL;

while (1)
{
printf("Enter -1 to break........\n");
printf("Enter the node: ");
scanf("%d",&i);
if (i == -1)
{
l->next=first;
break;
}
else
{
new1 = ((node *)malloc(sizeof(node)));
new1 -> info = i;
if (first == NULL)
{
first = l = new1;
}
else
{
l->next=new1;
l=new1;
}
}
count++;
}
return (first);
}

///////////////////////DISPLAY OF CIRCULAR LINK LIST///////////////////////
void display (node *first)
{
int i;
for (i=0 ; i< count ; i++)
{
printf("%d -> ",first->info);
first = first -> next;
}
}

node *insert (node *first)
{
int i = 0;
node *l, *new1;
l = first;
new1 = (node *) malloc (sizeof (node *));
printf("\nEnter the node: ");
scanf("%d",&new1 -> info);
if (first == NULL)
{
first = new1;
first -> next = first;
}
else
{
if (new1 -> info < first -> info)
{
new1 -> next = first;
first = new1;
}
else
{
while (l -> next -> info < new1 -> info && l -> next != first && i <= count)
{
l = l -> next;
i++;
}
if (l -> next == first i > count)
{
new1 -> next = l -> next;
l -> next = new1;
l = new1;
}
new1 -> next = l -> next;
l -> next = new1;
}
}
count++;
display (first);
return (first);
}

node *del (node *first)
{
int n,i=0;
node *l = first, *new1, *temp;
printf("\nEnter the information: ");
scanf("%d",&n);
if (first == NULL)
printf("\nLIST IS EMPTY\n");
else
{
if (first -> info == n)
{
temp = first;
first = first -> next;
free (temp);
}
else
{
while (l -> next -> info != n && l -> next != first && i <= count)
{
l = l -> next;
i++;
}
if (l -> next == first i > count)
{
printf("\nINFORMATION IS NOT PRESENT\n");
goto jmp;
}
else
{
temp = l -> next;
l -> next = l -> next -> next;
free(temp);
}
}
}
count --;
jmp:
display (first);
return (first);
}

No comments: