(1) ADT
typedef int ElemType
typedef struct Node{
ElemType data;
struct Node * next;
}Node;
typedef struct Node* linkList;
(2) int initList(linkList *L)
int initList(linkList *L)
{
(*L) = (linkList)malloc(sizeof(Node));
(*L)->next = NULL;
printf("create initial successfully\n");
return 1;
}
链表的插入。在那些函数中,我们无法使用简单赋值,因为变量名在函数的作用域内部是未知的。函数所拥有的只是一个指向需要修改的内存位置的指针,所以要对该指针进行间接访问操作以访问需要修改的变量。
(3) int createListHead(linkList *L,int n)
int createListHead(linkList *L,int n){
linkList p;
int i=0;
srand((int)time(0));
for( i = 0;i < n;i++)
{
p = (linkList malloc sizeof(Node);
p ->data = rand() %100;
printf("testing:Node[%d]=%d\n",i+1,p->data);
p->next = (*L)->next;
(*L)->next = p;
}
printf("linklist(headinsert) create successfully.\n");
return 1;
}
int createListTail(linkList *L,int n){
linkList p,temp;
temp = (*L);
int i;
srand((int)time(0));
for( i = 0; i < n;i++){
p = (linkList)malloc(sizeof(Node));
p->data = rand() % 100;
printf("testing:Node[%d]=%d\n",i+1,p->data);
p->next = NULL;
temp->next = p;
temp = p;
}
printf("linklist(tail)create successfully);
return 1;
}
(4)int getlength(linkList *L)
int getlength(linkList *L){
linkList p;
int length = 0;
p = (*L)->next;
while(p){
length++;
p = p->next;
}
return length;
}
(5) int printList(linkList *L)
int printList(linklist *L){
linkList p;
int i = 0;
p = (*L) ->next;
if(p == NULL){
printf("----------print the whole linklist----------\n");
}
while(p){
i++;
printf("the %d node 's data is =%d\n",i,p->data);
p=p->next;
}
return 1;
}
(6) int getElem(linkList *L,int i,ElemType *getdata)
int getElem(linkList *L,int i,ElemType *getdata){
linkList p;
p = (*L)->next;
if(p==NULL)
{
printf("the linklist is empty,please create a linklist\n");
*getdata = -1;
return 0;
}
if( i < 1)
{
printf("您所查询的节点%d,应该大于0,请重新输入查询\n",i);
*getdata = -1;
return 0;
}
int j =1;
while(p&&j<1){
j++;
p = p->next;
}
if(p == NULL)
{
printf("the node you have queried %d has beyonded the length of array\n",i);
*getdata = p->data;
printf("search successfully!\n",i);
return 1;
}
(7) Insert node
Insert node are classfied by two ways.
The first one is Insert node within the length of the linklist.
The second one is append a node at the end of the linklist.
We assume the length of the linklist is 10,the first one can insert nodes between 1 and 10.
For example, we insert nodes at the 10th place,then the 10th node change into 11.But the second is all the nodes can not change.We append a new node at the place of 11.
int insertList(linkList *L,int i,Elemtype data)
{
linklist p;
linklist insNode;
p = (*L);
int j=0;
// the linklist is empty,we insert a new node at the first place.
if(p->next == NULL){
printf(“the linklist is empty,we insert a new node at the first place.\n”);
insNode = (linkList)malloc(sizeif(Node));
insNode->data = data;
insNode->next = p->next;
p->next = insNode;
printf(“the node insert successfully.\n”);
return 1;
}
//at the situation of linklist is not empty.if beyand the length of the linkist,it will prompt error.
//
while(p&&j
j++;
p = p-> next;
//printf(“j=%d\tp->data=%d\n”,j,p->data);
}
if(p->next == NULL){
printf(“the place you want to insert into has beyand the length of the linklist %d,please operate again!\n”,j);
return 0;
}
insNode = (linkList)malloc(sizeof(Node));
insNode->data = data;
insNode->next = p->next;
p->next = insNode;
printf(“the node has been inserted successfully\n”);
return 1;
}
Append node.
int insertListTail(linkList *L,ElemType data)
{
linkList temp;
linkList p=(*L);
while§
{
temp = p;
p = p->next;
}
p = (linkList)malloc(sizeof(Node));
p->data = data;
p->next = NULL;
temp->next = p;
printf(“the node has been inserted successfully\n”);
return 1;
}
(8) Delete nodes.
int deleteList(linkList *L,int i,ElemType *data)
{
linkList p,pnext;
int j = 0;
p = (*L);
if(p->next == NULL){
printf(“the linklist is empty,can not delete the node.\n”);
*data = -1;
return 0;
}
while(p->next && j
p = p->next;
//printf(“j=%d\t p->data = %d\n”,j,p->data);
}
if( p -> next ==NULL) {
printf(“The node which you want to delete has beyond the length of linklist %d, please try again! \n”,j);
*data = -1;
return 0;
}
pnext = p->next;
p->next = pnext->next;
*data = pnext->data;
free(pnext);
printf(pnext);
printf(“the node has been deleted successfully\n”);
return 1;
}
(9) Delete this linkList.
int clearList(linkList *L){
linkList p,temp;
p = (*L)->next;
while§{
temp = p;
p = p->next;
free(temp);
}
(*L)->next = NULL;
printf("the whole linklist has been cleared.\n");
return 1;
}