A data structure, the linear form of a single linked list stores the chain ---

A data structure, the linear form of a single linked list stores the chain ---

I. Introduction to Linear table

------ linear table is a linear structure, which is a finite sequence of zero or more data elements thereof. Characterized in that the linear form in a sequence, in addition to head and tail elements, and each element has only a direct precursor, and only a direct successor, the head element without direct precursor, the tail element has no immediate successor.

------- common linear structure an array data structure, single chain, double chain, circular list and the like. Linear elements in the table is the same certain abstract data types. It may be a built-in type or structure of the C language, C ++ may be a custom type.

Second, the realization of a single list

------- called single node in a linked list chain is only one pointer field, and the pointer field for storing an address of the next node, different from the circular linked list, only a single linked list from the beginning to the final visit, but not back from the last node to the head node.

------- preliminary preparation, in order to facilitate future modifications in this type of unified redefine what

# include "stdio.h"
# include "iostream"
# define  list_length 100
# define add_list 10
# define  ok 1
# define  error 0
typedef int status;
using namespace std;

------ establish a node structure, and a pointer type structure redefinition

typedef struct LNode{
	status data;		//节点的数据域
	struct LNode * next;		//节点的指针域
}*TNode;

------ initialization list

int InitChainList( TNode&L) {
	L  = (LNode *)malloc(sizeof(LNode));
	if (!L)
		exit(OVERFLOW);
	L->data = 0;
	L->next = NULL;
	return ok;
}

This is why there will certainly be curious children's shoes not new, of course, you can use new, but I just simply to malloc under review, ha ha to be considerate, incidentally, together with everyone's review the difference between malloc / free, new / delete is:
In fact, the use of in most cases, both can be generic, but we still have to know the difference between two of his.

1, malloc and free is standard function c ++ / c language, new / delete the C ++ operator.

2, they can be used to apply dynamic memory and free memory. new / delete, in fact, the underlying malloc is executed / free. But the new / delete smarter, because new and delete automatically executed when the object is created in the constructor, the destructor will automatically execute before the object disappear.

3, new pointer returns the specified type, and may automatically calculate the required size. Such as:

int *p = new int; //返回类型为int*类型,大小为sizeof(int);

Malloc user must specify the size, and the return type is silent void *, it must be forced into actual pointer type.

int *p = (int*)malloc(sizeof(int));//返回类型为void*,必须强行转换为实际类型的指针,而且还得计算大小

------ increase node

int addChainList(TNode &L,status elem) {
	TNode p = (LNode*)malloc(sizeof(LNode));
	if (!p)
		exit(OVERFLOW);
	TNode q=L;
	while (q->next){
		q = q->next;
	}
	q->next = p;
	p->data = elem;
	p->next = NULL;
	return ok;
}

Here add a node when you must traverse the list to the last node, and then apply a good node connected to the next field of the last node.

------ delete nodes

int deleChainList(TNode &L, int index,status&s) {
	TNode p = L->next;
	int i = 0;
	while (p&&index>=0) {
		if (i==index-1){
			TNode q = p->next;
			p->next = q->next;
			s = q->data;
			free(q);
		}
		p = p->next;
		i++;
	}
	return ok;
}

Here delete the marked element index, first find the previous element index, and the index of the subsequent element assigned to the index of the precursor, i.e., to achieve index predecessor and successor connection, and with a temporary node q point index node, then free (q), and the value assigned to the removed element S;

------- access element at index index

int ChainListEleAt(TNode L,int index,status&s) {
	TNode p = L;
	int i = 0;
	while (p->next) {
		p = p->next;
		if (i==index){
			s = p->data;
			break;
		}
		i++;
	}
	return ok;
}

Here it is consistent with previous ideas.

The first article send hope you can ask questions at any time with feedback, common progress! ! !

Released four original articles · won praise 4 · Views 210

Guess you like

Origin blog.csdn.net/weixin_42183953/article/details/88652796