b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

C语言中怎样用链表实现增删改查

电脑杂谈  发布时间:2019-08-13 04:02:47  来源:网络整理

实现链表_数组实现链表_c 链表实现

#include <stdio.h>

#include <stdlib.h>

typedef struct node

{

int nDate;

struct node *pstnext;

}Node;

//函数输出

void output(Node *head)

{

Node *p = head->pstnext;

while(NULL != p)

{

printf("%d ", p->nDate);

p = p->pstnext;

}

printf("\r\n");

}

//函数构建

Node* creat()

{

Node *head = NULL, *p = NULL, *s = NULL;

int Date = , cycle = 1;

head = (Node*)malloc(sizeof(Node));

if(NULL == head)

{

printf("分配内存失败\r\n");

return NULL;

}

head->pstnext = NULL;

p = head;

while(cycle)

{

printf("请输入数据且当输入数据为0时结束输入\r\n");

scanf("%d",&Date);

if( != Date)

{

s = (Node*)malloc(sizeof(Node));

if(NULL == s)

{

printf("分配内存失败\r\n");

实现链表_数组实现链表_c 链表实现

return NULL;

}

s->nDate = Date;

p->pstnext = s;

p = s;

}

else

{

cycle = ;

}

}

p->pstnext = NULL;

return(head);

}

//函数按值查找

void research_Date(Node *head实现链表, int date)

{

Node *p;

int n=1;

p = head->pstnext;

while(NULL != p && date != p->nDate)

{

p = p->pstnext;

++n;

}

if(NULL == p)

{

printf("函数中没有找到该值");

}else if(date == p->nDate)

{

printf("要查找的值%d在函数中第%d个位置\r\n", date, n);

}

return;

}

//按序号查找

void research_Number(Node *head, int Num)

{

Node *p=head;

int i = ;

while(NULL != p && i < Num)

{

c 链表实现_数组实现链表_实现链表

p = p->pstnext;

i++;

}

if(p == NULL)

{

printf("查找位置不非法\r\n");

}else if(i == )

{

printf("查找位置为头节点\r\n");

}else if(i == Num)

{

printf("第%d个位置数据为%d\r\n", i, p->nDate);

}

}

//在指定元素之前插入新节点

void insert_1(Node *head, int i, int Newdate)

{

Node *pre = head, *New = NULL;

int j = ;

while(NULL != pre && j < i-1)

{

pre = pre->pstnext;

j++;

}

if(NULL == pre || j > i-1)

{

printf("插入位置不存在\r\n");

}else

{

New = (Node*)malloc(sizeof(Node));

if(NULL == New)

{

printf("分配内存失败\r\n");

return;

}

New->nDate = Newdate;

New->pstnext = pre->pstnext;

pre->pstnext = New;

}

}

//在指定元素以后插入新节点

实现链表_数组实现链表_c 链表实现

void insert_2(Node *head, int i, int Newdate)

{

Node *pre = head, *New = NULL;

int j = ;

while(NULL != pre->pstnext && j < i)

{

pre = pre->pstnext;

j++;

}

if(j == i)

{

New = (Node*)malloc(sizeof(Node));

if(NULL == New)

{

printf("分配内存失败\r\n");

return;

}

New->nDate = Newdate;

New->pstnext = pre->pstnext;

pre->pstnext = New;

}else

{

printf("插入位置不存在\r\n");

}

}

//删除指定节点

void Delete_1(Node *head, int i3)

{

Node *p = head, *pre = NULL;

int j = ;

while(NULL != p && j < i3)

{

pre = p;

p = p->pstnext;

j++;

}

if(NULL == p)

{

printf("删除位置不存在\r\n");

}else

{

c 链表实现_数组实现链表_实现链表

pre->pstnext = p->pstnext;

free(p);

}

}

void main()

{

int date, num; //待查找数据

int i3;//指定删除元素的位置

int i1, i2, Newdate_1, Newdate_2; //待插入的新数据

Node *Head = NULL; //定义头结点

Node *Head_New = NULL;

//函数构建

Head = creat();

printf("输出建立的单主键\r\n");

output(Head);

//函数按值查找

printf("请输入待查找的数据\r\n");

scanf("%d", &date);

research_Date(Head, date);

//函数按序号查找

printf("请输入待查找序号\r\n");

scanf("%d", &num);

research_Number(Head, num);

//在指定第i1个元素之前插入新元素Newdate

printf("在指定第i个元素之前插入新元素Newdate");

printf("请输入i与元素且以空格间隔\r\n");

scanf("%d,%d", &i1, &Newdate_1);

insert_1(Head, i1, Newdate_1);

printf("插入后新函数\r\n");

output(Head);

//在指定第i2个元素以后插入新元素Newdate

printf("在指定第i个元素以后插入新元素Newdate");

printf("请输入i与元素且以空格间隔\r\n");

scanf("%d,%d", &i2,&Newdate_2);

insert_2(Head实现链表, i2, Newdate_2);

printf("插入后新函数\r\n");

output(Head);

//指定删除i3元素

printf("删除元素的位置\r\n");

scanf("%d", &i3);

Delete_1(Head, i3);

printf("删除后新函数\r\n");

output(Head);

return;

}


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-119049-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...