数据结构练习
循环链表代码C++
首先,给出一遍链表的代码。
链表
#include <stdio.h>
#include <malloc.h>//using namespace std;c语言中不能使用;
#define bool short
#define true 1
#define false 0//! 元素为char类型
typedef char ElemType;typedef struct Node
{ElemType data;struct Node * next;
}Node,*LinkList;//! LinkList已经是指针了,等价于struct Node **L;
//! L为二级指针,赋值为指针的地址
InitListLinkList *L)
{//! 用*L即P申请了一个内存空间,P指针指向了这样一个节点,即成功的为P赋了初值*L=LinkList)mallocsizeofNode));//! set position next equal to null*L)->next=NULL;
}bool CreatFromHeadLinkList L)
{Node *s;char c;while1){c=getchar);ifc=='$'){//printf"Great success!\n");break;}else{s=Node *)mallocsizeofNode));s->data=c;s->next=L->next;L->next=s;// printf"%c",s->data);}}//printf"success?\n");return true;
}bool CreatFromTailLinkList L)
{//LinkList L;//L=Node *)mallocsizeofNode));Node *r,*s;r=L;char c;while1){c=getchar);ifc=='$'){s->next=NULL;break;}else{s=Node *)mallocsizeofNode));s->data=c;r->next=s;r=s;}}return true;
}Node * GetLinkList L,int n)
{ifn<=0){printf"Get position error!\n");return NULL;}int i=0;Node *P=L;whilei<n && P->next!=NULL){++i;P=P->next;}ifi==n){return P;}else{printf"Get position error!\n");return NULL;}
}Node *LocateLinkList L,char key)
{Node *P;P=L->next;whileP->data!=key && P->next!=NULL){P=P->next;}ifP->data==key){return P;}else{printf"Key value not int list.\n");return NULL;}
}int ListLengthLinkList L)
{Node *p;p=L->next;int ret=0;whilep!=NULL){++ret;p=p->next;}return ret;
}void InsertListLinkList L,int n,char e)
{ifn<=0){printf"Insert position error!");return;}Node *p,*s;p=L->next;int i=0;whilei<n-1 && p->next!=NULL){++i;p=p->next;}ifi==n-1){s=Node *)mallocsizeofNode));s->data=e;s->next=p->next;p->next=s;return;}else{printf"Insert position error!\n");return NULL;}
}bool DelListLinkList L,int k,ElemType *e)
{ifk<=0){printf"Delete position error!\n");return false;}Node *s,*p;s=L;p=L->next;//s=p;int i=1;whilep->next!=NULL && i<k){s=p;p=p->next;++i;}ifi==k){*e=p->data;//printf"%c",e);s->next=p->next;freep);return true;}else{printf"Delete position error\n");return false;}
}bool ClearListLinkList L)
{int n=ListLengthL);ElemType tmp;int i;fori=1;i<=n;++i){//printf"?");DelListL,1,&tmp);putchartmp);}putchar'\n');ifi==n){return true;}else{return false;}
}bool test1)
{//! 建立一级指针P,用于指向某个Node节点,节点不定LinkList P;//!give position,将一个指针的地址值传入InitList&P);//P=Node *)mallocsizeofNode));if!CreatFromHeadP)){printf"Creat from head fail");}printf"len:%d\n",ListLengthP));printf"list elems:\n");//CreatFromTailP);ClearListP);freeP);printf"Hello list!\n");return true;
}bool test2)
{//! 建立一级指针P,用于指向某个Node节点,节点不定LinkList P;//!give position,将一个指针的地址值传入InitList&P);//P=Node *)mallocsizeofNode));if!CreatFromTailP)){printf"Creat from head fail");}printf"len:%d\n",ListLengthP));printf"list elems:\n");Node *tmp;tmp=Node *)mallocsizeofNode));tmp=LocateP,'a');putchartmp->data);//CreatFromTailP);ClearListP);freeP);printf"Hello list!\n");return true;
}int main)
{iftest1)){printf"this test is build from head\n");printf"test1 run success\n\n\n");}iftest2)){printf"this test is build fron tail\n");printf"test2 run success\n");}return 0;
}
循环链表
其实我只写了一部分,创建和结构题方面有点差别,剩下的感觉差别不大,跟前面的链表的代码差不多。
#include <stdlib.h>
//#include <iostream.h>#define bool short
#define true 1
#define false 0typedef int ElemType;typedef struct Node
{ElemType data;struct Node * next;
}Node,*LinkList;//! 用尾指针表示循环链表
InitCLinkListLinkList *CL)
{*CL=LinkList)mallocsizeofNode));*CL)->next=*CL;
}bool CreatCLinkLIstLinkList CL)
{Node *rear,*s;int a;rear=CL;scanf"%d",&a);whilea!=0){s=Node *)mallocsizeofNode));s->data=a;rear->next=s;//s->next=rear->next;rear=s;scanf"%d",&a);}rear->next=CL;return true;
}boolint main)
{return 0;
}