Program of insertion of an element in list, Data Structure & Algorithms

Assignment Help:

Program will demonstrate the insertion of an element at desired position

/* Inserting an element into contiguous list (Linear Array) at particular position */

/* contiguous_list.C */

# include

/* definition of linear list */

typedefstruct

{

}list;

int data[10];

int count;

/*prototypes of functions */ void insert(list *, int, int); void create(list *);

 

void traverse(list *);

/* Definition of the insert funtion */

void insert(list *start, int position, int element)

{

int temp = start->count;

while( temp >= position)

{

start->data[temp+1] = start->data[temp];

temp --;

}

start->data[position] = element;

start->count++ ;

}

/* definition of the create function to READ data values in the list */

void create(list *start)

{

inti=0, test=1;

while(test)

{

fflush(stdin);

printf("\n input value for: %d:(zero to come out) ", i);

scanf("%d", &start->data[i]);

if(start->data[i] == 0)

test=0;

else

}

i++;

start->count=i;

}

/* OUTPUT FUNCTION TO PRINT ON THE CONSOLE */

void traverse(list *start)

{

inti;

for(i = 0; i< start->count; i++)

{

printf("\n Value at the position: %d: %d ", i, start->data[i]);

}

}

/* main function */

void main( )

{

int position, element;

list l;

create(&l);

printf("\n Entered list as:\n");

fflush(stdin);

traverse(&l);

fflush(stdin);

printf("\n input the position where youdesire to insert new item:");

scanf("%d", &position);

fflush(stdin);

printf("\n input value for desired position:");

scanf("%d", &element); insert(&l, position, element); traverse(&l);

}


Related Discussions:- Program of insertion of an element in list

Sorting algorithm is best if the list is already sorted, Which sorting algo...

Which sorting algorithm is best if the list is already sorted? Why? Insertion sort as there is no movement of data if the list is already sorted and complexity is of the order

Algorithm that counts number of nodes in a linked list, Q. Write an algorit...

Q. Write an algorithm that counts number of nodes in a linked list.                                       A n s . Algo rithm to Count No. of Nodes in Linked List C

#, write an algorithm to search a particular node in linked list which retu...

write an algorithm to search a particular node in linked list which returns " FOUND" or "NOT FOUND" as outcome.

Linked list, How to creat ATM project by using double linked list?

How to creat ATM project by using double linked list?

State about the pre- and post conditions, State about the pre- and post con...

State about the pre- and post conditions Programmers can easily document other pre- and post conditions and class invariants, though, and insert code to check most value preco

Types of triangular matrices, Triangular Matrices Tiangular Matrices is...

Triangular Matrices Tiangular Matrices is of 2 types: a)  Lower triangular b)  Upper triangular

What is Polyphase Sort, One of the best known methods for external sorting ...

One of the best known methods for external sorting on tapes is the polyphase sort. Principle: The basic strategy of this sort is to distribute ordered initial runs of predetermi

Polynomials - represented by using arrays, /* the program accepts two polyn...

/* the program accepts two polynomials as a input & prints the resultant polynomial because of the addition of input polynomials*/ #include void main() { int poly1[6][

Data Structure, Ask consider the file name cars.text each line in the file ...

Ask consider the file name cars.text each line in the file contains information about a car ( year,company,manufacture,model name,type) 1-read the file 2-add each car which is repr

What is gouraud shading, Gouraud Shading The faceted appearance of a La...

Gouraud Shading The faceted appearance of a Lambert shaded model is due to each polygon having only a single colour. To avoid this effect, it is necessary to vary the colour ac

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd