ece+lab+manual (1)

Upload: allen-sain

Post on 05-Apr-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Ece+Lab+Manual (1)

    1/39

    ARRAY IMPLEMENTATION OF LIST ADT

    Aim:

    Alg:

    PROGRAM:

    #include#include

    #include

    void create();

    void insert();void deletion();

    void search();

    void display();

    int a,b[20],n,d,e,f,i;void main()

    {int c;

    char g='y';

    clrscr();

    do{

    cout

  • 7/31/2019 Ece+Lab+Manual (1)

    2/39

    void create()

    {coutn;

    for(i=0;i>b[i];

    }}

    void deletion(){

    coutd;

    for(i=0;i

  • 7/31/2019 Ece+Lab+Manual (1)

    3/39

    Implementation Of Single Linked List

    READ THIS: Write aim,algorithm,program in obser. Other details for your ref

    Definition:

    In computer science, a linked list is a data structure that consists of a sequence of data records such

    that in each record there is a field that contains a reference (i.e., a link) to the next record in the

    sequence.

    (or)

    Linked list consists of series of nodes.Each node contains the element and a pointer to its successor

    node. The pointer of the last node points to NULL. Each record of a linked list is often called an

    element or node. The remaining fields may be called the data, information, value, or payload fields.

    The field of each node that contains address of the next node is usually called the next link or next

    pointer.

    Types of Linked list:

    1. Singly linked list.

    2. Doubly linked list.

    3. Circular linked list.

    Advantages:

    1. Length of the list need not be known in advance

    2. Optimum use of memory

    3. Insertion of element into the list is easier

    4. Deletion of element from the list is easier

    5. Concatenation of two linked lists is easier

    6. Memory is not wasted if you remove an element from the list as it is freed

    Singly linked list:

    The simplest kind of linked list is a singly-linked list, which has one link per node. This link

    points to the next node in the list, or to a null value or empty list if it is the final node.

    A singly linked list's node is divided into two parts. The first part holds or points to

    information about the node, and second part holds the address of next node. A singly linked list travelsone way.

    Operations on singly linked list:

  • 7/31/2019 Ece+Lab+Manual (1)

    4/39

    1. Insert the element in the list.2. Delete the element in the list3. Forward traverse.

    Advantages of singly linked list:

    1. Dynamic data structure.2. We can perform deletion and insertion any where in the list.3. We can merge two list easily.

    Disadvantages of singly linked list:

    1. Backward traversing is not possible in singly linked list.2. Insertion is easy but deletion take some additional time, because disadvantage of backward

    traversing.

    Algorithm steps:

    Step1: Create nodes first,last,next,prev and cur then set the value as NULL.

    Step 2: Read the list operation type.

    step 3: If operation type is create then process the following steps.

    1. Allocate memory for node cur.

    2. Read data in cur's data area.

    3. Assign cur link as NULL.

    4. Assign first=last=cur.

    Step 4: If operation type is Insert then process the following steps.

    1. Allocate memory for node cur.

    2. Read data in cur's data area.

    3. Read the position the Data to be insert.

    4. Availability of the position is true then assing cur's link as first and first=cur.

    5. If availability of position is false then do following steps.

  • 7/31/2019 Ece+Lab+Manual (1)

    5/39

    1. Assign next as cur and count as zero.

    2. Repeat the following steps until count less than postion.

    1 .Assign prev as next

    2. Next as prev of link.

    3. Add count by one.

    4. If prev as NULL then display the message INVALID POSITION.

    5. If prev not qual to NULL then do the following steps.

    1. Assign cur's link as prev's link.

    2. Assign prev's link as cur.

    Step5: If operation type is delete then do the following steps.

    1. Read the position .

    2. Check list is Empty .If it is true display the message List empty.

    3. If position is first.

    1. Assign cur as first.

    2. Assign First as first of link.

    3. Reallocate the cur from memory.

    4. If position is last.

    1. Move the current node to prev.

    2. cur's link as Null.

    3. Reallocate the Last from memory.

    4. Assign last as cur.

    5. If position is enter Mediate.

    1. Move the cur to required postion.

    2. Move the Previous to cur's previous position

    3. Move the Next to cur's Next position.

    4. Now Assign previous of link as next.

    5. Reallocate the cur from memory.

  • 7/31/2019 Ece+Lab+Manual (1)

    6/39

    step 6: If operation is traverse.1. Assign current as first.

    2. Repeat the following steps untill cur becomes NULL.

    PROGRAM:

    /* single linked list */#include

    #include

    #includeclass list

    { struct node{

    int data;

    node *link;

    }*p;public:

    void inslast(int);

    void insbeg(int);void insnext(int,int);

    void delelement(int);

    void delbeg();void dellast();

    void disp();

    int seek(int);list(){p=NULL;}

    ~list();

    };

    void list::inslast(int x)

    {node *q,*t;

    if(p==NULL){

    p=new node;

    p->data=x;p->link=NULL;

    }

    else

    {q=p;

  • 7/31/2019 Ece+Lab+Manual (1)

    7/39

    while(q->link!=NULL)

    q=q->link;t=new node;

    t->data=x;

    t->link=NULL;

    q->link=t;}

    coutlink=q;

    coutlink;delete q;

    return;

    }

    r=q;while(q!=NULL)

    {

    if(q->data==x){

    r->link=q->link;

    delete q;return;

    }

    r=q;

    q=q->link;}

    cout

  • 7/31/2019 Ece+Lab+Manual (1)

    8/39

  • 7/31/2019 Ece+Lab+Manual (1)

    9/39

    if(p==NULL) return;

    while(p!=NULL){

    q=p->link;

    delete p;

    p=q;}

    }

    void list::disp()

    {

    node *q;

    q=p;if(q==NULL)

    {

    coutlink=temp1;

  • 7/31/2019 Ece+Lab+Manual (1)

    10/39

    }

    temp=temp->link;}

    cout

  • 7/31/2019 Ece+Lab+Manual (1)

    11/39

    cout

  • 7/31/2019 Ece+Lab+Manual (1)

    12/39

    cin>>v;

    l.delelement(v);cout

  • 7/31/2019 Ece+Lab+Manual (1)

    13/39

    SAMPLE INPUT AND OUTPUT:

    SINGLY LINKED LIST

    1.CREATE

    2.INSERT3.DELETE4.EXIT

    ENTER YOUR CHOICE : 1

    ENTER THE DATA: 10

    10

    1.CREATE

    2.INSERT3.DELETE

    4.EXIT

    ENTER YOUR CHOICE : 2

    ENTER THE DATA: 30

    ENTER THE POSITION: 1

    30

    10

    1.CREATE

    2.INSERT

    3.DELETE

    4.EXIT

    ENTER YOUR CHOICE : 3

    ENTER THE POSITION : 2

    LIST IS EMPTY

  • 7/31/2019 Ece+Lab+Manual (1)

    14/39

    READ THIS: Write aim,algorithm,program in obser. Other details for your ref

    Implementation Of Double Linked ListDefinition:

    A variant of a linked list in which each item has a link to the previous item as well as the next. This

    allows easily accessing list items backward as well as forward and deleting any item in constant time.

    Also known as two-way linked list, symmetrically linked list.

    A doubly linked list is a linked list in which each node has three fields namely data field, forward link(Flink) and backward link (Blink). Flink points to the successor node in the list whereas Blink points

    to the predecessor node.

    Advantages:

    1. Deletion process is easier2.

    Additional pointer for previous node is not nessary.

    3. Backwarding and forwarding traverse are possible.Disadvantages :

    1. More Memory space is required since it has two pointers.Alogrithm steps:

    Step 1: Read the list operation.

    Step 2: If operation is Create then process the following steps.

    1. Create the new node and allocate memory for the new node.

    2. Read the data in the new node.

    3. Assign Flink and Blink as NULL.

    4. Assign current as new.

    Step 3: If operation is Insertion do the following steps.

    i) Check insertion at beginning is true then

    1. Create the new node and allocate memory for the new node

    2. Read the data in the new node.

    3. Move the current node to start position

  • 7/31/2019 Ece+Lab+Manual (1)

    15/39

    4. Assign new->Blink =Null

    5. Assign new->Flink=current

    6. Now assign current->Blink=new

    ii) Insertion at between nodes is true then

    1. Create the new node and allocate memory for the new node

    2. Read the data in the new node.

    3. Move the current node required position

    4. Assign new nodes Blink=current.

    5. Assign new nodes Flink=current->Flink

    6. Assign current nodes Flink =new

    iii) Insertion at between nodes is true then

    1. Create the new node and allocate memory for the new node.

    2. Read the data in the new node.

    3. Move the current node to end position.

    4. Assign current nodes Flink =new.

    5. Assign news Blink as current.

    6. Assign news Flink as NULL.

    7. Move end pointer to new node.

    Step 4: If operation is deletion then do the following steps.

    i). Check deletion at beginning is true then.

    1. Move current pointer to start position.

    2. Move the start pointer next node in the link.

    3. Assign starts Blink as NULL.

    4. Reallocate current node from memory.

  • 7/31/2019 Ece+Lab+Manual (1)

    16/39

    ii) Check deletion between two nodes is true

    1. Move the current pointer to required position.

    2. Assign currents->Blink->Flink as currents Flink.

    3. Assign currentss->Flink->Blink as currents Blink.

    4. Reallocate current from memory.

    iii) Check deletion at end is true

    1. Move the current pointer to end position.

    2. Assign currents->Blink->Flink as Null.

    3. Reallocate current from memory.

    4. Reallocate current from memory.

    Step 5: If the operation is traversing.

    i) Check deletion at end is true

    1. Move the current pointer to end position.

    2. Repeat the following steps until current becomes NULL.

    3. Display the data.

    4. Current=current->Flink.

    ii) If Backward traversing then

    1. Move the current to end position.

    2. Repeat the following steps until current becomes NULL.

    3. Display the data.

    4. Current=current->Flink.

  • 7/31/2019 Ece+Lab+Manual (1)

    17/39

    PROGRAM:

    #include

    #include

    #include

    #define NULL 0

    struct list

    {

    int data;

    struct list*next;

    struct list *prev;

    };

    typedef struct list node;

    node *find(node *,int);

    node *pre=NULL;

    node *fin=NULL;

    node *start;

    int main()

    {

    int menu(void);

    void create(node *);

    void display(node *);

    void insert(node *,int,int);

  • 7/31/2019 Ece+Lab+Manual (1)

    18/39

    void reverse(node *);

    void del(node *,int);

    int choice,ch;

    int data,tar;

    node *newrec;

    start=NULL;

    clrscr();

    do

    {

    choice=menu();

    switch(choice)

    {

    case 1:

    cout

  • 7/31/2019 Ece+Lab+Manual (1)

    19/39

    display(start);

    }

    getch();

    break;

    case 3:

    if (start==NULL)

    {

    cout

  • 7/31/2019 Ece+Lab+Manual (1)

    20/39

    coutdata;

    couttar;

    insert(start,data,tar);

    }

    break;

    case 5:

    if(start==NULL)

    {

    cout

  • 7/31/2019 Ece+Lab+Manual (1)

    21/39

  • 7/31/2019 Ece+Lab+Manual (1)

    22/39

    pre=NULL;

    }

    else

    {

    record->prev=pre;

    record->next=new node;

    pre=record;

    create(record->next);

    }

    return;

    }

    void display(node *record)

    {

    if(record->next!=NULL)

    {

    cout

  • 7/31/2019 Ece+Lab+Manual (1)

    23/39

    coutnext=record;

    newrec->prev=NULL;

    record->prev=newrec;

    start=newrec;

    }

    else

    {

    tag=find(record,target);

    temp=tag->prev;

    tag->prev=newrec;

    newrec->next=tag;

    temp->next=newrec;

    }

  • 7/31/2019 Ece+Lab+Manual (1)

    24/39

    if(tag==NULL)

    {

    coutdata==target)

    {

    temp=record;

    start=start->next;

    start->prev=NULL;

    delete temp;

    }

    else

    {

    tag=find(record,target);

    if(tag->next->next==NULL)

    {

  • 7/31/2019 Ece+Lab+Manual (1)

    25/39

    fin=fin->prev;

    fin->next=tag->next;

    }

    if(tag==NULL)

    {

    coutdata==target)

    {

    return(record);

    }

    else if(record->next==NULL)

    return(NULL);

    else

    find(record->next,target);

    }

  • 7/31/2019 Ece+Lab+Manual (1)

    26/39

    Sample Input Output:

    Main menu

    1.Create

    2.Insert3.Delete

    4.Display

    5.Exit

    Enter your choice:1

    Enter the data(-999 to stop):

    510

    15

    -999

    Main menu

    1.Create

    2.Insert

    3.Delete4.Display

    5.Exit

    Enter your choice:2

    New data item:20

    Position of insertion:

    Main menu

    1.Create

    2.Insert3.Delete

    4.Display

    5.ExitEnter your choice:4

    Forward traversal5 20 10 15 20

  • 7/31/2019 Ece+Lab+Manual (1)

    27/39

    Backward traversal

    20 15 10 20 5

    Main menu

    1.Create2.Insert3.Delete

    4.Display

    5.Exit

    Enter your choice:3

    Enter the data to be deleted:5

    Main menu

    1.Create2.Insert

    3.Delete4.Display

    5.Exit

    Enter your choice:4

    Forward traversal

    20 10 15 20

    Backward traversal

    20 15 10 20

  • 7/31/2019 Ece+Lab+Manual (1)

    28/39

    NO NEED TO wrire this program..just take print out

    Implementation Of Circular Linked List

    Circular Linked List:

    In circular linked list the pointer of the last node points to the first node.

    It can be implemented as singly linked list and doubly linked list.

    Advantages of Circular Linked List:

    1. It allows traversing the list starting at any point.

    2. It allows quick access to the first and last records.

    3. Circularly doubly linked list allows traversing the list in either direction.

    Algorithm Steps:

    Step 1: Create the pointers prev, cur ,last.

    Step 2: Read the operation type of the list.

    Step 3: If the operation type is Insertion then

    i) If insertion at beginning is true

    1. Create the new node and allocate memory for that node.2. Read the data in new nodes data part.3. Assign cur->link = last->link (now cur->points first node)4. Assign first=cur (move the first to cur)5. Assign last->link =first (now last -> link points new first

    node)

    ii) If insertion at between any two nodes is true

    1. Move the prev pointer to required position2. Create the new node and allocate memory for that node.3. Read the data in new nodes data part.4. Assign cur->link = prev-link (give the link to next node)5. Assign prev->link =cur.

    iii) If insertion at end is true

  • 7/31/2019 Ece+Lab+Manual (1)

    29/39

    1. Move the prev is to last position ( ).2. Create the new node and allocate memory for that node.3. Read the data in new nodes data part.4. Assign cur->link =prev->link5. Assing prev=cur.

    Step 4: If the operation type is deletion then

    i) If deletion at beginning is true then

    1. Assign first=last->link->link (Move the first to secondposition)

    2. Assign cur=last->link3. Assign last->link=first4. Reallocate the cur from memory.

    ii) If deletion between any two nodes is true

    1. Move the cur to required position.2. Move the prev to cur predecessors position.3. Assign prev->link=cur->link4. Reallocate cur from memory.

    iii). If deletion at ends is true

    1. Move the cur to last position.2.

    Move the prev to curs predecessor position3. Assign prev->link=cur->link

    4. Reallocate cur from memoryStep 5: If operation type is traverses

    1. Assign cur=first2. Repeat the process untill cur becomes last Cur=cur->link

  • 7/31/2019 Ece+Lab+Manual (1)

    30/39

    PROGRAM:

    //Circular Linked List in CPP

    #include

    #include

    #include

    #include

    #define null 0

    struct node

    {

    int info;

    struct node *link;

    public:

    }*start,*last;

    void main()

    {

    int ch,n,m,position,i;

    void create(int);

    void addat(int);

    void addbt(int,int);

    void del(int);

    void disp();

    last=null;

    clrscr();

    while(1)

  • 7/31/2019 Ece+Lab+Manual (1)

    31/39

    {

    cout

  • 7/31/2019 Ece+Lab+Manual (1)

    32/39

    cin>>position;

    addbt(m,position);

    break;

    case 4:

    if(last==null)

    {

    cout

  • 7/31/2019 Ece+Lab+Manual (1)

    33/39

    {

    struct node *q,*tmp;

    tmp=(struct node *)malloc(sizeof(struct node));

    tmp->info=data;

    tmp->link=null;

    if(last==null)

    {

    last=tmp;

    tmp->link=last;

    }

    else

    {

    tmp->link=last->link;

    last->link=tmp;

    last=tmp;

    }

    return;

    }

    void addat(int data)

    {

    struct node *q,*tmp;

    tmp=(struct node *)malloc(sizeof(struct node));

  • 7/31/2019 Ece+Lab+Manual (1)

    34/39

    tmp->info=data;

    tmp->link=last->link;

    last->link=tmp;

    }

    void addbt(int data,int pos)

    {

    struct node *tmp,*q;

    int i;

    q=last->link;;

    for(i=0;ilink;

    if(q==last->link)

    {

    cout

  • 7/31/2019 Ece+Lab+Manual (1)

    35/39

    void del(int data)

    {

    struct node *tmp,*q;

    if(last->link==last&&last->info==data)

    {

    tmp=last;

    last=null;

    free(tmp);

    cout

  • 7/31/2019 Ece+Lab+Manual (1)

    36/39

    q->link=tmp->link;

    free(tmp);

    coutlink;

    free(tmp);

    cout

  • 7/31/2019 Ece+Lab+Manual (1)

    37/39

    {

    struct node *q;

    if(last==null)

    {

    cout

  • 7/31/2019 Ece+Lab+Manual (1)

    38/39

    30

    -999Circular list

    10 20 30

    MIAN NENU

    1.CREATE

    2.INSERT

    3.DELETE4.EXIT

    Enter your choice:2

    Enter the new item:40

    Position of insertion:2

    Circular list10 40 20 30

    MIAN NENU

    1.CREATE

    2.INSERT3.DELETE

    4.EXIT

    Enter your choice:3

    Data to be deleted:20

    Circular List

    10 40 30

    MIAN NENU

    1.CREATE2.INSERT

    3.DELETE4.EXITEnter your choice:3

    Data to be deleted:60

    Element not found

  • 7/31/2019 Ece+Lab+Manual (1)

    39/39