删除链表中指定位置的元素
2016-04-16 15:30
381 查看
#include<stdio.h>
#include<iostream>
using namespace std;
typedef int Elemtype;
typedef struct node {
Elemtype data;
struct node* next;
};
node* Greatlist(int n) //创建链表
{
node*p, *r, *head = NULL;
r = (node*)malloc(sizeof(node));
int i;
Elemtype c;
for (i = 1; i <= n; i++)
{
cin >> c;
p = (node*)malloc(sizeof(node));
p->data = c;
p->next = NULL;
if (head == NULL)
{
r=head= p;
}
else
{
r->next = p;
r = p;
}
}
return head;
}
int Linklen(node*head) //返回链表长度,本题中用不到这个函数
{
int count = 0;
node* p = head;
while (p!=NULL)
{
count++;
p=p->next;
}
return count;
}
node* getPtr(node* head, int pos) //得到指定位置的链表元素的指针
{
node* p = head;
int i;
for (i = 1; i < pos; i++)
{
p = p->next;
}
return p;
}
int main()
{
node*l;
cout << "input 10 interger:" << endl;
l = Greatlist(10);
cout << "the length is:" << Linklen(l) << endl;
node*p, *q;
p = getPtr(l, 4);
q = p->next;
p->next = q->next;
free(q);
cout << "the linklist after deleting the fifth node :" << endl;
p = l;
while (p)
{
cout << p->data << " ";
p = p->next;
}
return 0;
}
#include<iostream>
using namespace std;
typedef int Elemtype;
typedef struct node {
Elemtype data;
struct node* next;
};
node* Greatlist(int n) //创建链表
{
node*p, *r, *head = NULL;
r = (node*)malloc(sizeof(node));
int i;
Elemtype c;
for (i = 1; i <= n; i++)
{
cin >> c;
p = (node*)malloc(sizeof(node));
p->data = c;
p->next = NULL;
if (head == NULL)
{
r=head= p;
}
else
{
r->next = p;
r = p;
}
}
return head;
}
int Linklen(node*head) //返回链表长度,本题中用不到这个函数
{
int count = 0;
node* p = head;
while (p!=NULL)
{
count++;
p=p->next;
}
return count;
}
node* getPtr(node* head, int pos) //得到指定位置的链表元素的指针
{
node* p = head;
int i;
for (i = 1; i < pos; i++)
{
p = p->next;
}
return p;
}
int main()
{
node*l;
cout << "input 10 interger:" << endl;
l = Greatlist(10);
cout << "the length is:" << Linklen(l) << endl;
node*p, *q;
p = getPtr(l, 4);
q = p->next;
p->next = q->next;
free(q);
cout << "the linklist after deleting the fifth node :" << endl;
p = l;
while (p)
{
cout << p->data << " ";
p = p->next;
}
return 0;
}
相关文章推荐
- 解决sui的Form表单验证回调问题
- vsftpd
- 分享一个完整的Mybatis分页解决方案
- Linux(妙算)串口通信
- LeetCode——026
- Android Bundle详解
- golang中文路径的问题
- web服务器搭建之ngrok篇
- APM代码中MAVLINK的初步分析。
- nginx 父子进程通信 channel
- Missing Operating System问题检查及恢复
- 安卓与HTTPS
- 若你早知道这些,工作后就不会混成现在的样子!
- 华为P9遭疯抢,首发3分钟售罄;苹果汽车概念图遭权威杂志曝光引热议;中科大机器人太逼真
- HTML5+JS手机web开发之jQuery Mobile初涉
- Apple quietly slips WebRTC audio, video into Safari's WebKit spec
- URL shortner
- HttpServletResponse 和 HttpServletRequest的应用场景
- c语言结构体复习笔记
- List遍历:for,foreach还是Iterator?