Exemplo n.º 1
0
Status getElemFromLinkedList(const LinkedList L, int i, ElemType *e){
	assert(L);
	LinkedList p = L;
	if(i < 1 || i > lengthOfLinkedList(L) ){
		printf("Error: invalid index.\n");
		return(ERROR);
	}
	while(i--){
		p = p->next;
	}
	*e = p->data;
	return OK;
}
Exemplo n.º 2
0
Status removeItemFromLinkedList(LinkedList L, int i, ElemType *e){
	assert(L);
	LinkedList p = L, q = NULL;
	if(i < 1 || i > lengthOfLinkedList(L)){
		printf("Error: invalid index for remove.\n");
		return ERROR;
	}
	while(--i){
		p = p->next;
	}
	q = p->next;
	p->next = q->next;
	*e = q->data;
	L->data -= 1;
	free(q);
	return OK;
}
Exemplo n.º 3
0
/* 在L中第i个位置之前插入新的数据元素e, 1<= i <= length(L) + 1*/
Status insertItemToLinkedList(LinkedList L, int i, ElemType e){
	assert(L);
	LinkedList p = L;
	if(i < 1 || i > lengthOfLinkedList(L) + 1){
		printf("Error: invalid index for insertion, index must be in[1,%d],you give %d.\n", lengthOfLinkedList(L),i);
		return ERROR;
	}
	LNode * newNode = (LNode *)malloc(sizeof(LNode));
	while(--i){
		p = p->next;
	}
	newNode->data = e;
	newNode->next = p->next;
	p->next = newNode;
	L->data += 1;
	return OK;
}
Exemplo n.º 4
0
/*
 * Maintain two pointers – reference pointer and main pointer.
 *  Initialize both reference and main pointers to head.
 *  First move reference pointer to n nodes from head.
 *  Now move both pointers one by one until reference pointer reaches end.
 * Now main pointer will point to nth node from the end. Return main pointer.
 */
void SinglyLinkedList::printNthNodeFromEndLinkedList(int n){
	SingleNode *refptr=head;
	SingleNode *mainptr=head;
	int count =1;
	if(n>lengthOfLinkedList() || n<=0){
		cout<<"Number is not correct.Must be from 1 to length "<<endl;
		return;
	}
	while(count <= n){
		refptr = refptr->next;
		count ++;
	}
	while(refptr){
		refptr = refptr->next;
		mainptr = mainptr->next;
	}
	cout<<"Data is ::"<<mainptr->data;
}
Exemplo n.º 5
0
Status isLinkedListEmpty(const LinkedList L){
	assert(L);
	return lengthOfLinkedList(L) == 0;
}
Exemplo n.º 6
0
bool SinglyLinkedList::checkIfListPalindrome_usingNoStorage(){
	int length=lengthOfLinkedList();
	int middle,index_tmp;
	int index=0;
	if(length%2==0){
		middle=length/2;
	}
	else{
		middle=length/2+1;
	}
	SingleNode *tmp = head;
	SingleNode *prev =NULL;
	SingleNode *next;
	//Go to second half starting location
	while(index != middle){
		prev=tmp;
		tmp =tmp->next;
		index++;
	}
	SingleNode *end_firsthalf=prev;//Needed to recover the original list
	//Reverse second half of linked list
	while(tmp != NULL){
				next = tmp -> next;
				tmp -> next = prev;
				prev = tmp;
				tmp = next;
			}
	SingleNode *start_secondhalf=prev;//Needed to recover the original list
 //Now the second half of the list is reversed.
	SingleNode *index_firsthalf =head;
	SingleNode *index_secondhalf =prev;
	index=0;
	if(length%2==0){
		index_tmp=middle;
	}
	else{
		index_tmp=middle-1;
	}
	int flag=1;
	while(index<index_tmp){
		if(index_firsthalf->data != index_secondhalf->data){
			flag=0;
			break;
		}
		index_firsthalf=index_firsthalf->next;
		index_secondhalf=index_secondhalf->next;
		index++;
	}
	SingleNode *next_recoverlist;
	SingleNode *prev_recoverlist=NULL;
	int index_recoverlist=0;
	while(index_recoverlist!=length/2){
		next_recoverlist=start_secondhalf->next;
		start_secondhalf->next = prev_recoverlist;
		prev_recoverlist=start_secondhalf;
		start_secondhalf=next_recoverlist;
		index_recoverlist=index_recoverlist+1;
	}
	//Reverse the list again to undo the changes
   if(flag==0)
	return false;
   else
	   return true;
}