链表存储基本操作

简介: #include #include #include #include /*定义表示结点的结构体类型*/typedef struct list { int data; struct list* ...
#include <stdio.h>
#include <stdlib.h> 
#include <string.h>
#include <windows.h>
/*定义表示结点的结构体类型*/
typedef struct list 
{
    int data;
    struct list* next; 
}LIST;
void add(int data);
LIST* findBack();
void insertBack(LIST *p);
void insert(int position,int data);
void discs(int data);
void returnvalue();
void del(int position);
void change(int position,int data);
void showMenu(); 


LIST head; //头结点 
char ch;
int count=0; //统计数据个数
int position; //位置
int main()
{   
    int select;
    head.next=NULL; //将头结点的next置为NULL
    while(1)
    {
        showMenu();
        printf("请选择需要的操作:");
        scanf("%d",&select);
        fflush(stdin);  //清除键盘缓冲区
        switch(select)  //根据用户选择,调用相应函数完成   
        {
        case 1:
        case 2:
        case 3:
        case 4:
        case 0:exit(1);
        default:printf("输入错误!\n");
        }
        system("pause");
    }
    return 0;
}
///////////     函数的定义       ////////// 
void add(int data) //添加 
{ 
    LIST *p;
    p=(LIST *)malloc(sizeof(LIST)); //新建结点
    count++;
    if(p == NULL)
    {
        printf("动态内存分配失败!");
        return;
    }
    p->data = data;
    insertBack(p); 
}

LIST* findBack() //查找尾结点地址 
{
    LIST *p;
    p=&head;
    while(p->next!=NULL)
    {
        p=p->next;
    }
    return p;
} 

void insertBack(LIST *p) //插入一个结点 
{
    LIST *tail;
    tail = findBack();
    tail->next = p;
    p->next = NULL;
    return ;
}

void change(int position,int data)  //改变 
{ 
    LIST *p;
    p=&head;
    while(position)
    {
        p = p->next;
        position--;
    }
    p->data = data;
}

void insert(int position,int data)  //插入 
{ 
    LIST *p,*pre;
    p=(LIST *)malloc(sizeof(LIST));
    if(position>(count+1)) 
    {
        printf("超出插入位置范围!\n");
        system("pause");
        return;
    }
    pre=&head;  //pre指向头结点
    position--;
    while(position)
    { 
        if(p!=NULL)
        {
            position--;
            pre = pre->next; //pre指向下一个结点  
        }
    }
    if(p!=NULL)
    { 
        p->next = pre->next;
        pre->next = p;
        p->data = data;
    }
}
void del(int position) //删除
{
    LIST *p,*pre;
    pre=&head;  //pre指向头结点
    p=head.next; //p指向第一个结点
        if(position < 0||position > count)  
    {
        printf("你删除的位置不存在\n");
            system("pause");
            return;
    }
    position--;
    while(position)
    { 
            position--;
            pre = pre->next; //p pre指向下一个结点 pre在p之前
            p = p->next;    
    } 
    if(p!=NULL)
    {
        pre->next = p->next;
        free(p);
    }
}
void returnvalue()  //反转 
{
    LIST *p,*q,*headp;
    headp=&head;
    p=&head;
    p = p->next;
    while(p->next != NULL)
    {
        q = p->next;
        p->next = q->next;
        q->next = headp->next;
        headp->next = q;
    }
    p->next =headp;
    headp =p->next;
    p->next = NULL; 
    printf("反转成功!\n");
    system("pause");
}
void showMenu()
{
    system("cls");  /*清屏*/
    return;
}

目录
相关文章
|
10月前
|
存储 算法
数据结构实验---顺序表的合并---链表的基本操作
数据结构实验---顺序表的合并---链表的基本操作
|
10月前
|
C语言
C语言链表基本操作
C语言链表基本操作
31 0
|
5月前
|
C++
c++ 链表基本操作
c++实例化对象: 类名 变量 = 类名() 如果是new方式,那么是类名* 指针名 = new 类名()
17 0
|
存储 算法
【数据结构和算法】图的各类概念与图的存储结构(还有十字链表与邻接多重表的介绍)
【数据结构和算法】图的各类概念与图的存储结构(还有十字链表与邻接多重表的介绍)
173 0
【数据结构和算法】图的各类概念与图的存储结构(还有十字链表与邻接多重表的介绍)
|
11月前
|
存储
线性表的链式存储——链表
线性表的链式存储——链表
132 0
|
存储 API
【数据结构】线性表的链式存储(链表)API及实现
【数据结构】线性表的链式存储(链表)API及实现
130 0
【数据结构】线性表的链式存储(链表)API及实现
|
存储
编写一个应用程序,在主类Test1类中,创建两个链表List&lt;E&gt;对象,分别存储通过键盘输入的字符串内容
编写一个应用程序,在主类Test1类中,创建两个链表List&lt;E&gt;对象,分别存储通过键盘输入的字符串内容
54 0
|
C++
【C/C++】用格雷戈里公式求π
输入精度e,使用格雷戈里公式(π/4​=1-1/3+1/5+...)求π的近似值,精确到最后一项的绝对值小于e。要求定义和调用函数funpi(e)求π的近似值。
333 0
【C/C++】用格雷戈里公式求π
|
存储
链式存储之:链表的引出及其简介
链式存储之:链表的引出及其简介
77 0
链式存储之:链表的引出及其简介