Tuesday, July 19, 2011

Program for linear Array list {create,insert,delete,traverse}

/*Serial No.136     [swami117.cpp]*/

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

typedef struct list
    {
    int data[10];
    int length;
    }list;
list l;
void main()
{
int ch;
void create_list(list *);
void traverse_list(list *);
void insert_list(list *);
void delete_list(list *);
    clrscr();
    printf("\n\t[1]: Create List.\n");
    printf("\n\t[2]: Insert into List.\n");
    printf("\n\t[3]: Delete from List.\n");
    printf("\n\t[4]: Traverse List.\n");
    printf("\n\t[5]: Exit.\n");
    printf("\n\t!!!without creating list you can't use other operations.!!!\n");

do
    {
    printf("\n\t\t Enter your choice= ");
    scanf("%d",&ch);

    switch(ch)
        {
        case 1: create_list(&l);
            break;
        case 2: insert_list(&l);
            break;
        case 3: delete_list(&l);
            break;
        case 4: traverse_list(&l);
            break;
        case 5: exit(0);

        }
    }while(ch!=5);
getch();
}

void create_list(list *start)
    {
    int ele=0,i=-1;
    printf("Enter elements in the list.\n\t!**enter -1111 to exit the entry process**!\n");
    while(ele!=-1111)
        {
        i++;
        scanf("%d",&ele);
        start->data[i]=ele;
        }
    start->length=i-1;
    }

void traverse_list(list *start)
    {
    printf("\nElements in the linear array are:\n");
    for(int i=0;i<=start->length;i++)
        {
        printf("%d, ",start->data[i]);
        }
    }

void insert_list(list *start)
    {
    int temp=start->length,ele,position;
    printf("\n\nEnter the position at which u want to insert element= ");
    scanf("%d",&position);
    if((position>(temp+1))||(position<0))
        {
        printf("\tYou are going beyond range.\n\tYou can enter only from [0 to %d]!!!\n",(temp+1));
        insert_list(&l);
        }
    else
        {
        printf("\nEnter the value to be inserted= ");
        scanf("%d",&ele);

        while(temp>=position)
            {
            start->data[temp+1]=start->data[temp];
            temp--;
            }
            start->data[position]=ele;
            start->length+=1;
        }
    }

void delete_list(list *start)
    {
    int temp=start->length,pos;
    printf("\n\nEnter the position at which value u want to delete=");
    scanf("%d",&pos);
    if((pos>temp)||(pos<0))
        {
        printf("\tYou are going beyond range.\n\tYou can enter only from [0 to %d]!!!\n",temp);
        delete_list(&l);
        }
    else
        {
        while(pos<=start->length)
            {
            start->data[pos]=start->data[pos+1];
            pos++;
            }
        start->length-=1;
        }
    }

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Would you please give me c program only traverse in a Linear array? I only need traverse in linear array. I Can't separate from this following programme. Would you please send me a separate programme please?

    ReplyDelete
  3. this is the main part which is used to traverse
    void traverse_list(list *start)
    {
    printf("\nElements in the linear array are:\n");
    for(int i=0;i<=start->length;i++)
    {
    printf("%d, ",start->data[i]);
    }
    }

    remaining is for input or edit your input as without input we can't traverse matrix or array

    ReplyDelete

If you have any doubt, feel free to ask...