数据结构_单链表

来源:互联网 发布:如何解析网址域名 编辑:程序博客网 时间:2024/06/02 21:54

单链表

编程实现单链表的基本操作(建立、插入、删除、求表长、顺序查找等),并设计一个菜单调用。
说明:略

/*头文件221*/#pragma once#include <stdio.h>#include <iostream>#include <malloc.h>#include <process.h>#include <string.h>#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0#define OVERFLOW -1typedef int Status;
/*头文件221*/#pragma oncetypedef int ElemType;typedef struct LNode { //结点    ElemType data;    struct LNode *next;}*Link,*Position;typedef struct  {     //链表    Link head;  //头指针    int len;  //节点数量}LinkList;
/*头文件223 function*/#pragma onceusing namespace std;Status InitList(LinkList &L){    //建立 插入、顺序查找、折半查找、排序等    int i, n;//n:元素个数; i:for 循环    Position p, q;//q:尾node  p:新node    L.head= (Position)malloc(sizeof(LNode));    if (!L.head) {        exit(OVERFLOW);    }    L.head->next = NULL;    q = L.head;    cout << "输入元素个数:";    cin >> n;    L.len = n;    cout << "输入各元素值:"<<endl;    for (i = 0; i<n; i++) {        p= (Position)malloc(sizeof(LNode));        if (!L.head) {            exit(OVERFLOW);        }        cin>>p->data;        q->next = p;        q = p;    }    q->next = NULL;    return OK;}Status DisplayList(LinkList &L) { //输出展示    Position q = NULL;    if (!(L.head->next)) cout <<"空表"<<endl;    q = L.head->next;    while (q) {        cout << q->data << "  ";        q = q->next;    }    cout<<endl;    return OK;}Status DestroyList(LinkList &L) { //清除    Position q=NULL;    while(L.head)    {        q = L.head->next;        free(L.head);        L.head = q;    }    return OK;}Status IsRighti(LinkList L, int n){    return OK;}Status InsertANode(LinkList &L) {  //插入元素,增加len    int n;//插入的位置    cout << "输入插入位置,一个 1—" << L.len + 1 << "的数字" << endl;    cin >> n;    while (n < 1 || n>1 + L.len) {        cout << "数字不合法,请重输入:";        cin >> n;    };    Position q = (Position)malloc(sizeof(LNode));    if (!q) exit(OVERFLOW);    Position p = (Position)malloc(sizeof(LNode));    if (!p) exit(OVERFLOW);    ElemType e;    cout << "输入插入数的值:";    cin >> e;    p->data = e;    q = L.head;    for (int i = 0; i < n-1; i++) {        q = q->next;    }    p->next = q->next;    q->next = p;    L.len++;    return OK;}Status DeleteANode(LinkList &L) {//删除元素,减少len    int n;//插入的位置    cout << "输入删除位置,一个 1—" << L.len << "的数字" << endl;    cin >> n;    while (n < 1 || n>L.len) {        cout << "数字不合法,请重输入:";        cin >> n;    };    Position p = L.head;    for (int i = 0; i < n - 1; i++) {        p = p->next;    }    Position q = p->next;    p->next=p->next->next;    q->next = NULL;    free(q);    L.len--;    return OK;}Status ReadANode(LinkList L, int n) {// 随机访问    Position p = L.head->next;    if (L.len < n&&n>0) return ERROR;    else {        for (int i = 1; i <n; i++) {            p = p->next;        }        cout << "postion " << n << ": " << p->data << endl;        return OK;    }}Status FindByTurn(LinkList L, ElemType e) {//顺序查找    Position p = L.head->next;    int i = 1;    int n = 0;    while (p) {        if (p->data == e) {            cout << "position : " << i << endl;            n++;        }        i++;        p = p->next;    };    if (!n) cout << "Not Found" << endl;    return OK;}Status SwapNode (Position p, Position q) {    ElemType temp;    temp = p->data;    p->data = q->data;    q->data = temp;    return OK;}Status BubbleSortList(LinkList &L) {//冒泡排序    for (int i = 0; i < L.len; i++) {        Position p = L.head->next;        for (int j = 1; j < L.len ; j++) {            if (p->data > p->next->data) SwapNode(p, p->next);            p = p->next;        }    }    return OK;}int GetLength(LinkList L) {    Position p = L.head->next;    int length = 0;    while (p) {        length++;        p = p->next;    }    return length;}

GERONIMO

原创粉丝点击