Tuesday, January 06, 2015

Working with Singly Linked List

Posted By: BackBenchers World - Tuesday, January 06, 2015

Share

& Comment

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

struct node
{
int info;
struct node *rpt;
}*first;

void create()
{
struct node *ptr, *cpt;
char ch;

ptr=(struct node*)malloc(sizeof(struct node));    //Allocating memory to ptr

printf("Enter the information of first node: ");
scanf("%d",&ptr->info);

first=ptr;                                       //Initializing 'first'

do
{
cpt=(struct node*)malloc(sizeof(struct node));    //Allocating memory to cpt

printf("\nEnter the information of next node: ");
scanf("%d",&(cpt->info));

ptr->rpt=cpt;
ptr=cpt;

printf("\nPress <Y> for more nodes\n");
ch=getch();
}
while(ch=='y'||ch=='Y');

ptr->rpt=NULL;
}

void traverse()
{
struct node *ptr;
ptr=first;

if(ptr==NULL) printf("List is empty");

else
{
printf("\nThe Elements in the list are: ");
while(ptr!=NULL)
{
printf("\t%d",ptr->info);
ptr=ptr->rpt;
}
}
}

void binsert()
{
struct node *ptr;

ptr=(struct node*)malloc(sizeof(struct node));    //Allocating memory to ptr

printf("Enter the information of node: ");
scanf("%d",&ptr->info);

ptr->rpt=first;
first=ptr;
traverse();
}

void einsert()
{
struct node *ptr, *cpt;
cpt=first;

ptr=(struct node*)malloc(sizeof(struct node));    //Allocating memory to ptr

printf("Enter the information of node: ");
scanf("%d",&ptr->info);

while(cpt->rpt!=NULL)
{
cpt=cpt->rpt;
}
cpt->rpt=ptr;
cpt=ptr;
cpt->rpt=NULL;
traverse();
}

void dinsert()
{
struct node *ptr, *cpt;
int data;
cpt=first;

printf("Enter the node information after which you want to insert: ");
scanf("%d",&data);

while(cpt->info!=data)
{
cpt=cpt->rpt;
}

ptr=(struct node*)malloc(sizeof(struct node));    //Allocating memory to ptr

printf("Enter information of node to be inserted: ");
scanf("%d",&ptr->info);

ptr->rpt=cpt->rpt;
cpt->rpt=ptr;
traverse();
}

void bdel()
{
struct node *ptr;
ptr=first;

first=first->rpt;
free(ptr);
traverse();
}

void edel()
{
struct node *ptr, *cpt;
ptr=first;

if(ptr->rpt==NULL)
{
bdel();
}

else
{
while(ptr->rpt!=NULL)
{
cpt=ptr;
ptr=ptr->rpt;
}
cpt->rpt=NULL;
free(ptr);
traverse();
}
}

void ddel()
{
struct node *ptr, *cpt;
int data;
cpt=first;

printf("Enter the node information you want to delete: ");
scanf("%d",&data);

while(cpt->info!=data)
{
ptr=cpt;
cpt=cpt->rpt;
}

if(cpt==first)
{
bdel();
}

else
{
ptr->rpt=cpt->rpt;
free(cpt);
traverse();
}
}


void main()
{
clrscr();
char ch=1;
char choice;
printf("Working with Data Strucrure\n\n");
printf("Menu\n\n1. Create\n2. Insert at Beginning\n3. Insert After Given Info Node\n");
printf("4. Insert at End\n5. Delete Node From Beginning\n6. Delete Given Info Node\n");
printf("7. Delete Node From End\n8. Traverse\n0. Exit");

while(ch)
{
printf("\n\nEnter your choice: \n");
choice=getch();

switch(choice)
{
case '1': create();
break;

case '2': binsert();
break;

case '3': dinsert();
break;

case '4': einsert();
break;

case '5': bdel();
break;

case '6': ddel();
break;

case '7': edel();
break;

case '8': traverse();
break;

case '0': return;

default: printf("Wrong choice");
}
}
}

About BackBenchers World

Techism is an online Publication that complies Bizarre, Odd, Strange, Out of box facts about the stuff going around in the world which you may find hard to believe and understand. The Main Purpose of this site is to bring reality with a taste of entertainment

0 comments:

Post a Comment

Copyright © 2013 TechDotHunter™ is a registered trademark.

Designed by Templateism. Hosted on Blogger Platform.