2.3-线性表的链接实现
 // DataStructTest.cpp : Defines the entry point for the console application.
// DataStructTest.cpp : Defines the entry point for the console application. //
//
 #include "stdafx.h"
#include "stdafx.h" #include <iostream.h>
#include <iostream.h> #include <malloc.h>
#include <malloc.h>
 typedef struct node * pointer;
typedef struct node * pointer; struct node
struct node {
{ int data;
    int data; pointer next;
    pointer next; };
}; typedef pointer lklist;
typedef pointer lklist; lklist head=NULL;                        //表头
lklist head=NULL;                        //表头
 //初始化链表
//初始化链表 lklist initiate_lklist()
lklist initiate_lklist() {
{ lklist t=(lklist)malloc(sizeof(node));
    lklist t=(lklist)malloc(sizeof(node)); t->next=NULL;
    t->next=NULL; return t;
    return t; }
} //求表长
//求表长 int length_lklist(lklist head)
int length_lklist(lklist head) {
{ int i=0;
    int i=0; lklist p=head;
    lklist p=head; while(p->next!=NULL)
    while(p->next!=NULL) {
    { i++;
        i++; p=p->next;
        p=p->next; }
    } return i;
    return i; }
} //按序号查找
//按序号查找 pointer find_lklist(lklist head,int index)
pointer find_lklist(lklist head,int index) {
{ lklist p=head;
    lklist p=head; int j=0;
    int j=0; while(p->next!=NULL && j<index)
    while(p->next!=NULL && j<index) {
    { j++;
        j++; p=p->next;
        p=p->next; }
    } if (j==index)
    if (j==index) return p;
        return p; else
    else return NULL;
        return NULL; }
} //定位
//定位 int locate_lklist(lklist head,int value)
int locate_lklist(lklist head,int value) {
{ int index=0;
    int index=0; lklist p=head;
    lklist p=head; while(p->next!=NULL && value!=p->data)
    while(p->next!=NULL && value!=p->data) {
    { p=p->next;
        p=p->next; index++;
        index++; }
    } if (p!=NULL && p->data==value)
    if (p!=NULL && p->data==value) return index;
        return index; else
    else return NULL;
        return NULL;
 }
} //删除
//删除 void delete_lklist(lklist head,int index, char * error)
void delete_lklist(lklist head,int index, char * error) {
{ lklist p=NULL;
    lklist p=NULL; lklist pNext=NULL;
    lklist pNext=NULL; p=find_lklist(head,index-1);                    //找到前驱
    p=find_lklist(head,index-1);                    //找到前驱 if (p!=NULL)
    if (p!=NULL) {
    { pNext=p->next->next;
        pNext=p->next->next; free(p->next);
        free(p->next); p->next=pNext;
        p->next=pNext; }
    } else
    else {
    { error="不存在此结点";
        error="不存在此结点"; }
    } }
} //新增
//新增 void insert_lklist(lklist head,int value,int index,char * error)
void insert_lklist(lklist head,int value,int index,char * error) {
{ lklist p=NULL;
    lklist p=NULL; lklist pNext=NULL;
    lklist pNext=NULL; lklist pNew=NULL;
    lklist pNew=NULL; p=find_lklist(head,index-1);
    p=find_lklist(head,index-1); if (p!=NULL)
    if (p!=NULL) {
    {     pNext=p->next;
        pNext=p->next; pNew=(lklist)malloc(sizeof(node));
        pNew=(lklist)malloc(sizeof(node)); p->next=pNew;
        p->next=pNew; pNew->next=pNext;
        pNew->next=pNext; pNew->data=value;
        pNew->data=value; }
    } else
    else {
    { error="不存在此结点";
        error="不存在此结点"; }
    } }
} //显示
//显示 void display(lklist head)
void display(lklist head) {
{ lklist p=head;
    lklist p=head; int i=0;
    int i=0; p=p->next;                //指向第一个结点
    p=p->next;                //指向第一个结点 while(p!=NULL)
    while(p!=NULL) {
    { i++;
        i++; cout<<"第"<<i<<"个元素的值为:"<<p->data<<endl;
        cout<<"第"<<i<<"个元素的值为:"<<p->data<<endl; p=p->next;
        p=p->next; }
    } }
} int main(int argc, char* argv[])
int main(int argc, char* argv[]) {
{ char * error=NULL;
    char * error=NULL; head=initiate_lklist();
    head=initiate_lklist(); pointer p=NULL;
    pointer p=NULL; int i=0;
    int i=0; //新增
    //新增 cout<<"新增4个结点"<<endl;
    cout<<"新增4个结点"<<endl; insert_lklist(head,1,1,error);
    insert_lklist(head,1,1,error); insert_lklist(head,2,2,error);
    insert_lklist(head,2,2,error); insert_lklist(head,3,3,error);
    insert_lklist(head,3,3,error); insert_lklist(head,4,4,error);
    insert_lklist(head,4,4,error); display(head);
    display(head); //表长
    //表长 i=length_lklist(head);
    i=length_lklist(head); cout<<"表长为:"<<i<<endl;
    cout<<"表长为:"<<i<<endl; //按序号查找
    //按序号查找 p=find_lklist(head,2);
    p=find_lklist(head,2); cout<<"第二个元素的值为:"<<p->data<<endl;
    cout<<"第二个元素的值为:"<<p->data<<endl; //定位
    //定位 cout<<"定位测试值为3的元素在链表中的位置为:"<<locate_lklist(head,3)<<endl;
    cout<<"定位测试值为3的元素在链表中的位置为:"<<locate_lklist(head,3)<<endl; //删除
    //删除 cout<<"删除第3个元素:"<<endl;
    cout<<"删除第3个元素:"<<endl; delete_lklist(head,3,error);
    delete_lklist(head,3,error); display(head);
    display(head); return 0;
    return 0; }
}
    一点说明:为什么在标题中要嵌入英文?原因是为了能够让国外的网友能查询到这篇文章。平常在Google上查资料的时候,经常参考国外网友的博客,帮助我解决了很多问题,所以我也想让他们能够参考我写的内容。当然文中我不可能全部译为英文,所以我尽量把代码粘全,靠代码说话吧。
 
                    
                     
                    
                 
                    
                


 
     
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号