注:因为夜大学的数据结构的作业,自己费了些时间给完成了,发布到博客,记录下。
不错的参考资料:
http://www.cnblogs.com/xuyuan77/archive/2008/03/29/1129295.html
http://blog.csdn.net/conanswp/article/details/23275531
单链表的数据结构,作业要求:
linklist.h
#ifndef __LINKLIST_H__ #define __LINKLIST_H__ #include <stdio.h> #include <stdlib.h> //define lnode struct typedef struct node { int data; struct node *next; } lnode; lnode *src = NULL; /****define lnode operation*****/ //init the linklist bool initLinkList(); //add element to linklist bool addElementToLinkList(lnode *addEle); //add element to point position bool addElementToPointPosition(int index, lnode *addEle); //delete element to point position bool delElementByIndex(int index); //find element in linklist int findElementInLinkList(lnode *ele); //get length of linklist int getLinkListLength(); //show node list void showNodeList(); #endif
linklist.cpp
#include <stdio.h> #include <stdlib.h> #include "linklist.h" /****define lnode operation*****/ //init the linklist bool initLinkList() { //分配内存空间 src = (lnode *)malloc(sizeof(lnode)); if (NULL == src) { return false; } else { src->data = 0; src->next = NULL; printf("initLinkList Finished! \n"); return true; } return false; } //add element addEle to the end of linklist src bool addElementToLinkList(lnode *addEle) { //if head node not init if (NULL == src) { printf("addElementToLinkList NULL == src \n"); return false; } lnode *p = src->next; lnode *q = src; //traversal the linklist to the last node. while (NULL != p) { q = p; p = p->next; } q->next = addEle; addEle->next = NULL; printf("addElementToLinkList ADD SUCCESSFULLY \n"); return true; } //add element to point position bool addElementToPointPosition(int index, lnode *addEle){ if (NULL == src) { return false; } //get length of linklist int length = getLinkListLength(); if (index > length) { return false; } else { lnode *p = src; for (int i = 0; i < index; i++) { p = p->next; } lnode *temp = p->next; p->next = addEle; addEle->next = temp; return true; } } //delete element to point position bool delElementByIndex(int index) { //if head node not init. if (NULL == src) { return false; } //get length of linklist int length = getLinkListLength(); //if index > legth if (index > length) { return false; } else { lnode *q = src; lnode *p = src; for (int i = 0; i < index; i++) { q = p; p = p->next; } lnode *temp = p->next; q->next = temp; free(p); return true; } } //find element in linklist int findElementInLinkList(lnode *ele) { int index = 0; if (NULL == src) { return 0; } lnode *p = src; int length = getLinkListLength(); for (int i = 0; i < length; i++) { if ((p->data) == (ele->data)) { index = i; break; } p = p->next; } return index; } //get length of linklist int getLinkListLength() { int length = 0; lnode *p = src->next; while (NULL != p) { length++; p = p->next; } return length; } //show node list void showNodeList() { if (NULL == src) { printf("linklist is NULL"); return; } lnode *p = src; int length = getLinkListLength(); printf("===============SHOWNODELIST===============\n"); for (int i = 0; i < length; i++) { p = p->next; printf("showNodeList: lnode[%d] is %d \n", i, p->data); } printf("===============SHOWNODELIST===============\n\n\n"); } int main() { initLinkList(); //建立有10个元素的线性表 for (int i = 0; i < 9; i++) { lnode *p = (lnode *)malloc(sizeof(lnode)); p->data = i; p->next = p; printf("lnode[%d] is %d \n", i, p->data); addElementToLinkList(p); } //输出单链表元素 showNodeList(); //向指定位置插入数据 lnode *addEle = (lnode *)malloc(sizeof(lnode)); addEle->data = 8888; addEle->next = addEle; addElementToPointPosition(5, addEle); //输出单链表元素 showNodeList(); //删除线性表中指定位置的元素 delElementByIndex(6); //输出单链表元素 showNodeList(); //输入一个元素,查找是否存在 lnode *fEle1 = (lnode *)malloc(sizeof(lnode)); fEle1->data = 5; fEle1->next = fEle1; printf("Linklist has %d ? %d \n", fEle1->data, findElementInLinkList(fEle1)); lnode *fEle2 = (lnode *)malloc(sizeof(lnode)); fEle2->data = 888; fEle2->next = fEle2; printf("Linklist has %d ? %d \n", fEle2->data, findElementInLinkList(fEle2)); getchar(); return 0; }
文章的脚注信息由WordPress的wp-posturl插件自动生成