C链表操作

#define  _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

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

SLIST *SList_Create(); //创建链表
int SList_Print(SLIST *pHead); //遍历链表
int SList_NodeInsert(SLIST *pHead, int x, int y); //插入值  在x值之前 删除y
int SList_NodeDel(SLIST *pHead, int y);
int SList_Destory(SLIST *pHead);

//创建链表
SLIST *SList_Create()
{

    SLIST *pHead,/*头节点*/  *pM,/*新节点*/ *pCur/*尾节点*/;
    int        data;
    //创建头节点 并初始化
    pHead = (SLIST *)malloc(sizeof(SLIST));
    if (pHead == NULL)
    {
        return NULL;
    }
    pHead->data = 0;
    pHead->next = NULL;

    printf("\nplease enter you data: ");
    scanf("%d", &data);

    pCur = pHead;

    while (data != -1)
    {
        //创建业务节点 并初始化 不断接受输入 malloc新结点
        pM = (SLIST *)malloc(sizeof(SLIST));
        if (pM == NULL)
        {
            return NULL;
        }
        pM->data = data;
        pM->next = NULL;

        //2 新结点 入链表
        pCur->next = pM;

        //3 新结点变成当前节点
        pCur = pM;  //链表结点的尾部追加 

        printf("\nplease enter you data: ");
        scanf("%d", &data);
    }

    return pHead;
}

//打印链表
int SList_Print(SLIST *pHead)
{
    SLIST *tmp = NULL;
    if (pHead == NULL)
    {
        return -1;
    }
    tmp = pHead->next;

    printf("\nBegin\t");
    while (tmp)
    {
        printf("%d ", tmp->data);
        tmp = tmp->next;
    }
    printf("\tEnd");
    return 0;
}

//插入节点
int SList_NodeInsert(SLIST *pHead, int x, int y)
{
    SLIST *pM, *pCur, *pPre;

    //创建新的业务节点pM
    pM = (SLIST *)malloc(sizeof(SLIST));
    if (pM == NULL)
    {
        return -1;
    }
    pM->next = NULL;
    pM->data = y;

    //遍历链表
    pPre = pHead;
    pCur = pHead->next;

    while (pCur)
    {
        if (pCur->data == x)
        {
            break;
        }
        pPre = pCur;
        pCur = pCur->next;
    }

    //让新结点 连接 后续链表
    pM->next = pPre->next;
    //让前驱节点 连接 新结点
    pPre->next = pM;

    return 0;
}
//删除节点
int SList_NodeDel(SLIST *pHead, int y)
{
    SLIST  *pCur, *pPre;

    //初始化状态

    pPre = pHead;
    pCur = pHead->next;

    while(pCur != NULL)
    {
        if (pCur->data == y)
        {
            break;
        }
        pPre = pCur;
        pCur = pCur->next;
    }

    //删除操作
    if (pCur == NULL)
    {
        printf("没有找到结点值为:%d 的结点\n", y);
        return -1;
    }
    pPre->next = pCur->next;
    if (pCur != NULL)
    {
        free(pCur);
    }
    return 0;
}

int SList_Destory(SLIST *pHead)
{
    SLIST *tmp = NULL;
    if (pHead == NULL)
    {
        return -1;
    }

    while (pHead != NULL)
    {
        tmp = pHead->next;//下一个节点缓存下来
        free(pHead);
        pHead = tmp;//tmp变成了head
    }
    return 0;
}

int SList_Reverse(SLIST *pHead)
{
    SLIST    *p = NULL; //前驱指针
    SLIST    *q = NULL; //当前指针
    SLIST    *t = NULL; //缓存的一个结点

    if (pHead==NULL || pHead->next == NULL ||pHead->next->next ==NULL )
    {
        return 0;
    }

    //初始化     //前驱结点
    p = pHead->next;
    q = pHead->next->next;

    //p = pHead;  //代码能兼容
    //q = pHead->next;

    //一个结点 一个结点的逆置
    while(q)
    {
        t = q->next; //缓冲后面的链表

        q->next = p;    //逆置

        p = q; //让p下移一个结点

        q = t;
    }

    //头节点 变成 尾部结点 后  置null
    pHead->next->next = NULL;
    pHead->next = p;  //

    return 0;
}
void main()
{
    int  ret = 0;

    SLIST *pHead = NULL;
    pHead = SList_Create();
    ret = SList_Print(pHead);

    ret = SList_NodeInsert(pHead, 20, 19);
    ret = SList_Print(pHead);

    ret = SList_NodeDel(pHead, 19);
    ret = SList_Print(pHead);

    ret =  SList_Reverse(pHead);
    ret = SList_Print(pHead);

    SList_Destory(pHead);

    printf("hello...\n");
    system("pause");
    return ;
}
时间: 2024-07-29 05:05:22

C链表操作的相关文章

数据结构上机测试2-1:单链表操作A (顺序建表+关键字删除)

数据结构上机测试2-1:单链表操作A Time Limit: 1000MS Memory limit: 4096K 题目描述 输入n个整数,先按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据m,将单链表中的值为m的结点全部删除.分别输出建立的初始单链表和完成删除后的单链表. 输入 第一行输入数据个数n: 第二行依次输入n个整数: 第三行输入欲删除数据m. 输出 第一行输出原始单链表的长度: 第二行依次输出原始单链表的数据: 第三行输出完成删除后的单链表长度: 第四行依次输出完成删除后的

单链表操作

#include<stdio.h>#include<malloc.h> typedef struct Node{ int data; struct Node *next;}LinkList; //就地反转int LinkListRerverse(LinkList *head){ LinkList *q,*p; p = head->next; head->next = NULL; while(p != NULL){ q = p->next; p->next =

数据结构之链表操作

#include <IOSTREAM> #include <IOMANIP> #include <STRING> using namespace std; typedef struct Student { int id; char name[20]; char sex[20]; struct Student *next; }node; typedef node * pnode; pnode Create(); void Print(pnode h); void Sort

单向链表操作 原文:http://canlynet.blog.163.com/blog/static/255013652009113001041903/

/********************************************** 功能:单向链表操作(注意Head指针 需要被改变时传入的是二级指针) 日期:2009.12.29 作者:DC ***********************************************/ #include <stdio.h> #include <stdlib.h> #define OVER_FLOW -2 #define OK 0 typedef int ElemTy

单链表操作系列

#include<stdio.h> #include<stdlib.h> typedef int ElemType; //定义结点类型 typedef struct Node { ElemType data; struct Node *next; }LNode,*LinkList; //单链表的建立1,头插法建立单链表,逆序生成 LinkList LinkListCreateH() { LinkList L,p; L = (LinkList)malloc(sizeof(LNode)

链表操作 -- 有环链表问题

参考: http://blog.163.com/[email protected]/blog/static/1113522592011828104617420/ 问题: 判断一个链表中是否有环. 分析: 我们都知道,当一个链表中没有环时,我们使用一个指针能从头遍历到尾:当链表中有环时,链表会在环中旋转. 当我们只使用一个链表指针时,可想到方法就是使用额外的数据结构来存储遍历过的每个节点,在遍历next节点时,判断next节点是否已存在于存储的节点中. 存储结构可以选择为hashTable,这样的

C# 链表操作

关于链表操作,在C#当中微软已经提供了一个LinkedList<T>的数据结构,通过这个类提供的一系列方法就能够实现链表操作. 这里我提供一段代码,这是在论坛里面有人提问时给出的代码,它实现了自定义链表的操作(读者可以在此基础上进一步完善).因为这段代码涉及一些C#技巧,所以贴出来给初学者学习C#提供一点参考. 实体类:     /// <summary>     /// 学生类     /// </summary>     public class Student  

链表操作 -- 问题总结贴

本文同时收集了好友 sosohu 和 zhuoyuan 的文章,共同进步.欢迎指正. 链表结构的实现: -- zhouyuan 单链表操作: (1). 倒序访问 --  sosohu (2). 获取链表的倒数第K个元素 -- sosohu    zhouyuan carlsama (3). 查找链表的中间节点 -- sosohu  carlsama (4). 链表反转 -- sosohu zhuoyuan carlsama (5). 链表中节点的删除 -- sosohu  carlsama (6

数据结构之 线性表---单链表操作A (删除链表中的指定元素)

数据结构上机测试2-1:单链表操作A Time Limit: 1000MS Memory limit: 4096K 题目描述 输入n个整数,先按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据m,将单链表中的值为m的结点全部删除.分别输出建立的初始单链表和完成删除后的单链表. 输入 第一行输入数据个数n: 第二行依次输入n个整数: 第三行输入欲删除数据m. 输出 第一行输出原始单链表的长度: 第二行依次输出原始单链表的数据: 第三行输出完成删除后的单链表长度: 第四行依次输出完成删除后的

【数据结构与算法】java链表操作

链表操作代码量少但是比较容易出错,是比较适合面试的地方. 代码实现 /** * 源码名称:MyLinkList.java * 日期:2014-09-05 * 程序功能:java链表操作 * 版权:[email protected] * 作者:A2BGeek */ import java.util.Stack; public class MyLinkList { class LinkNode<T> { private T mValue; private LinkNode<T> mNe