c链表结点的删除和添加
#include<stdio.h>
#include<stdlib.h>
typedef char datetype;/*定义新的数据类型名*/
typedef struct node
{
datetype date;
struct node *next;
}listnode;
typedef listnode *linklist;
int delete(linklist h,int num)/*删除结点*/
{
linklist p=h;
listnode *q=NULL;
int i=num;
int n=;
while(n<i)/*寻找删除位置*/
{
q=p;/*该结点和前后两个结点*/
p=p->next;
n++;
}
if(p==NULL)/*该结点不存在*/
printf("No Found Node!");
else
{
q->next=p->next;
free(p);
}
} void output(linklist head)/*链表遍历*/
{
linklist p=head;
while(p!=NULL)
{
printf("%c",p->date);
p=p->next;
}
}
int rear_creat(linklist head,int index0,int m)/*插入新结点,链表头,结点位置,结点date*/
{
linklist k,g;
int i=index0,n;
k=head;
n=;
while(n<i)/*寻找结点位置*/
{
n++;
k=k->next;
}
g=(listnode *)malloc(sizeof(listnode));
g->next=k->next;
g->date=m;
k->next=g;
}
int main()
{
char ch;
int index,index0;
char m;
linklist head=NULL;
listnode *p,*r;
ch=getchar();
while(ch!='\n')
{
p=(listnode *)malloc(sizeof(listnode));
p->date=ch;
if(head==NULL)
head=p;
else
r->next=p;
r=p;
ch=getchar();
}
output(head);
printf("\ndelete a node:\n");
scanf("%d",&index);
if(index==)
head=head->next;
else
delete(head,index);
output(head); printf("\ncreat a node:\n");
scanf("%d%c",&index0,&m);
rear_creat(head,index0,m);
output(head);
return ;
}
#include<stdio.h>/*尾插法*/
#include<stdlib.h>
typedef char datetype;
typedef struct node
{
datetype date;
struct node *next;
}listnode; typedef listnode *linklist;
linklist r=NULL,head=NULL; void rear_creat(datetype ch)
{
linklist p=NULL;
p=(listnode *)malloc(sizeof(listnode));
p->date=ch;
if(head==NULL)
{
head=p;
}
else
{
r->next=p;
}
r=p;
}
void output(linklist head)
{
while(head!=NULL)
{
printf("%c ",head->date);
head=head->next;
} }
void insert_rear(int index,int num)
{
listnode *p=NULL,*u;
u=head;
int n=;
p=(listnode *)malloc(sizeof(listnode));
p->date=index;
while(num>n)
{
u=u->next;
n++;
}
p->next=u->next;
u->next=p;
}
void delete(int num)
{
int n=;
linklist p=head,q;
while(num>n)
{
n++;
q=p;
p=p->next;
}
q->next=p->next;
} int main()
{
datetype index,ch;
int num;
ch=getchar();
while(ch!='\n')
{
rear_creat(ch);
ch=getchar();
}
output(head);
scanf("%c%d",&index,&num);
insert_rear(index,num);
output(head);
scanf("%d",&num);
delete(num);
output(head);
}
c链表结点的删除和添加的更多相关文章
- 时间复杂度分别为 O(n)和 O(1)的删除单链表结点的方法
有一个单链表,提供了头指针和一个结点指针,设计一个函数,在 O(1)时间内删除该结点指针指向的结点. 众所周知,链表无法随机存储,只能从头到尾去遍历整个链表,遇到目标节点之后删除之,这是最常规的思路和 ...
- 剑指Offer面试题:12.在O(1)时间删除链表结点
一.题目:在O(1)时间删除链表结点 题目:给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点. 原文采用的是C/C++,这里采用C#,节点定义如下: public class ...
- 【编程题目】在 O(1)时间内删除链表结点
60.在 O(1)时间内删除链表结点(链表.算法).题目:给定链表的头指针和一个结点指针,在 O(1)时间删除该结点.链表结点的定义如下:struct ListNode{int m_nKey;List ...
- 33.在O(1)时间删除链表结点[DeleteListNode]
[题目] 给定链表的头指针和一个结点指针,在O(1)时间删除该结点.链表结点的定义如下: C++ Code 123456 struct ListNode { int m_ ...
- 剑指Offer:面试题13——在O(1)时间删除链表结点
问题描述: 给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点.链表结点与函数的定义如下: public class ListNode{ int value; ListNode ...
- 【面试题013】在O(1)时间删除链表结点
[面试题013]在O(1)时间删除链表结点 我们要删除结点i,我们可以把结点i的下一个结点j的内容复制到结点i,然后呢把结点i的指针指向结点j的下一个结点.然后在删除结点j. 1.如果结点i位于链表 ...
- 【剑指offer 面试题13】在 O(1) 时间删除链表结点
#include <iostream> using namespace std; //构造链表结点 struct ListNode { int val; ListNode *next; L ...
- 在O(1)时间删除链表结点
题目:给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点. 链表结点与函数的定义如下: struct ListNode { int m_nValue; ListNode* m_p ...
- P99、面试题13:在o(1)时间删除链表结点
题目:给定单向链表的头指针和一个结点指针,定义一个函数在o(1)时间删除该结点.链表结点与函数的定义如下:struct ListNode{ int m_nValue; List ...
随机推荐
- iOS学习之详解AppDelegate
AppDelegate, 类似于监听接口. 用个很简单的例子说:ios系统会控制每个程序的开始和结束.但是ios又不知道每个程序的开始需要运行成么代码,结束需要运行什么代码.这个时候,ios就制定了一 ...
- win7自由调整CMD窗口
有如下命令,只需要改动相关参数即可以任意改变cmd窗口大小. mode con lines= mode con cols= color cls @cmd
- layout_weight
最近写Demo,突然发现了Layout_weight这个属性,发现网上有很多关于这个属性的有意思的讨论,可是找了好多资料都没有找到一个能够说的清楚的,于是自己结合网上资料研究了一下,终于迎刃而解,写出 ...
- LeetCode_implement strstr ()
Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...
- Qt工具知多少
一级题目: Qt Designer — 所见即所得的界面设计工具, 可以用拖拽的方式将控件排布在界面上,支持layout, 支持signal/slot编辑. 生成的文件保存为ui格式, ui是xml格 ...
- java设计模式--结构型模式--装饰模式
装饰模式 概述 动态地给一个对象添加一些额外的职责.就增加功能来说,Decorator模式相比生成子类更为灵活. 适用性 1.在不影响其他对象的情况下,以动态.透明的方式给单个对象添加职责. 2.处理 ...
- C++函数后面加const修饰
声明一个成员函数的时候用const关键字是用来说明这个函数是 "只读(read-only)"函数,也就是说明这个函数不会修改任何数据成员(object). 为了声明一个const成 ...
- bwlabel函数的c++实现
实验中需要用到区域联通的算法,就是类似于matlab中bwlabel的函数.网上找了找c++源码未果,bwlabel-python版用python描述了matlab中的实现方法,但是最后对标签的处理部 ...
- linux find命令-print0和xargs中-0使用技巧(转载)
本文介绍了linux find命令中-print0和xargs中-0用法技巧,一些find命令的使用经验,需要的朋友参考下. 本节内容:linux find命令中-print0和xargs中-0的用法 ...
- hadoop执行hbase插入表操作,出错:Stack trace: ExitCodeException exitCode=1:(xjl456852原创)
在执行hbase和mapreduce融合时,将hdfs上的文本文件插入到hbase中,我没有使用"胖包"(胖包就是将项目依赖的jar包放入项目打包后的lib目录中),而是直接将hb ...