1.单链表
单链表的结点类型node定义:
typedef struct linknode { int data; struct linknode *node; }node;
<1>.建立单链表
void CreateSingleLink() { node *head, *p, *s; int tempValue; bool bCycle = true; //bCycle循环控制变量 head = (node *)malloc(sizeof(node)); //建立头结点 p = head; while (bCycle) { scanf("%d", &tempValue); if (0 != tempValue) //以0标志结束 { s = (node *)malloc(sizeof(node)); //建立下一个结点 s->data = tempValue; p->next = s; //把新结点连接到前面建立的链表中 p = s; } else { bCycle = false; } } p->next = NULL; //如果是循环链表,则改为p->next = head; p = head; head = head->next; free(p); //删除头结点 }
<2>.单链表查找结点
bool FindNode(node *head, int iValue) { bool bFind = false; node *p; //if 普通链表 p = head; //else 循环列表 //if (head->data == iValue) //{ // bFind = true; // return bFind; //} //p = head->next; while (p!=NULL && p->data!=iValue) { p = p->next; } if (p != NULL) { bFind = true; } return bFind; }
<3>.单链表长度
//普通链表
int LengthLink(node *head) { int iLength = 0; node *p; p = head; while (p != NULL) { p = p->next; iLength++; } return iLength; }
//循环链接
int LengthLink(node *head) { int iLength = 0; node *p; if (head == NULL) //空链表 { return iLength; } p = head->next; iLength = 1; while (p != NULL) { p = p->next; iLength++; } return iLength; }
<4>.单链表插入结点
bool InsertNode(node *head, int i, int iValue) { node *s, *p; int j; bool bInsert = false; s = (node *)malloc(sizeof(node)); //建立待插入结点 s->data = iValue; if (i == 0) { //表头插入结点 s->next = head; head = s; bInsert = true; return bInsert; } p = head; j = 1; while (p!=NULL && j<i) { j++; p = p->next; } if (p != NULL) { //查找成功,将s插入到其后 s->next = p->next; p->next = s; bInsert = true; } return bInsert; }
<5>.单列表删除结点
bool DeleteNode(node *head, int iValue) { node *p, *q; bool bDelete = false; if (head == NULL) { //链表为空,下溢处理 return bDelete; } if (head->data == iValue) { //表头为删除结点 p =head; head = head->next; free(p); bDelete = true; return bDelete; } q = head; p = head->next; //从第二个结点开始查找值为iValue的结点 while (p!=NULL && p->data!=iValue) { if (p->data != iValue) { q = p; p = p->next; } } if (p != NULL) { //找到结点,作删除处理 q->next = p->next; free(p); bDelete = true; } return bDelete; }
2.双链表
双链表结点类型定义
typedef struct linknode { int data; struct linknode *left, *right; }dnode;
<1>.创建双链表
void CreateDoubleLink() { dnode *head, *p, *s; int iValue; bool bCycle = true; //bCycle控制循环变量 head = (dnode *)malloc(sizeof(dnode)); p = head; while (bCycle) { scanf("%d", &iValue); if (iValue != 0) //0作为标志结束 { s = (dnode *)malloc(sizeof(dnode)); //建立下一个结点 s->data = iValue; p->right = s; s->left = p; p = s; } else { bCycle = false; } } head = head->right; //删除头结点 head->left = NULL; //如果是循环双链表,则head->left = p; p->right = NULL; //如果是循环双链表,则p->right = head; }
<2>.查找结点
//普通双链表
bool FindNode(dnode *head, int iValue) { bool bFind = false; dnode *p; p = head; while (p!=NULL && p->data!=iValue) { p = p->right; } if (p != NULL) { bFind = true;//找到结点 } return bFind; }
//循环双链表
bool FindNode(dnode *head, int iValue) { bool bFind = false; if (head->data == iValue) { bFind = true; return bFind; } dnode *p; p = head->right; while (p->data!=iValue && p!=head) { p = p->right; } if (p != head) { bFind = true; //找到结点 } return bFind; }
<3>.双链表插入结点
bool InsertNode(dnode *head, int i, int iValue) { dnode *p, *s; bool bInsert = false; s = (dnode *)malloc(sizeof(dnode)); //创建待插入结点; s->data = iValue; if (i == 0) { //表头插入结点 s->right = head; head->left = s; head = s; head->left = NULL; bInsert = true; return bInsert; } int j = 1; p = head; while (p!=NULL && j<i) { j++; p = p->right; } if (p != NULL) { //查找成功,把s插到p之后 if (p->right == NULL) //p为最后一个结点 { p->right = s; s->right = NULL; s->left = p; } else { s->right = p->right; p->right->left = s; p->right = s; s->left = p; } bInsert = true; } return bInsert; }
<4>.双链表删除结点
bool DeleteNode(dnode *head, int iValue) { bool bDelete = false; if (head == NULL) { //链表为空,下溢处理 return bDelete; } dnode *p; if (head->data == iValue) //表头为删除结点 { p = head; head = head->right; head->left = NULL; free(p); bDelete = true; return bDelete; } p = head->right; while (p!=NULL && p->data!=iValue) { if (p->data != iValue) { p = p->right; } } if (p != NULL) { if (p->right = NULL) //最后一个结点 { p->left->right = NULL; free(p); } else { p->left->right = p->right; p->right->left = p->left; free(p); } bDelete = true; } return bDelete; }