#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
struct node *prev;
}Node,*NODE;
typedef struct
{
NODE head;
NODE current;
NODE tail;
NODE p;
int tot;
}LINK;
void creat(LINK &l)
{
l.head=(Node*)malloc(sizeof(Node));
l.tail=(Node*)malloc(sizeof(Node));
l.tot=1;
if(!l.head&&!l.tail )
printf("wrong\n");
else
{
l.head =NULL;
l.tail=NULL;
}
}
void add(LINK &l)
{
l.current=(Node*)malloc(sizeof(Node));
if(!l.current )
printf("wrong\n");
else
{
if(l.head==NULL)
{
l.head=l.tail=l.current;
l.tail->next=l.head;
l.tail->prev=l.head ;
}
else
{
l.tail->next=l.current ;
l.current ->next=l.head;
l.current->prev=l.tail;
l.tail=l.current;
}
printf("输入元素值:");
scanf("%d",&l.current->data );
// l.current->prev=l.current;
l.tot++;
}
}
void insert(LINK &l)
{
l.p=(Node*)malloc(sizeof(Node));
int sel,location;
printf("1)在某处之前插入\n2)在某处之后插入\n");
scanf("%d",&sel);
if(sel==1)
{
int count=1;
printf("在第几个位置前插入元素:");
scanf("%d",&location);
if(location==1)
{
l.p->next=l.head;//新节点的下一个节点是原来的头结点
l.p->prev=l.tail;//新节点的前一个节点是尾节点
l.head->prev=l.p;//原头结点的前一个节点是新节点
l.tail->next=l.p;//尾节点指向新节点
l.head=l.p;//新节点作为新的头结点
printf("输入元素:");
scanf("%d",&l.p->data);
}
else
{
l.current = l.head;////////////////
while(count++<location)
{
l.current=l.current->next;
}
/*
l.p->next=l.current->next ;//将新节点指向现在的插入节点
l.p->prev=l.current ;//新节点的前一项是当前节点
l.current->next->prev=l.p;//插入节点的前一个节点是新节点
l.current->next=l.p;//将新节点挂入链表
*/
l.current->prev->next = l.p;
l.p->prev = l.current->prev;
l.p->next = l.current;
l.current->prev = l.p;
printf("输入元素:");
scanf("%d",&l.p->data );
}
}
else if(sel==2)
{
int count=1;
printf("在第几个元素的后面插入:");
scanf("%d",&location);
while(count<location)
{
l.current=l.current->next;
count++;
}
/*
l.p->next =l.current->next;//新节点指向插入节点的后一个
l.current->next ->prev=l.p;//当前节点的下一节点的前一个是新节点
l.p->prev=l.current ;//新节点的前一个是当前节点
l.current->next=l.p;//插入节点的下一个是新节点
*/
l.p->prev = l.current;
l.p->next = l.current->next;
l.current->next->prev = l.p;
l.current->next = l.p;//注意顺序,这个指针的重新指定一定要放在最后
printf("输入元素:");
scanf("%d",&l.p->data);
}
l.tot++;
}
void del(LINK &l)
{
int sel,location;
l.p=(Node*)malloc(sizeof(Node));
l.current=l.head;
printf("1)删除某处之前的节点\n2)删除某处之后的节点\n");
scanf("%d",&sel);
if(sel==1)
{
int count=1;
printf("要删除第几个节点前的节点:");
scanf("%d",&location);
if(location==1)
{
l.p=l.tail;
l.p ->prev->next=l.head;//尾节点的前一个节点指向头结点
//l.head->prev=l.p->prev ;//头结点指向尾节点的前一个节点
l.tail=l.p->prev;//尾节点的前一个节点为新的尾节点
free(l.p);//清除原尾节点
}
else
{
while(count++<location)
{
l.current=l.current->next;//找到要删除节点
}
/*
l.p=l.current;
l.p->prev->next=l.p->next;//删除节点前的的节点指向删除节点的后一个节点
l.p->next->prev=l.p->prev;//删除节点后的节点指向删除节点前的节点
*/
l.p = l.current->prev;
l.p->prev->next = l.current;
l.current->prev = l.p->prev;
free(l.p);
}
}
else if(sel==2)
{
int count=1;
printf("要删除第几个节点后的节点:");
scanf("%d",&location);
/*
while(count<location)
{
l.current=l.current->next;//找到删除节点的前一个节点
count++;
}
l.p=l.current;
l.p->next =l.p->next->next;//删除节点的后一个节点是当前节点的下一个节点
l.p->next->next ->prev=l.p;//前节点的下一个节点是删除节点的后一个节点当
*/
while(count++<location)
{
l.current=l.current->next;//找到要删除节点
}
l.p = l.current->next;
l.p->next->prev = l.current;
l.current->next = l.p->next;
free(l.p);
}
l.tot--;
}
void search(LINK &l)
{
int location,count=1;
printf("查看第几个节点的信息:");
scanf("%d",&location);
l.current=l.head;
while(count++<location)
{
l.current=l.current->next;
}
printf("%d\n",l.current->data );
}
void show(LINK &l)
{
l.current =l.head ;
int i=1;
while(i<l.tot )
{
printf("第%d个元素是%d \n",i++,l.current ->data);
l.current =l.current ->next;
}
}
void main()
{
LINK L;
int sel;
creat(L);
printf("1)添加元素\n2)插入元素\n3)删除元素\n4)查询信息\n5)打印\n6)退出\n");
while(scanf("%d",&sel)&&sel!=6)
{
switch(sel)
{
case 1:add(L);break;
case 2:insert(L);break;
case 3:del(L);break;
case 4:search(L);break;
case 5:show(L);break;
}
printf("\n1)添加元素\n2)插入元素\n3)删除元素\n4)查询信息\n5)打印\n6)退出\n");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
指针链表文件操作等c语言难点实例程序
共4个文件
txt:4个
4星 · 超过85%的资源 需积分: 13 54 下载量 26 浏览量
2010-05-24
18:44:29
上传
评论
收藏 4KB RAR 举报
温馨提示
包含了对指针数组,数组指针的实例应用,对深入理解指针有很好的帮助;链表操作有双向循环链表的操作,可以用于实际应用;文件读写,以巧妙的方式读取文件内容,并且把文件中的内容存放到数组中,方便可靠。
资源推荐
资源详情
资源评论
收起资源包目录
范例.rar (4个子文件)
从文件中读取整数到数组中.txt 728B
双向循环链表.txt 4KB
指针数组和数组指针.txt 818B
双向链表修改.txt 5KB
共 4 条
- 1
资源评论
- xiaobocwb2014-08-20资源不错,很适合我当前的情况,通过这个资源弄明白了好多疑问
- caojun197906292013-06-19经典源码学习
otherway_haha
- 粉丝: 2
- 资源: 33
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功