数据结构:链表
一,单链表
1,首先定义节点结构:
struct node { char data; node *next; };
2,声明相关函数:
注意创建和显示的时候,需要返回头节点指针;
插入和删除的时候需要传入头结点指针的引用:"node* &head"。
node *Create(); node *search(node *head,const char findchar); void ShowList(node *head); void insert(node *&head,char dest,char newchar); void deleteNode(node *&head, char dest);
3,函数实现
node *Create() {
//先定义用到的三个标志节点:头、尾、中间。 node *head = NULL; node *end = head; node *pS = NULL; char temp; cout<<"输入,以#结束"<<endl; do{ cin>>temp; //逐个读取字符,进行判断处理。 if(temp != '#') { pS = new node; pS->data = temp; pS->next = NULL; if(head == NULL) { head = pS; //如果是没有头节点,那么把第一个节点同时作为head,end。 } else { end->next = pS; //第二个节赋值给end的next,即head的next,再把新节点赋给真实的end; } end = pS; //从第三个节点开始,把新节点赋值给上一个end的next,并作为真正的end。 } else { break; } } while(temp != '#'); return head; } void ShowList(node *head) { node *pHEAD = head; while(pHEAD != NULL) { cout<<pHEAD->data<<endl; pHEAD = pHEAD->next; } } node *search(node *head,const char findchar) { node *pDest = head; while(pDest != NULL ) { if(pDest->data == findchar) { cout<<"找到字符:"<<findchar<<endl; return pDest; } pDest = pDest->next; }; return NULL; } void insert(node* &head,char dest,char newchar) { //首先创建需要插入的节点。 node *Dest = new node; Dest->data = newchar; Dest->next = NULL; node *pGuard = search(head,dest); if(head == NULL || pGuard == NULL ) { Dest->next = head; head = Dest; cout<<"开头位置插入节点'd':"<<endl; } else { Dest->next = pGuard->next; //先连 pGuard->next = Dest; //后断 cout<<"节点"<<dest<<"后插入节点:"<<newchar<<endl; } } void deleteNode(node* &head, char dest) { node *pGuard = head; if(head->data == dest) { node *p = head; head = head->next; delete p;
p = NULL; cout<<"删除头结点:"<<dest<<endl; return; } else { while(pGuard->next != NULL) { if(pGuard->next->data == dest) { node *p = pGuard->next; if(p->next != NULL) { pGuard->next = p->next; } else if(p->next == NULL) { pGuard->next = NULL; } delete p; p = NULL; cout<<"删除的节点为:"<<dest<<endl; return; } pGuard = pGuard->next; } } }
4.调用
int main(int argc, char* argv[]) { //创建 node *pHead = Create(); //输出 cout<<"您创建的单链表是:"<<endl; ShowList(pHead); //查找 cout<<"请输入需要查找的字符"<<endl; char chFind; cin>>chFind; node *nFind = search(pHead,chFind); if(nFind != NULL) { cout<<"找到" <<chFind<<endl; } else { cout<<"未找到" <<chFind<<endl; } //插入 char chGuard;char newchar; cout<<"请输入需要插入的位置"<<endl; cin>>chGuard; cout<<"请输入要插入的值"<<endl; cin>>newchar; insert(pHead,chGuard,newchar); cout<<"插入结果:"<<endl; ShowList(pHead); //删除 char chDel; cout<<"请输入需要删除的字符"<<endl; cin>>chDel; deleteNode(pHead,chDel); ShowList(pHead); system("pause"); return 0; }
// doubleList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <fstream> #include "conio.h" using namespace std; struct _DOUBLE_LINK_NODE //定义一个双链表结构 { char * data; struct _DOUBLE_LINK_NODE* prev; //双链表的前驱 struct _DOUBLE_LINK_NODE* next;//双链表的后驱 } ; class DoubleList { private: public: struct _DOUBLE_LINK_NODE * Head; DoubleList() { Head = (struct _DOUBLE_LINK_NODE *) malloc (sizeof(_DOUBLE_LINK_NODE)); memset(Head, 0, sizeof(_DOUBLE_LINK_NODE)); } ~DoubleList() { } struct _DOUBLE_LINK_NODE * create_double_link_node(char * value) { struct _DOUBLE_LINK_NODE* pnode; pnode =(_DOUBLE_LINK_NODE *)malloc(sizeof(_DOUBLE_LINK_NODE)); memset(pnode,0,sizeof(_DOUBLE_LINK_NODE)); pnode->data=(char *)malloc(strlen(value)+1); memset(pnode->data,0,strlen(value)+1); memcpy(pnode->data,value,strlen(value)); pnode->prev=(struct _DOUBLE_LINK_NODE *)malloc(sizeof(_DOUBLE_LINK_NODE)); memset(pnode->prev,0,sizeof(_DOUBLE_LINK_NODE)); memcpy(pnode->prev,pnode,sizeof(_DOUBLE_LINK_NODE)); return pnode; } _DOUBLE_LINK_NODE* find_data_in_double_link(char * data) { _DOUBLE_LINK_NODE* pNode = Head; int count=count_number_in_double_link(); //for(;pNode && pNode->next;pNode = pNode->next) if (data==NULL) { return NULL; } else { for(int i=0;i<count;i++) { if (pNode->data && strcmp(pNode->data, data) == 0) { return pNode; } else { pNode = pNode->next; } } } return NULL; } bool insert_data_into_double_link(_DOUBLE_LINK_NODE *node,char * data) { _DOUBLE_LINK_NODE * pNode=Head; _DOUBLE_LINK_NODE * findNode= NULL; int count=count_number_in_double_link(); if (find_data_in_double_link(data)!=NULL) { findNode=find_data_in_double_link(data); } else { for(int i=0;i<count;i++) { if (pNode->next==NULL) { findNode=pNode; } else { pNode = pNode->next; } } } if (pNode->data==NULL && pNode->next ==NULL) { pNode->next=node->prev; node->prev=pNode; } else { if (findNode->next==NULL) { pNode->next=node->prev; node->prev=pNode; } else { node->next=findNode->next->prev; findNode->next=node; node->prev=findNode->prev; node->next->prev=node; } } return true; } bool delete_data_from_double_link(char * data) { _DOUBLE_LINK_NODE* pNode; pNode=find_data_in_double_link(data); //*pNode->next->prev = *pNode->prev; if (pNode->next!=NULL) { pNode->next->prev=pNode->prev; pNode->prev->next = pNode->next; } else { pNode->prev->next = pNode->next; } free(pNode); return true; } void print_double_link_node() { _DOUBLE_LINK_NODE *DoubleList =Head; while(NULL != DoubleList){ printf("%s\n", DoubleList->data); DoubleList = DoubleList ->next; } } int count_number_in_double_link() { int count = 0; _DOUBLE_LINK_NODE *DoubleList =Head; while(NULL != DoubleList){ count ++; DoubleList = DoubleList->next; } return count; } }; /////////////////////////////////////////// int _tmain(int argc, _TCHAR* argv[]) { DoubleList *list=new DoubleList(); char * str="Hello word!你好~~"; char * dd="jsgw"; _DOUBLE_LINK_NODE *node= list->create_double_link_node(str); _DOUBLE_LINK_NODE * node1=list->create_double_link_node(dd); list->insert_data_into_double_link(node,NULL); list->insert_data_into_double_link(node1,NULL); node= list->create_double_link_node("hello world!"); list->insert_data_into_double_link(node,"adf"); int d=list->count_number_in_double_link(); list->print_double_link_node(); printf("链表中共有%d条数据\n",d); printf("删除数据:"); char * str1="hello world!"; int a; cin>>a; if (a==0) { list->delete_data_from_double_link(str1); list->print_double_link_node(); } cin.get(); system("pause"); return 0; }