博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C语言单链表的创建,插入,删除,逆致
阅读量:5900 次
发布时间:2019-06-19

本文共 2763 字,大约阅读时间需要 9 分钟。

  hot3.png

` /************************************************************************* > File Name: link.c > Author: 胡若晨 > Mail: ----------------------------- > Created Time: 2016年03月29日 星期二 14时01分11秒 ************************************************************************/ #include<stdio.h> #include<stdlib.h>

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

struct Node *create() //创建链表 { struct Node *p,*q,*head; //q是前驱指针 char interrupt[10]; //终端符

p = q = head = (struct Node *) malloc (sizeof(struct Node));printf("please input data:");scanf("%d",&p->data);

// printf("interrupt? yes($),no(any key) : "); // scanf("%s",interrupt); // while(p->data != interrupt[0]){ while(p->data != 0){ q = p; p = (struct Node *) malloc (sizeof(struct Node));

printf("please input data:");    scanf("%d",&p->data);

// printf("interrupt? yes($),no(any key) : "); // scanf("%s",interrupt);

q->next = p; //连接链表}p->next = NULL;return head;

}

void print(struct Node *head) { while(head != NULL){ printf("%4d",head->data); head = head->next; } printf("\n");

}

struct Node *delete(struct Node *head) { struct Node *p,*q;//q是前驱指针 p = q = head; int del,d; printf("\nplease input the del:"); scanf("%d",&del);

q = p = head;while(p){    d = del;    if( p->data != d){        q = p;        p = p->next;    }    else         break;    if(!p){        printf("can't find\n");    }}if(p == head){    head = head->next;}else{    q->next = p->next;}return head;free(p);

}

struct Node *insert(struct Node *head) { struct Node *p,*ins; int e,inst;

printf("\nplease input the address(after) you want to insert:");scanf("%d",&inst);printf("\nplease input the element you want to insert:");scanf("%d",&e);p = (struct Node *) malloc (sizeof(struct Node));p->data = e;while(ins->data != inst){    ins = ins->next;}if(ins->next == NULL){    printf("can't find\n");}else{    p->next = ins->next;    ins->next = p;}if(head == NULL){    head = p;    p->next = NULL;}

// else{ //insert after head // p->next = head->next; // head->next = p; // } // else{//insert before head // p->next = head; // head = p; // }

return head;

} struct Node *reverse(struct Node *head) { struct Node *p,*r;

if(head->next != NULL){    p = head;    r = p->next;    p->next = NULL;//link tail == NULL    printf("p->data = %d\n",p->data);    while(r){        p = r;        r = r->next;        p->next = head;        head = p;    }}printf("\nreverse success\n");printf("The result is as following\n");return head;

}

int main(void) { struct Node *head;

head = create();print(head);

/下面这个地方一定要写成head = delete(head);以刷新head,博主就是在这里打成了delete(head);结果无法删除头节点,找了很久的bug,找到之后。。。。。。。/ head = delete(head); print(head); head = insert(head); print(head); head = reverse(head); print(head);

return 0;

} `

转载于:https://my.oschina.net/u/1774218/blog/649517

你可能感兴趣的文章
关于eclipse的ADT(插件)对xml的android:text属性检查修改
查看>>
iostat命令学习
查看>>
SQL 三种分页方式
查看>>
查看linux是ubuntu还是centos
查看>>
html video的url更新,自动清缓存
查看>>
IOS Xib使用——为控制器添加Xib文件
查看>>
CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙步骤
查看>>
react 取消 eslint
查看>>
【11】ajax请求后台接口数据与返回值处理js写法
查看>>
Python菜鸟之路:Jquery Ajax的使用
查看>>
LeetCode算法题-Maximum Depth of Binary Tree
查看>>
Vim和操作系统剪贴板交互
查看>>
Cox 教学视频5
查看>>
JVM类加载(4)—加载器
查看>>
public/private/protected的具体区别
查看>>
Jenkins持续集成学习-搭建jenkins问题汇总
查看>>
C#Note13:如何在C#中调用python
查看>>
Android介绍以及源码编译---Android源码下载
查看>>
SpringBoot集成redis缓存
查看>>
sql经典语句
查看>>