Circular Linked List

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Circular Linked List as PDF for free.

More details

  • Words: 403
  • Pages: 6
#include<stdio.h> #include #include<stdlib.h> typedef struct cllist{ int x; struct cllist *next; }nd; nd*insert(nd**,nd**,nd**); nd*delete(nd**,nd**,nd**); nd*insfro(nd**,nd**,nd**); nd*insaftnd(nd**,nd**,nd**); void display(nd*,nd*); void main() { int n; nd *start,*current,*head; head=(nd*)malloc(sizeof(nd)); start=head; current=start; do{ clrscr(); textbackground(red); textcolor(black+blink); printf(" circular linked list _________________________________________ "); printf(" main menu ---------------------------"); printf(" 1>. insert 2>.insertion at front node

3>.inserion after a 4>.deletion 5>.end

"); printf(" ______________________________________________________ "); printf(" enter your choice: "); scanf("%d",&n); switch(n) { case 1: insert(&head,&start,�t); break;

case 2: insfro(&head,&start,�t); break; case 3: insaftnd(&head,&start,�t); break; case 4: if(start==head){ printf(" the list empty!"); break; } delete(&head,&start,�t); break; case 5: clrscr(); printf("

");

printf(" press any key to exit..."); getch(); exit(0); default: printf(" invalid choice!"); getch(); break; } }while(n!=5); getch(); } nd*insert(nd**head,nd**start,nd**current) { int m; nd*temp,*val; clrscr(); printf(" n");

insertion ______________________________________

do{ temp=(nd*)malloc(sizeof(nd)); if(!temp ){ printf("memory allocation not possible!"); getch(); exit(0); } val=temp; temp=temp->next;

printf(" enter the element:"); scanf("%d",&m); val->x=m; val->next=*head; if(*start==*head){ (*head)->next=val; *start=val; *current=*start; } else{ (*current)->next=val; *current=val; } printf(" any more inserton(y/n):"); fflush(stdin); }while(toupper(getch())!='n'); display(*head,*start); getch(); return(*head,*start,*current); } void display(nd*head,nd*start) { if(head==start){ printf(" the list is empty!"); getch(); return; } else{ printf(" the list is now: "); do{ printf("%5d",start->x); start=start->next; }while(start!=head); } return; } nd*delete(nd**head,nd**start,nd**current) { int ele; nd *temp,*save,*val,*pred; clrscr(); printf(" deletion " );

___________________________________ save=*start; do{ display(*head,*start );

val=save; printf(" enter the element to be deleted:"); scanf("%d",&ele); while(save->x!=ele && save->next!=val){ pred=save; save=save->next; } if(save->next==val){ printf(" the elment is not in the list!"); getch(); break; } temp=save; printf(" the element to be deleted is:%d",temp->x); printf(" r u sure(y/n):"); fflush(stdin); if(toupper(getch())!='n'){ if(save==(*head)->next){ *start=(*start)->next; free(temp); save=*start; } else if(save->next==*head){ pred->next=*head; *current=pred; free(temp); save=pred; } else{ pred->next=temp->next; free(temp); save=pred; } } else break; printf(" anymore deletion(y/n):"); fflush(stdin); }while(toupper(getch())!='n'); display(*head,*start); getch(); return(*head,*start,*current); } nd*insfro(nd**head,nd**start,nd**current) { int m; nd *temp,*val; clrscr(); printf(" insertion in front ");

");

printf(" __________________________________

do{ temp=(nd*)malloc(sizeof(nd)); if(!temp){ printf(" memory allocation not possible!"); getch(); exit(0); } val=temp; temp=temp->next; printf(" enter the element:"); scanf("%d",&m); val->x=m; if(*start==*head){ val->next=*head; (*head)->next=val; *start=val; *current=*start; } else{ (*head)->next=val; val->next=*start; *start=val; } printf(" any more insertion to be performed(y/n):"); fflush(stdin); }while(toupper(getch())!='n'); display(*head,*start); getch(); return(*head,*start,*current); } nd*insaftnd(nd**head,nd**start,nd**current) { int trg,n=1,m; nd *temp,*val,*save,*pred; save=*start; clrscr(); printf(" insertion after a node "); ");

printf(" __________________________________

do{ display(*head,*start); temp=(nd*)malloc(sizeof(nd)); if(!temp){ printf(" memory allocation not possible!"); getch(); exit(0); }

val=temp; temp=temp->next; printf(" enter the element:"); scanf("%d",&m); val->x=m; if(*start==*head){ val->next=*head; (*head)->next=val; *start=val; *current=*start; } else{ printf(" enter the position of insertion:"); scanf("%d",&trg); while(trg!=n){ pred=save; save=save->next; n++; if(save==*head) n=0; } pred->next=val; val->next=save; save=save->next; } printf(" any more insertion(y/n):"); fflush(stdin); }while(toupper(getch())!='n'); display(*head,*start); getch(); return(*head,*start,*current); }

Related Documents

Circular Linked List
November 2019 9
Linked List
November 2019 12
Linked List
May 2020 8
Linked List Notes
November 2019 16
Materi 7 - Linked List
November 2019 19