circular linked list program example.docx

download circular linked list program example.docx

of 22

Transcript of circular linked list program example.docx

  • 7/29/2019 circular linked list program example.docx

    1/22

    Circular linked list

    #include

    #include

    #include

    #include

    /***** Structure template *****/

    struct list{

    int roll_no;

    char name[20];

    float marks;

    struct list *next;

    };

    /***** Redefining struct list as node *****/

    typedefstruct list node;

    void init(node*);void ins_aft(node*); /*** FUNCTION FOR DELETING A NODE AFTER ***/

    node* ins_bef(node*); /*** FUNCTION FOR INSERTING A NODE BEFORE ***/

    node* del(node*); /*** FUNCTION FOR DELETING A NODE ***/

    void search(node*); /*** FUNCTION FOR SEARCHING CRITERIA INPUT ***/

    void disp(node*); /*** FUNCTION DISPLAYING THE NODES ***/

    void rollsrch(node*); /*** FUNCTION SEARCHING THROUGH ROLL NUMBER ***/

    void namesrch(node*); /*** FUNCTION SEARCHING THROUGH NAME ***/

    void marksrch(node*); /*** FUNCTION SEARCHING THROUGH MARKS ***/

    /***** Main function *****/

    void main()

    {

    node *head; /* HEAD OF THE LINK LIST */

    char ch; /* Choice inputing varible */

    int opt; /* Option inputing variable*/

    staticint flag=0; /* Unchanged after iniialization */

    clrscr();

    head=(node*)malloc(sizeof(node));

    head->next=NULL; /* NULL is being over written in init function */

    do

    {

    again:

    printf("\nEnter your option\n");

    printf("\n1. Initialize the node\n");

    printf("\n2. Insert before a specified node\n");printf("\n3. Insert after a specified node\n");

    printf("\n4. Delete a particular node\n");

    printf("\n5. Search the nodes\n");

    printf("\n6. Display all the nodes\n");

    scanf("%d",&opt);

    if(flag==0 && opt!=1)

    {

    printf("\nNo. You must first initialize at least one node\n");

    http://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.html
  • 7/29/2019 circular linked list program example.docx

    2/22

    goto again;

    }

    if(flag==1 && opt==1)

    {

    printf("\nInitialisation can occur only once.\n");

    printf("\nNow you can insert a node\n");

    goto again;}

    if(opt==4 && head->next==head)

    {

    printf("\nYou cannot delete the one and only the single node\n");

    goto again;

    }

    if(flag==0 && opt==1)

    flag=1;

    switch(opt)

    {

    case1:

    init(head);

    break;

    case2:

    head=ins_bef(head);

    break;

    case3:

    ins_aft(head);

    break;

    case4:

    head=del(head);

    break;

    case5:

    search(head);

    break;

    case6:

    disp(head);

    break;

    }

    printf("\nDo you wish to continue[y/n]\n");

    ch=(char)getche();

    }while(ch=='Y' || ch=='y');

    printf("\nDone by \"SHABBIR\"\n");

    printf("\nPress any key to exit\n");

    getch();

    }

    /***** Initialisation function *****/

    void init(node *start)

    {

    printf("\nEnter Roll number\n");

    scanf("%d",&start->roll_no);

    printf("\nEnter the name\n");

    fflush(stdin);

    gets(start->name);

    http://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.html
  • 7/29/2019 circular linked list program example.docx

    3/22

    printf("\nEnter the marks\n");

    scanf("%f",&start->marks);

    start->next=start;

    }

    /***** Function for inserting before a particular node *****/

    node* ins_bef(node *start){

    int rno; /* Roll number for inserting a node*/

    node *newnode; /* New inputed node*/

    node *current; /* Node for travelling the linked list*/

    newnode=(node*)malloc(sizeof(node));

    current=start;

    printf("\nEnter the roll number before which you want to insert a node\n");

    scanf("%d",&rno);

    init(newnode);

    if(current->roll_no==rno)

    {

    newnode->next=start;

    while(current->next!=start)

    current=current->next;

    current->next=newnode;

    start=newnode;

    return(start);

    }

    while(current->next!=start)

    {

    if(current->next->roll_no==rno)

    {

    newnode->next=current->next;

    current->next=newnode;

    return(start);

    }

    current=current->next;

    }

    /*

    If the function does not return from any return statement.

    There is no match to insert before the input roll number.

    */

    printf("\nMatch not found\n");

    return(start);

    }

    /***** Function for inserting after a particular node *****/void ins_aft(node *start)

    {

    int rno; /* Roll number for inserting a node*/

    int flag=0;

    node *newnode; /* New inputed node*/

    node *current; /* Node for travelling the linked list*/

    newnode=(node*)malloc(sizeof(node));

    printf("\nEnter the roll number after which you want to insert a node\n");

    http://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.html
  • 7/29/2019 circular linked list program example.docx

    4/22

    scanf("%d",&rno);

    init(newnode);

    current=start;

    while(current->next!=start)

    {

    /*** Insertion checking for all nodes except last ***/

    if(current->roll_no==rno){

    newnode->next=current->next;

    current->next=newnode;

    flag=1;

    }

    current=current->next;

    }

    if(flag==0 && current->next==start && current->roll_no==rno)

    {

    /*** Insertion checking for last nodes ***/

    newnode->next=current->next; /** start is copied in newnode->next**/

    current->next=newnode;

    flag=1;

    }

    if(flag==0 && current->next==start)

    printf("\nNo match found\n");

    }

    /***** deletion function *****/

    node* del(node *start)

    {

    int rno; /* Roll number for deleting a node*/

    node *delnode; /* Node to be deleted */

    node *current; /* Node for travelling the linked list*/

    printf("\nEnter the roll number whose node you want to delete\n");

    scanf("%d",&rno);

    current=start;

    if(current->roll_no==rno)

    {

    /*** Checking condition for deletion of first node ***/

    delnode=current; /* Unnecessary step */

    while(current->next!=start)

    current=current->next;

    current->next=start->next;

    start=start->next;

    free(delnode);

    return(start);}

    else

    {

    while(current->next->next!=start)

    {

    /*** Checking condition for deletion of ***/

    /*** all nodes except first and last node ***/

    if(current->next->roll_no==rno)

    http://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.html
  • 7/29/2019 circular linked list program example.docx

    5/22

    {

    delnode=current->next;

    current->next=current->next->next;

    free(delnode);

    return(start);

    }

    current=current->next;}

    if(current->next->next==start && current->next->roll_no==rno)

    {

    /*** Checking condition for deletion of last node ***/

    delnode=current->next;

    free(delnode);

    current->next=start;

    return(start);

    }

    }

    printf("\nMatch not found\n");

    return(start);

    }

    void search(node *start)

    {

    int ch; /* Choice inputing variable */

    printf("\nEnter the criteria for search\n");

    printf("\n1. Roll number\n");

    printf("\n2. Name\n");

    printf("\n3. Marks\n");

    scanf("%d",&ch);

    switch(ch)

    {

    case1:

    rollsrch(start);

    break;

    case2:

    namesrch(start);

    break;

    case3:

    marksrch(start);

    break;

    default:

    rollsrch(start);

    }

    }

    /***** Search function searching the appropriate roll number *****/

    void rollsrch(node *start)

    {

    int rno; /* Roll number to be searched */

    node *current; /* Node for travelling the linked list*/

    current=start;

    printf("\nEnter the roll number to search\n");

    http://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.html
  • 7/29/2019 circular linked list program example.docx

    6/22

    scanf("%d",&rno);

    while(current->next!=start)

    {

    if(current->roll_no==rno)

    printf("\n %d %s %f\n",current->roll_no,current->name,current-

    >marks);

    current=current->next;}

    if(current->next==start && current->roll_no==rno)

    printf("\n %d %s %f\n",current->roll_no,current->name,current->marks);

    }

    /***** Search function searching the appropriate name *****/

    void namesrch(node *start)

    {

    char arr[20]; /* Name to be searched */

    node *current; /* Node for travelling the linked list*/

    current=start;

    printf("\nEnter the name to search\n");

    fflush(stdin);

    gets(arr);

    while(current->next!=start)

    {

    if(strcmp(current->name,arr)==NULL)

    printf("\n %d %s %f\n",current->roll_no,current->name,current-

    >marks);

    current=current->next;

    }

    if(current->next==start && strcmp(current->name,arr)==NULL)

    printf("\n %d %s %f\n",current->roll_no,current->name,current->marks);

    }

    /***** Search function searching the appropriate marks *****/

    void marksrch(node *start)

    {

    float marks; /* Marks to be searched */

    node *current; /* Node for travelling the linked list*/

    current=start;

    printf("\nEnter the marks to search\n");

    scanf("%f",&marks);

    while(current->next!=start)

    {

    if(current->marks==marks)

    printf("\n %d %s %f\n",current->roll_no,current->name,current->marks);

    current=current->next;

    }

    if(current->next==start && current->marks==marks)

    printf("\n %d %s %f\n",current->roll_no,current->name,current->marks);

    }

    /***** Output displaying function *****/

    http://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.html
  • 7/29/2019 circular linked list program example.docx

    7/22

    void disp(node *start)

    {

    node *current; /* Node for travelling the linked list*/

    current=start;

    while(current->next!=start)

    {

    printf("\n %d %s %f",current->roll_no,current->name,current->marks);current=current->next;

    }

    printf("\n %d %s %f",current->roll_no,current->name,current->marks);

    }

    http://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.html
  • 7/29/2019 circular linked list program example.docx

    8/22

    Single linked list

    #include#includevoid creation(int);

    void display();void insertion(int,int);void deletion(int);

    struct list{

    int num;struct list *next;

    }*head, *l;

    void main(){

    int n1,ch,pos,val;clrscr();printf("enter the no of nodes to be entered : ");scanf("%d",&n1);

    do{

    clrscr();printf("\n1.creation\n2.insertion\n3.deletion\n4.display\n5.exit\n");printf("\nenter ur choice : ");scanf("%d",&ch);switch(ch){

    case 1:creation(n1);

    break;case 2:

    printf("\n\nenter the postion in which to be inserted : ");scanf("%d",&pos);printf("\n\nenter the value : ");scanf("%d",&val);insertion(pos,val);break;

    case 3:printf("\n\nenter the position to be deleted : ");scanf("%d",&pos);deletion(pos);break;

    case 4:display();getch();break;

    case 5:exit(0);

    }}while(ch!=5);

    getch();

  • 7/29/2019 circular linked list program example.docx

    9/22

    }

    /* CREATION *//* -------- */

    void creation(int n1){

    int i,n;head=((struct list *)malloc(sizeof(struct list)));l=head;for(i=0;inum=n;l->next=((struct list *)malloc(sizeof(struct list)));l=l->next;

    }l->next=0;

    }

    /* DISPLAY *//* ------- */

    void display(){

    l=head;printf("\nthe nodes entered are : ");

    while(l->next>0)

    {printf("%d\t",l->num);l=l->next;

    }printf("null");

    }

    /* INSERTION *//* --------- */

    void insertion(pos,val){

    int i;struct list *x,*y;l=head;i=2;if(pos==1){x=((struct list *)malloc(sizeof(struct list)));x->num=val;x->next=l;head=x;

  • 7/29/2019 circular linked list program example.docx

    10/22

    }else{

    while(l->next>0){

    if(pos==i-1)

    {x=((struct list *)malloc(sizeof(struct list)));x->num=val;x->next=l;y->next=x;

    }y=l;l=l->next;i++;

    }}

    }

    /* DELETION */

    /* -------- */

    void deletion(pos){

    int i;struct list *y;l=head;i=1;if(pos==1){ head=l->next; }else{

    while(l->next>0){

    if(pos==i){ l=l->next;y->next=l;break;

    }y=l;l=l->next;i++;

    }}

    }

  • 7/29/2019 circular linked list program example.docx

    11/22

    1. //////////////////////////////////////////////////////////////

    2. ////////// -: Singly Link list :- /////////

    3. ////////////////////////////////////////////////////////////

    4.

    5. /* reference : Understandng pointer though c - Y karnetkar.

    6. */

    7.8. //////////////////////////////////////////////////////////

    9. ////////// Programmer : Harsh chandra ///////////

    10.////////////////////////////////////////////////////////

    11.

    12.# include

    13.# include

    14.# include

    15.# include

    16.struct node

    17.{int data;

    18.struct node *link;

    19.};

    20.voidappend(struct node **,int);

    21.voidin_begin(struct node **,int);

    22.voiddel(struct node **,int);

    23.voidin_middle(struct node **,int,int);

    24.int count(struct node *);

    25.voiddisplay(struct node *);

    26.

    27.voidmain()

    28.{struct node *p; /* p can be said as the head or a start ptr */

    29.p=NULL;

    30./* Printing the menu */

    31.int num,loc;

    32.char choice;

    33.do

    34.{ clrscr();

    35.printf("PROGRAM TO IMPLEMENT SINGLY LINKED LIST ");

    36.printf("\n=====================================" );

    37.printf("\n\n1.Create \\ Appending The List");

    38.printf("\n2.Insert Node At Begining");

    39.printf("\n3.Insert Node In Middle");

    40.printf("\n4.Deleting a Node");

    41.printf("\n5.Counting The No Of Nodes");

    42.printf("\n6.Displaying the list");

    43.printf("\n7.Exit");

    44.oper:

    45.gotoxy(1,15);printf(" ");

    46.gotoxy(1,11);printf("\n\nEnter ur Choice : ");

    47.choice=getch();

    48.switch(choice)

    49.{case'1':

    50.char ans;

    51.do

    52.{printf("Enter any number : ");

  • 7/29/2019 circular linked list program example.docx

    12/22

    53.scanf("%d",&num);

    54.append(&p,num);

    55.printf("Enter more (y/n) :");

    56.fflush(stdin);

    57.ans=getchar();

    58.}while(ans !='n');

    59.break;60.case'2':

    61.printf("Enter The Data : ");

    62.scanf("%d",&num);

    63.in_begin(&p,num);

    64.break;

    65.

    66.case'3':

    67.printf("\nEnter The Position :");

    68.scanf("%d",&loc);

    69.printf("\nEnter The Data : ");

    70.scanf("%d",&num);

    71.in_middle(&p,loc,num);

    72.break;

    73.

    74.case'4':

    75.printf("\nEnter The Data u Want To Delete : ");

    76.scanf("%d",&num);

    77.del(&p,num);

    78.break;

    79.

    80.case'5':

    81.printf("\nThe No Of Nodes Are %d",count(p));

    82.getch();

    83.break;

    84.

    85.case'6':

    86.display(p);

    87.getch();

    88.break;

    89.

    90.case'7':

    91.printf("\n\nQuiting.......");

    92.getch();

    93.exit(0);

    94.break;

    95.

    96.default:

    97.gotoxy(1,15);printf("Invalid choice.Please Enter Correct Choice");

    98.getch();

    99.goto oper;

    100.

    101. }

    102.

    103. }while(choice !=7);

    104.

  • 7/29/2019 circular linked list program example.docx

    13/22

    105. }

    106.

    107. voidappend(struct node **q,int num)

    108. {struct node *temp,*r;

    109. temp = *q;

    110. if(*q==NULL)

    111. { temp = (struct node *)malloc(sizeof(struct node));112. temp->data=num;

    113. temp->link=NULL;

    114. *q=temp;

    115. }

    116. else

    117. { temp = *q;

    118. while(temp->link !=NULL)

    119. { temp=temp->link;

    120. }

    121. r = (struct node *)malloc(sizeof(struct node));

    122. r->data=num;

    123. r->link=NULL;

    124. temp->link=r;

    125. }

    126. }

    127.

    128. voiddisplay(struct node *q)

    129. {if(q==NULL)

    130. {printf("\n\nEmpty Link List.Can't Display The Data");

    131. getch();

    132. goto last;

    133. }

    134. while(q!=NULL)

    135. {printf("\n%d",q->data);

    136. q=q->link;

    137. }

    138. last:

    139. }

    140.

    141. int count(struct node *q)

    142. {int c=0;

    143. if(q==NULL)

    144. {printf("Empty Link List.\n");

    145. getch();

    146. goto last;

    147. }

    148. while(q!=NULL)

    149. { c++;

    150. q=q->link;

    151. }

    152. last:

    153. return c;

    154.

    155. }

    156.

  • 7/29/2019 circular linked list program example.docx

    14/22

    157. voidin_begin(struct node **q,int num)

    158. {struct node *temp;

    159. if(*q==NULL)

    160. {printf("Link List Is Empty.Can't Insert.");

    161. getch();

    162. goto last;

    163. }164. else

    165. { temp=(struct node *)malloc(sizeof(struct node));

    166. temp->data=num;

    167. temp->link=*q;

    168. *q=temp; /* pointing to the first node */

    169. }

    170. last:

    171. getch();

    172. }

    173.

    174. voidin_middle(struct node **q,int loc,int num)

    175. {struct node *temp,*n;

    176. int c=1,flag=0;

    177. temp=*q;

    178. if(*q==NULL)

    179. {printf("\n\nLink List Is Empty.Can't Insert.");

    180. getch();

    181. goto last;

    182. }

    183. else

    184. while(temp!=NULL)

    185. {if(c==loc)

    186. { n = (struct node *)malloc(sizeof(struct node));

    187. n->data=num;

    188. n->link=temp->link;

    189. temp->link=n;

    190. flag=1;

    191. }

    192. c++;

    193. temp=temp->link;

    194. }

    195. if(flag==0)

    196. {printf("\n\nNode Specified Doesn't Exist.Cant Enter The Data");

    197. getch();

    198. }

    199. else

    200. {printf("Data Inserted");

    201. getch();

    202. }

    203. last:

    204. getch();

    205. }

    206.

    207. voiddel(struct node**q,int num)

    208. {if(*q==NULL)

  • 7/29/2019 circular linked list program example.docx

    15/22

    209. {printf("\n\nEmpty Linked List.Cant Delete The Data.");

    210. getch();

    211. goto last;

    212. }

    213. else

    214. {

    215. struct node *old,*temp;216. int flag=0;

    217. temp=*q;

    218. while(temp!=NULL)

    219. {if(temp->data==num)

    220. {if(temp==*q)/* First Node case */

    221. *q=temp->link; /* shifted the header node */

    222. else

    223. old->link=temp->link;

    224.

    225. free(temp);

    226. flag=1;

    227. }

    228. else

    229. { old=temp;

    230. temp=temp->link;

    231. }

    232. }

    233. if(flag==0)

    234. printf("\nData Not Found...");

    235. else

    236. printf("\nData Deleted...Tap a key to continue");

    237. getch();

    238. }

    239. last:

    240. getch();

    241. }

  • 7/29/2019 circular linked list program example.docx

    16/22

    Singly linked list

    A Linked list is a chain of structs or records called Nodes. Each node has at least two members, one of which

    points to the next Node in the list and the other holds the data. These are defined as Single Linked Lists

    because they can only point to the next Node in the list but not to the previous.

    view plainprint?

    1. struct Node2. {3. int Data;4. struct Node *Next;5. }*Head;

    We use above structure for a Node in our example. Variable Data holds the data in the Node while pointer of

    type struct Node Next holds the address to the next Node in the list. Head is a pointer of type struct Node

    which acts as the Head to the list.

    Initially we set 'Head' as NULL which means list is empty.

    view plainprint?

    1. //Set HEAD as NULL2. Head=NULL;

    Let us see Insertion Functions .. addBeg, addEnd, addAt.

    view plainprint?

    1. // Adding a Node at the Beginning of the List2.3. void addBeg(int num)

    4. {5. struct Node *temp;6.7. temp=(struct Node *)malloc(sizeof(struct Node));8. temp->Data = num;9.10. if (Head == NULL)11. {12. //List is Empty13. Head=temp;14. Head->Next=NULL;15. }16. else17. {18. temp->Next=Head;19. Head=temp;

    20. }21. }

    http://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.html
  • 7/29/2019 circular linked list program example.docx

    17/22

    We create 'temp' node and save the Data part.

    IfHead is NULL it means list is empty. So we set temp node as the Head of the list and set theNext as NULL.

    Else we set the Next in temp node as Head and reassign Head with temp.

    view plainprint?

    1. //Adding a Node at the end of the list2.3. void addEnd(int num)4. {5. struct Node *temp1, *temp2;6.7. temp1=(struct Node *)malloc(sizeof(struct Node));8. temp1->Data=num;9.10. // Copying the Head location into another node.11. temp2=Head;12.13. if(Head == NULL)14. {15. // If List is empty we create First Node.16. Head=temp1;17. Head->Next=NULL;18. }19. else20. {21. // Traverse down to end of the list.22. while(temp2->Next != NULL)23. temp2=temp2->Next;24.25. // Append at the end of the list.26. temp1->Next=NULL;27. temp2->Next=temp1;28. }29. }

    http://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://3.bp.blogspot.com/_MQa_rKnACRw/S9NRS5daK_I/AAAAAAAAB44/F2Vp5GR0Tho/s1600/01.jpghttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.html
  • 7/29/2019 circular linked list program example.docx

    18/22

    view plainprint?

    1. // Adding a new Node at specified position

    2.3. void addAt(int num, int loc)4. {5. int i;6. struct Node *temp, *prev_ptr, *cur_ptr;7.8. cur_ptr=Head;9.10. if(loc > (length()+1) || loc Data=num;31.32. prev_ptr->Next=temp;33. temp->Next=cur_ptr;

    34. }35. }36. }37.38. // Counting number of elements in the List39.40. int length()41. {42. struct Node *cur_ptr;43. int count=0;44.45. cur_ptr=Head;

    http://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://4.bp.blogspot.com/_MQa_rKnACRw/S9NYLGRW6jI/AAAAAAAAB5A/hySIJz0v1ZQ/s1600/02.jpghttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.html
  • 7/29/2019 circular linked list program example.docx

    19/22

    46.47. while(cur_ptr != NULL)48. {49. cur_ptr=cur_ptr->Next;50. count++;51. }52. return(count);53. }

    Now we will see how to delete a node from the list depending upon the data in the Node.

    view plainprint?

    1. // Deleting a node from List depending upon the data in the node.2.3. int delNodeData(int num)4. {5. struct Node *prev_ptr, *cur_ptr;

    6.7. cur_ptr=Head;8.9. while(cur_ptr != NULL)10. {11. if(cur_ptr->Data == num)12. {13. if(cur_ptr==Head)14. {15. Head=cur_ptr->Next;16. free(cur_ptr);17. return 0;18. }19. else20. {21. prev_ptr->Next=cur_ptr->Next;

    22. free(cur_ptr);23. return 0;24. }25. }26. else27. {28. prev_ptr=cur_ptr;29. cur_ptr=cur_ptr->Next;30. }31. }32.33. printf("\nElement %d is not found in the List", num);

    http://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://1.bp.blogspot.com/_MQa_rKnACRw/S9NcDunFmFI/AAAAAAAAB5I/NM5Czfo1ZbE/s1600/03.jpghttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.html
  • 7/29/2019 circular linked list program example.docx

    20/22

    34. return 1;35. }

    If the value to be deleted is the head of the list.

    If the value to be deleted is not the head of the list.

    Similarly if we want to delete a node based on its location in the list.

    view plainprint?

    1. // Deleting a node from List depending upon the location in the list.2.3. int delNodeLoc(int loc)4. {5. struct Node *prev_ptr, *cur_ptr;6. int i;7.8. cur_ptr=Head;9.10. if(loc > (length()) || loc Next;20. free(cur_ptr);21. return 0;22. }23. else24. {25. for(i=1;i

  • 7/29/2019 circular linked list program example.docx

    21/22

    27. prev_ptr=cur_ptr;28. cur_ptr=cur_ptr->Next;29. }30.31. prev_ptr->Next=cur_ptr->Next;32. free(cur_ptr);33. }34. }

    35. return 1;36. }

    Displaying a list.

    view plainprint?

    1. // Displaying list contents2.3. void display()4. {5. struct Node *cur_ptr;6.7. cur_ptr=Head;8.

    9. if(cur_ptr==NULL)10. {11. printf("\nList is Empty");12. }13. else14. {15. printf("Elements in the List: ");16. //traverse the entire linked list17. while(cur_ptr!=NULL)18. {19. printf(" -> %d ",cur_ptr->Data);20. cur_ptr=cur_ptr->Next;21. }22. printf("\n");23. }24. }

    Reversing a list.

    view plainprint?

    1. //Reversesing a Linked List2.3. void reverse()4. {5. struct Node *prev_ptr, *cur_ptr, *temp;6.7. cur_ptr=Head;8. prev_ptr=NULL;9.10. while(cur_ptr != NULL)11. {12. temp=prev_ptr;13. prev_ptr=cur_ptr;14.15. cur_ptr=cur_ptr->Next;16. prev_ptr->Next=temp;17. }18.19. Head=prev_ptr;20. }

    http://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.htmlhttp://codingfreak.blogspot.com/2009/08/implementation-of-singly-linked-list-in.html
  • 7/29/2019 circular linked list program example.docx

    22/22