自己实现C语言单向链表

本文详细介绍了使用C++创建并操作链表的过程,包括创建、插入、删除、打印链表元素等基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<pre name="code" class="cpp">#include<iostream>
#include<stdio.h>
#include<string.h>
#include<malloc.h>

#define CHAR char
#define VARTYPE CHAR //默认存放char类型

using namespace std;

struct myNode;
typedef struct myNode Node;
typedef Node* List;
typedef Node* PtrToNode;


struct myNode
{
    VARTYPE data;
    PtrToNode next;
};
//在p位置后插入元素
void _insert(List T,int p,VARTYPE x);
//尾部插入元素
void _push(List T,VARTYPE x);
//移除内容为x的元素
void _removec(List T,VARTYPE x);
//移除内容为编号为p的元素
void _removep(List T,int p);
//删除链表
void _dellist(List T);
//打印char类型链表内容
#ifdef CHAR
void _print(List T);
#endif
//清空链表
void _clear(List T);
//创建链表
List createlist();
//返回编号为p的元素
VARTYPE _findc(List T,int p);
//链表元素个数
int _size(List T);
int main()
{
    List T = createlist();
    _push(T,'a');
    _push(T,'b');
    _push(T,'c');
    _push(T,'d');
    _push(T,'e');
    _push(T,'e');
    _push(T,'d');
    //_insert(T,2,'p');
    // _removec(T,'e');
    //_removep(T,3);
    //_print(T);
    //_clear(T);
    _push(T,'p');
    _push(T,'q');
    //printf("%d",_size(T));
    //printf("%c",_findc(T,3));
    _print(T);
    _dellist(T);
    return 0;
}

//在p位置后插入元素
void _insert(List T,int p,VARTYPE x)
{
    PtrToNode pre;
    PtrToNode tmp;
    int i = 0;
    while(NULL!=T)
    {
        pre = T;
        T = T->next;
        if(++i==p)
            break;
    }
    tmp = (PtrToNode)malloc(sizeof(Node));
    if(NULL==tmp)
        perror("malloc");
    else
    {
        tmp->data = x;
        pre->next = tmp;
        tmp->next = T;
    }
};
//尾部插入元素
void _push(List T,VARTYPE x)
{
    while(NULL!=T->next)
        T = T->next;
    PtrToNode tmp = (PtrToNode)malloc(sizeof(Node));
    if(NULL==tmp)
        perror("malloc");
    else
    {
        tmp->data = x;
        tmp->next = NULL;
        T->next = tmp;
    }
};
//移除内容为x的元素
void _removec(List T,VARTYPE x)
{
    PtrToNode pre;
    pre = T;
    T = T->next;
    while(NULL!=T)
    {
        if(T->data==x)
        {
            pre->next = T->next;
            free(T);
        }
        else
            pre = T;
        T = pre->next;
    }
};
//移除内容为编号为p的元素
void _removep(List T,int p)
{
    PtrToNode pre;
    int i = 0;
    while(NULL!=T)
    {
        pre = T;
        T = T->next;
        if(++i==p)
            break;
    }
    pre->next = T->next;
    free(T);
};
//删除链表
void _dellist(List T)
{
    PtrToNode tmp = T;
    while(NULL!=T)
    {
        tmp = T->next;
        free(T);
        T = tmp;
    }
};
//打印char类型链表内容
#ifdef CHAR
void _print(List T)
{
    T = T->next;
    while(NULL!=T)
    {
        printf("%c ",T->data);
        T=T->next;
    }
};
#endif
//清空链表
void _clear(List T)
{
    _dellist(*&T->next);
    T->next = NULL;
}
//创建链表
List createlist()
{
    PtrToNode tmp = (PtrToNode)malloc(sizeof(Node));
    tmp->next=NULL;
    return tmp;
}
//返回编号为p的元素
VARTYPE _findc(List T,int p)
{
    int i = 0;
    while(NULL!=T)
    {
        T = T->next;
        if(++i==p)
            break;
    }
    return T->data;
}
//链表元素个数
int _size(List T)
{
    int i = -1;
    while(NULL!=T)
    {
        T = T->next;
        ++i;
    }
    return i;
}


2014/10/15改进版

#include<iostream>
#include<cstdio>
#include<string>
#include<malloc.h>
using namespace std;

struct mynode;
typedef struct mynode Node;
struct mynode
{
    int data;
    Node *next;
};

char str[] = {"\
-------------------------------------------------\n\
-------C:创建链表-------------------------------\n\
-------D:删除节点-------------------------------\n\
-------I:插入新节点-----------------------------\n\
-------P:输出节点-------------------------------\n\
-------R:链表逆置-------------------------------\n\
-------Q:撤销链表-------------------------------\n\
-------E:退出-----------------------------------\n\
-------------------------------------------------\n\
"
             };

Node* createlist()
{
    int n;
    Node *head,*p,*q;
    head = (Node *)malloc(sizeof(Node));
    if(NULL==head)
    {
        perror("out of space");
        exit(1);
    }
    head->next = NULL;
    printf("请输入节点个数:");
    scanf("%d",&n);
    p = head;
    for(int i=1; i<=n; ++i)
    {
        q = (Node *)malloc(sizeof(Node));
        if(NULL==head)
        {
            perror("out of space");
            exit(1);
        }
        q->next = NULL;
        p->next = q;
        printf("data%2d: ",i);
        scanf("%d",&q->data);
        p = p->next;
    }
    return head;
}

void dellist(Node *head)
{
    Node *p;
    while(head)
    {
        p = head->next;
        free(head);
        head = p;
    }
}

void printlist(Node *head)
{
    int i = 1;
    head = head->next;
    while(head)
    {
        printf("data%2d: %d\n",i++,head->data);
        head = head->next;
    }
}

void _insert(Node *head)
{
    Node *temp;
    int pos;
    if(NULL==head)
        return;
    printf("请输入插入节点位置:");
    scanf("%d",&pos);
    if(pos<0)
        return;
    else
    {
        while(head->next&&pos--)
            head = head->next;
    }
    temp = (Node *)malloc(sizeof(Node));
    if(NULL==temp)
    {
        perror("out of space");
        exit(0);
    }
    printf("请输入data的值:");
    scanf("%d",&temp->data);
    temp->next = head->next;
    head->next = temp;
}

void delnode(Node *head)
{
    int pos;
    Node *p;
    printf("删除节点编号:");
    scanf("%d",&pos);
    if(pos<1)
        return ;
    while(head&&--pos)
    {
        head = head->next;
    }
    if(NULL==head)
        return;
    p = head->next;
    head->next = p->next;
    free(p);
}

void _reverse(Node* head)
{
    Node *p1,*p2,*p3;
    p1 = head->next;
    if(NULL==p1||p1->next==NULL)
        return;
    p2 = p1->next;
    p3 = p2->next;
    while(p3)
    {
        p2->next = p1;
        p1 = p2;
        p2 = p3;
        p3 = p3->next;
    }
    p2->next = p1;
    head->next->next = NULL;
    head->next = p2;
    return;
}

int main()
{
    char p;
    printf(str);
    while(scanf("%c",&p)!=EOF)
    {
        if(p>='a'&&p<='z')p = p-('a'-'A');
        Node *_list;
        switch(p)
        {
        case 'C':
            dellist(_list);
            _list = createlist();
            break;
        case 'D':
            delnode(_list);
            break;
        case 'I':
            _insert(_list);
            break;
        case 'P':
            printlist(_list);
            break;
        case 'R':
            _reverse(_list);
            break;
        case 'Q':
            dellist(_list);
            break;
        case 'E':
            dellist(_list);
            return 0;
            break;
        default:
            printf(str);
            break;
        }
    }
}




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值