在VS2005下面测试通过.最基本的
- #include "stdafx.h"
- #include<stdlib.h>
- #include "stdio.h"
- #include <iostream>
- using namespace std;
- typedef int Type;
- typedef struct LNode{
- Type data;//数据域
- struct LNode *next;//指针域
- }LNode,*LinkList;
- LinkList Head;//头结点
- //创建单链表 头插法,栈
- //返回链表头指针
- LinkList CreatLinkList1(){
- LinkList L=new LNode;
- L->next=0;
- LNode *s;
- Type value;//数据类型
- cin>>value;
- while (value!=-1)//-1是结束标志
- {
- s=new LNode;
- s->data=value;
- s->next=L->next;
- L->next=s;
- cin>>value;
- }
- return L;
- }
- //创建单链表尾插法
- //返回链表头指针
- LinkList CreatLinkList2()
- {
- LinkList L=new LNode;
- L->next=0;//空表
- LNode *Node,*rear=L;
- Type value;//数据类型
- cin>>value;
- while(value!=-1)//-1是结束标志
- {
- Node=new LNode;//??这里是否会失败?空指针?
- if(0==Node) break;//??
- Node->data=value;
- rear->next=Node;
- rear=Node;//r指向新的尾结点
- cin>>value;
- }
- rear->next=0;
- return L;
- }
- //求表长 表默认带头结点而且不包含头结点
- int LengthLinkList(LinkList L)
- {
- LNode *p=L;//p指向头结点
- int count=0;
- while(p->next)
- {
- p=p->next;
- count++;
- }
- return count;
- }
- //查找
- //按序号查找,返回指针值
- LNode * getLinkList(LinkList L,int i){
- LNode *p=L;
- int j=0;
- while(p->next!=0&&j<i)
- {p=p->next;j++;}
- if(j==i)return p;
- else return 0;//
- }
- //按值查找,返回第一个符合的值
- LNode *LocateLink(LinkList L,Type x){
- LNode *p=L->next;
- while(p!=0&&p->data!=x)
- p=p->next;
- return p;
- }
- //插入操作在第i个值之之后插入
- bool insertLinkList(LinkList L,int i,Type value)
- {
- bool returnvalue=false;
- LinkList p=getLinkList(L,i);
- LNode *Node=new LNode;//这里是否会失败??
- if(0!=p)
- {
- Node->next=p->next ;
- p->next=Node;
- Node->data=value;
- returnvalue=true;
- }
- return returnvalue;
- }
- //打印链表所有值并打印长度
- void printLink(LinkList L)
- {
- int count=0;
- if(L->next) //从表头走到第一个元素
- {
- L=L->next;
- cout<<L->data<<endl;
- }
- L=L->next;//走到第二个元素
- while(L!=0)
- {
- //printf("%d",L->data);//假设是int值
- cout<<L->data<<endl;
- count++;
- L=L->next;
- }
- printf("表长度为%d",count);
- }
- int main(){
- LinkList M;
- //声明
- LinkList CreatLinkList1();
- LinkList CreatLinkList2();
- int LengthLinkList(LinkList L);
- LNode * getLinkList(LinkList L,int i);
- bool insertLinkList(LinkList L,int i,Type value);
- void printLink(LinkList L);
- //
- //CreatLinkList1();//测试头插法
- M=CreatLinkList2();//测试尾插法
- //printf("长度为%d",LengthLinkList(M));测试长度代码
- //if(0!=getLinkList(M,3)) printf("找到");//按序号查找
- //if(LocateLink(M,3)) printf("找到");//测试按值查找
- //insertLinkList(M,2,8);
- cout<<"OK"<<endl;
- printLink(M);
- system("PAUSE");
- return 0;
- }
- /////
顺序表的动态增长
- #include "stdafx.h"
- #include<stdlib.h>
- #define initSize 100//表的初始定义
- #define ListIncrement 10
- typedef int ElemType;
- typedef struct{
- ElemType *elem;//数据元素
- int length;//使用长度
- int listsize;//总长度
- }SqList;
- bool InitSqList(SqList &L){
- bool trueorfalse=false;
- L.elem=new ElemType[initSize];
- if(!L.elem ) {}
- else {
- L.listsize=initSize;
- L.length=0;
- trueorfalse=true;
- }
- return trueorfalse;
- }
- bool insertSqList(SqList &L,int i ,int value){
- bool trueorfalse=false;
- if(i<0||i>L.length){}
- else{
- int j;
- for( j=L.length;j>i;j--)
- L.elem[j]=L.elem[j-1];
- L.elem[j]=value;
- trueorfalse=true;
- }
- return trueorfalse ;
- }
- bool deleteSqList(SqList &L,int i){
- bool trueorfalse=false;
- if(i<0||i>L.length) {}
- else{
- int j=L.length;
- for(i;i<j;i++)
- L.elem[i]=L.elem[i+1];
- L.length--;
- trueorfalse=true;
- }
- return trueorfalse;
- }
- int locateSqList(SqList L,int i)
- {
- int trueorfalse=-1;
- if(i<0||i>L.length) {}
- else{
- trueorfalse=L.elem[i];
- }
- return trueorfalse;
- }
- int main()
- {
- SqList M;
- //声明部分
- bool InitSqList(SqList &L);
- bool insertSqList(SqList &L,int i ,int value);
- bool deleteSqList(SqList &L,int i);
- //声明结束
- if(InitSqList(M) ) printf("InitTrue/n");
- if(insertSqList(M,0,1)) printf("inserttrue/n");
- if(deleteSqList(M,0))printf("deletetrue/n");
- system("PAUSE");
- return 0;
- }
- ////
单链表有空指针的可能.