数据结构—顺序表增删改查

#include 
#include 
#define ML 100 
typedef int ElemType ;
typedef struct List
{
	ElemType list[ML] ;
	int size ;
 }sqlist ;
sqlist *createList(int n )
{
	sqlist *L ;
	int i ;
	
	L = (sqlist *) malloc(sizeof (sqlist)) ;
	L -> size  = 0 ;
	printf("Please input the elements\n") ;
	
	for(i = 0 ; i < n ; i ++)
	{
		scanf("%d",&(L->list[i])) ;
		L -> size ++ ;
	}
	
	return L ;
}

void display(sqlist *L)
{
	int i ;
	for(i = 0 ; i < L->size ; i ++) printf("%d\t",L->list[i]) ;
	
	puts("") ;
}

int locateElem(sqlist *L , ElemType x)
{
	int i ;
	for(i = 0 ; i < L->size ; i ++)
	{
		if(L->list[i] == x) printf("The location of %d is %d", x , i) ;
	}
	
	puts("") ;
	
	return 0 ;
}

int insert(sqlist *L , int mark , ElemType x)
{
	if(mark < 0 || mark > L->size) return 0 ;
	
	int i ;
	for(i = L->size - 1 ; i >= mark ; i --)
		L->list[i + 1] = L->list[i] ;
	
	L->list[mark] = x , L->size ++ ;
	
	return 1 ;
}

int deleteElem(sqlist *L , int item)
{
	int j , i ;
	for(i = 0 ; i < L->size ; i ++)
		if(item == L->list[i])
		{
			j = i ; 
			break ; 
		}
	
	for(i = j ; i < L->size ; i ++)
		L->list[i] = L->list[i + 1] ;
	
	L->size -- ;
	
	return 1 ;
}

void main()
{
	int p , q , r , i , x , num ;
	sqlist *head ;
	printf("Please input the num of elements\n") ;
	scanf("%d",&num) ;
	head = createList(num) ;
	printf("create list finsted\n") ;
	display(head) ;
	printf("Please input insert location and element\n") ;
	scanf("%d%d",&p,&q) ;
	insert(head , p , q) ;
	display(head) ;
	printf("Please input the element to be delete\n") ;
	scanf("%d",&r) ;
	deleteElem(head , r) ;
	display(head) ;
	printf("Please input the element to be located\n") ;
	scanf("%d",&x) ;
	locateElem(head , x) ;
	
	return ;
}

你可能感兴趣的:(数据结构,链表)