c program day-16

Upload: eshamu

Post on 04-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 C Program Day-16

    1/18

  • 7/31/2019 C Program Day-16

    2/18

    Circular Linked List : (CLL)

    The Last node in the list points to the first node. (ie.,) The last

    node pointer field contains the address of the first node.

    Doubly Linked List : (DLL)

    Each node of DLL consists of two pointers, which holds the

    address of the previous node and next node. Therefore, it enables bi-

    directional traversing. (ie.,) it traverse both forward and backward.

    The pointer points to the previous node are called Left link

    (llink) and pointer points to the next node are called right link (rlink)

  • 7/31/2019 C Program Day-16

    3/18

    //DOUBLY LINKED LIST EXAMPLE#include#includestruct emp{int eno;

    char ename[25];int sal;struct emp *next;struct emp *prev;};struct emp *new1=NULL,*start=NULL,*temp=NULL,*last=NULL,*insptr=NULL,*dptr;

    void accept(){new1=(struct emp*) malloc(sizeof(struct emp));printf("\n Enter the Employee Number");scanf("%d",&new1->eno);

    printf("\n Enter the Employee Name");

    scanf("%s",&new1->ename);

    printf("\n Enter the Employee Salary");scanf("%d",&new1->sal);}

    Continued,

  • 7/31/2019 C Program Day-16

    4/18

    void insert()

    {

    insptr=(struct emp*) malloc(sizeof(struct emp));

    printf("\n Enter the Employee Number");

    scanf("%d",&insptr->eno);

    printf("\n Enter the Employee Name");

    scanf("%s",&insptr->ename);

    printf("\n Enter the Employee Salary");scanf("%d",&insptr->sal);

    }

    void main()

    {

    int recno;char ch='y';

    clrscr();

    while(ch=='y')

    {

    Continued,

  • 7/31/2019 C Program Day-16

    5/18

    accept();

    fflush(stdin);

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

    scanf("%c",&ch);

    if(start==NULL)

    {

    start=new1;

    temp=new1;

    new1->prev=NULL;

    new1->next=NULL;

    }else

    {

    temp->next=new1;

    new1->prev=temp;

    new1->next=NULL;temp=new1;

    }

    }

    Continued,

  • 7/31/2019 C Program Day-16

    6/18

    last=new1;

    printf("\n Printing the datas in the Stack Form");

    for(new1=last;new1!=NULL;new1=new1->prev)

    {

    printf("%d %s %d\n",new1->eno,new1->ename,new1->sal);

    }

    printf("\n Printing the datas in the Queue Form");

    for(new1=start;new1!=NULL;new1=new1->next)

    {

    printf("%d %s %d\n",new1->eno,new1->ename,new1->sal);

    }

    // Insert the record

    printf("\n Enter the record number to be inserted");

    scanf("%d",&recno);insert();

    for (new1=start;new1!=NULL;new1=new1->next)

    {

    if (start->eno==recno)

    Continued,

  • 7/31/2019 C Program Day-16

    7/18

    {

    insptr->next=new1;

    insptr->prev=NULL;

    new1->prev=insptr;

    start=insptr;

    }

    else if(new1->eno==recno&&new1->next!=NULL)

    {

    temp->next=insptr;

    insptr->next=new1;

    new1->prev=insptr;insptr->prev=temp;

    }

    else if(new1->eno==recno&&new1->next==NULL)

    {

    new1->next=insptr;insptr->next=NULL;

    insptr->prev=new1;

    last=insptr;

    }

    temp=new1;} Continued,

  • 7/31/2019 C Program Day-16

    8/18

    printf("\n Printing the datas in the After Insertion Stack Form");

    for(new1=last;new1!=NULL;new1=new1->prev)

    {

    printf("%d %s %d\n",new1->eno,new1->ename,new1->sal);

    }

    printf("\n Printing the datas in the Queue Form");

    for(new1=start;new1!=NULL;new1=new1->next)

    {

    printf("%d %s %d\n",new1->eno,new1->ename,new1->sal);

    }

    // Deletion

    printf("\n Enter the record number to be Deleted");

    scanf("%d",&recno);

    for (new1=start;new1!=NULL;new1=new1->next){

    if (start->eno==recno)

    {

    Continued,

  • 7/31/2019 C Program Day-16

    9/18

    start=start->next;

    start->prev=NULL;

    dptr=NULL;

    }

    else if(new1->eno==recno && new1->next!=NULL){

    dptr=new1;

    temp->next=new1->next;

    new1->next->prev=temp;

    free(dptr);

    dptr=NULL;

    }

    else if(new1->eno==recno&&new1->next==NULL)

    {

    dptr=new1;

    temp->next=NULL;last=temp;

    free(dptr);

    }

    temp=new1;

    }Continued,

  • 7/31/2019 C Program Day-16

    10/18

    printf("\n Printing the datas in the After Insertion Stack Form");

    for(new1=last;new1!=NULL;new1=new1->prev)

    {

    printf("%d %s %d\n",new1->eno,new1->ename,new1->sal);}

    printf("\n Printing the datas in the Queue Form");

    for(new1=start;new1!=NULL;new1=new1->next){

    printf("%d %s %d\n",new1->eno,new1->ename,new1->sal);

    }

    getch();}

  • 7/31/2019 C Program Day-16

    11/18

    // Circular linked list using LIFO(stack) method

    #include

    #include

    #include

    #include#include

    typedef struct stack

    {

    int data;

    struct stack *next;}node;

    struct node *head,*top;

    void main()

    {

    int data,item,choice;

    char ans,ch;

    void push(int,node **);

    void display(node **);

    int pop(node **);

    Continued,

  • 7/31/2019 C Program Day-16

    12/18

    int sempty(node *);

    clrscr();

    top=NULL;

    printf("\n\n\t Stack using Linked List");

    do

    {

    printf("\n \n The main menu ");

    printf("\n 1.Push\n 2.Pop \n 3.Display \n 4.Exit");

    printf("\n Enter your choice");

    scanf("%d",&choice);

    switch(choice)

    {

    case 1:

    printf("\n Enter the data");

    scanf("%d",&data);

    push(data,&top);

    break;

    case 2:

    if(sempty(top))

    printf("\n stack underflow");

    Continued,

  • 7/31/2019 C Program Day-16

    13/18

    else

    {

    item=pop(&top);

    printf("\n The popped node is %d",item);

    }

    break;case 3:

    Display(&top);

    break;

    case 4:

    printf("\n Do U want to quit?[Y/n]");

    ch=getche();if(ch=='y')

    exit(0);

    else

    break;

    }

    printf("\n Do U want to continue?");ans=getche();

    getch();

    clrscr();

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

    getch();

    }Continued,

  • 7/31/2019 C Program Day-16

    14/18

    // Push the data items

    void push(int item,node **top)

    {

    node *new,*next=NULL;

    node *get_node(int);

    new=get_node(item);

    if(*top ==NULL)

    head=new; // marking Ist node in the stack

    new->next=*top;

    *top=new;

    head->next=*top;}

    node *get_node(int item)

    {

    node *temp;

    temp=(node *) malloc(sizeof(node));if(temp==NULL)

    printf("\n Memory cannot be allocated");

    temp->data=item;

    temp->next=NULL;

    return(temp);} Continued,

  • 7/31/2019 C Program Day-16

    15/18

    int sempty(node *temp)

    {

    if(temp==NULL)

    return 1;

    else

    return 0;

    }

    int pop(node **top)

    {

    int item;node *temp;

    item=(*top)->data;

    temp=*top;

    if(temp->next==*top)

    *top=NULL;*top=(*top)->next;

    head->next=*top;

    free(temp);

    return(item);

    }Continued,

  • 7/31/2019 C Program Day-16

    16/18

    void display(node **top)

    {

    node *temp;

    temp=*top;

    if(sempty(temp))printf("\n Stack is empty");

    else

    {

    while(temp->next != *top);

    {printf("%d\n",temp->data);

    temp=temp->next;

    }

    printf("%d\n",temp->data);

    }

    getch();

    }

  • 7/31/2019 C Program Day-16

    17/18

    Session Summary

    The logical organisation of data for easier manipulation is called as a datastructure.

    The data structure formed when the number of data items are known in advance is

    referred as Static Data structure.

    Dynamic data structure can grow or shrink during their execution time.

    Some of the Dynamic data structures includes linked lists, stacks, queues and Binarytrees NULL address is the address stored by the Null pointer in the last node of the list

    A single linked list is a list which each node as a single link to its next node.

    A double linked list is a list in which each node as two links one its next node and the

    other to its previous node.

    A circlular linked list is a list in which each node is connected to the next node as in the

    case of a singular list except that the list has no end.(.: The last node is connected to the

    first node of the list)

  • 7/31/2019 C Program Day-16

    18/18