苏嵌 暑假实训之第一天之数据结构单链双链表的初始化创建插入中间删除表之篇章。。。。
2012-07-14 09:44
267 查看
带着对项目班的 向往和担忧 我开始了这个被我称之为闭关修炼的学习。。。。诚然 我基础不好,尤其是 c 但是正如老师所说,都知道苦但又有几个吃下去这个苦呢,,,梅花香之苦寒来。。。人生的意义就是在于挑战自己的缺陷弥补自己的不足的 。。。。
废话不说了,,,开始今天的学习,首先上午老师让我们进行了c的阶段性测试,真心感觉那题目啊 很多不会做呢,,,概念题还可以 那编程题啊 ,,,压力山大。。。。
下午讲的是数据结构, 所以 我谈谈我的数据结构的 收获吧:
首先是单链表:
1、初始化节点
struct node
{
int num; /*数域*/
struct node *next; /*链域*/
};/*这里尤为注意这个冒号,创建节点后需要加冒号*/
2、初始化链表
void cre_list()
{
head = (struct node *)malloc(sizeof(structnode));/*分配内存空间*/
head->next = NULL;
head = NULL;
}
3、添加数据
void add_node(int num)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->num = num;
ptr->next = head;
head = ptr;
}
4、遍历
void display_node()
{
struct node *ptr = head;
while(ptr!=NULL)/*单链表尾为空*/
{
printf("%d\t",ptr->num);
ptr = ptr->next;
}
printf("\n");
}
练习1:创建一个单链表添加10个数据,然后将其逆序,再在5后面添加11,12,13,14,15,16。
代码如下:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int num;
struct node*next;
};
struct node*head;
void cre_list()
{
head = (struct node*)malloc(sizeof(struct node));
head->next = NULL;
head = NULL;
}
void display_node()
{
struct node *ptr = head;
while(ptr!=NULL)
{
printf("%d\t",ptr->num);
ptr = ptr->next;
}
printf("\n");
}
void add_node(int num)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->num = num;
ptr->next = head;
head = ptr;
}
void add_model(n,m)
{
struct node *ptr =head;
struct node *p = (struct node *)malloc(sizeof(struct node));
while (ptr)
{
if(ptr->num == n)
{
p->num=m;
p->next=ptr->next;
ptr->next=p;
}
ptr = ptr->next;
}
}
void re_node()
{
struct node *p1=NULL;
struct node *p2=NULL;
while(head)
{
p2 = head->next;
head->next = p1;
p1 = head;
head = p2;
}
head = p1;
}
void del_node()
{
struct node *ptr = head;
struct node *temp=NULL;
while(ptr)
{
if((ptr->num) % 2 ==0)
{
temp = ptr;
ptr = ptr->next;
head = head->next;
free(temp);
temp=NULL;
}
else
{
temp = ptr;
ptr = ptr->next;
if((ptr->num)%2 == 0)
{
temp->next = ptr->next;
temp = ptr;
ptr = ptr->next;
free(temp);
temp = NULL;
}
}
}
}
int main()
{ int i=0;
cre_list();
for(i;i<10;i++)
{
add_node(i);
}
display_node();
re_node();
display_node();
for(i=0;i<=5;i++)
{
if(i==0)
{
add_model(5,11);
}
else
{
add_model(i+10,i+11);
}
}
display_node();
return 0;
练习2有n个人围成一圈,顺序排号,从第一个开始报数(从1到3报数),凡报到3的退出圈子,问最后最后留下的是原来第几号的那个数(约瑟夫环)。
#include<stdio.h>
#include<stdlib.h>
struct node
{
int num;
struct node *next;
};
struct node *head;
struct node *last;
void cre_list()
{
head = (struct node*)malloc(sizeof(struct node));
last = head;
head->next = head;
}
void del_node()
{
last->next=head->next;
struct node *p1 = last->next;
struct node *p2 ;
int i=1;
while(p1->next!=p1)
{
p2=p1->next;
p2->next=p2->next->next;/*利用p1,p2删除后面第三个节点*/
p1=p2->next;
}
printf("%d\t",p1->num);
printf("\n");
}
void display_node()
{
struct node *ptr =head;
while(ptr->next!=head)
{
ptr=ptr->next;
printf("%d\t ",ptr->num);
}
}
void add_node(int num)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->num = num;
last->next = ptr;
ptr->next = head;
last = ptr;
}
int main()
{
int i;
cre_list();
for(i=0;i<10;i++)
{
add_node(i);
}
display_node();
del_node();
return 0;
}
以上编程思路画个图就全部很清晰了,约瑟夫环利用了p1,p2将其第三个也就是p1,p2后面一个删除,依次循环就可以了。
练习3用双向循环链表进行文件检索。(利用循环链表将一个目录下的文件建成一个循环链表,然后通过输入指令去查看上个文件以及下个文件)
#include<stdio.h>
#include<stdlib.h>
#include"fcntl.h"
#include"dirent.h"
#include<string.h>
struct node
{
char name[20];
struct node *prior;
struct node *next;
};
struct node *head = NULL;
struct node *last = NULL;
void cre_list()
{
DIR *mydir;
struct dirent *myitem;
mydir = opendir(".");
while (( myitem = readdir(mydir)) !=NULL)
{
if(( strcmp(myitem->d_name,".") ==0)||(strcmp(myitem->d_name,"..")==0))/*每个目录下面都有两个隐藏的文件所以这里需要将这两个文件排除*/
{
continue;
}
struct node *p = (struct node *)malloc(sizeof(struct node));
if( head == NULL)
{
strcpy(p->name,myitem->d_name);
head = p;
head->prior = NULL;
head->next = head;
last = p;
}
else
{
strcpy(p->name,myitem->d_name);
last->next = p;
p->prior = last;
last = p;
last->next = head;
}
}
head->prior = last;
closedir(mydir);
}
void display_node()
{
struct node *p = head;
while(p->next != head)
{
printf("%s\t",p->name);
p = p->next;
}
printf("%s\n",p->name);
}
void printfile()
{
int x;
printf("请选择功能\n");
printf("1输出上个文件\n");
printf("2输出下个文件\n");
printf("3正常退出\n");
struct node *p = head;
printf("此刻第一个文件为: %s\n",p->name);
while(1)
{
printf("input\n");
scanf("%d",&x);
switch(x)
{
case 1:
{
p = p->prior;
printf("上一个文件为:%s\n",p->name);
break;
}
case 2:
{
p=p->next;
printf("下一个文件是:%s\n",p->name);
break;
}
case 3:
{
printf("正常退出\n");
exit(0);
}
}
}
}
int main()
{
cre_list();
display_node();
printfile();
return 0;
}
文件检索主要还是建立在双向循环链表之上的,就是把一个目录下的文件有几个算几个串连起来形成一个双向链表,再通过移动指针来查看上一个或者是下个文件。感觉这个做起来 在目录加进来那个地方还是有点不太理解的。。。
废话不说了,,,开始今天的学习,首先上午老师让我们进行了c的阶段性测试,真心感觉那题目啊 很多不会做呢,,,概念题还可以 那编程题啊 ,,,压力山大。。。。
下午讲的是数据结构, 所以 我谈谈我的数据结构的 收获吧:
首先是单链表:
1、初始化节点
struct node
{
int num; /*数域*/
struct node *next; /*链域*/
};/*这里尤为注意这个冒号,创建节点后需要加冒号*/
2、初始化链表
void cre_list()
{
head = (struct node *)malloc(sizeof(structnode));/*分配内存空间*/
head->next = NULL;
head = NULL;
}
3、添加数据
void add_node(int num)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->num = num;
ptr->next = head;
head = ptr;
}
4、遍历
void display_node()
{
struct node *ptr = head;
while(ptr!=NULL)/*单链表尾为空*/
{
printf("%d\t",ptr->num);
ptr = ptr->next;
}
printf("\n");
}
练习1:创建一个单链表添加10个数据,然后将其逆序,再在5后面添加11,12,13,14,15,16。
代码如下:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int num;
struct node*next;
};
struct node*head;
void cre_list()
{
head = (struct node*)malloc(sizeof(struct node));
head->next = NULL;
head = NULL;
}
void display_node()
{
struct node *ptr = head;
while(ptr!=NULL)
{
printf("%d\t",ptr->num);
ptr = ptr->next;
}
printf("\n");
}
void add_node(int num)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->num = num;
ptr->next = head;
head = ptr;
}
void add_model(n,m)
{
struct node *ptr =head;
struct node *p = (struct node *)malloc(sizeof(struct node));
while (ptr)
{
if(ptr->num == n)
{
p->num=m;
p->next=ptr->next;
ptr->next=p;
}
ptr = ptr->next;
}
}
void re_node()
{
struct node *p1=NULL;
struct node *p2=NULL;
while(head)
{
p2 = head->next;
head->next = p1;
p1 = head;
head = p2;
}
head = p1;
}
void del_node()
{
struct node *ptr = head;
struct node *temp=NULL;
while(ptr)
{
if((ptr->num) % 2 ==0)
{
temp = ptr;
ptr = ptr->next;
head = head->next;
free(temp);
temp=NULL;
}
else
{
temp = ptr;
ptr = ptr->next;
if((ptr->num)%2 == 0)
{
temp->next = ptr->next;
temp = ptr;
ptr = ptr->next;
free(temp);
temp = NULL;
}
}
}
}
int main()
{ int i=0;
cre_list();
for(i;i<10;i++)
{
add_node(i);
}
display_node();
re_node();
display_node();
for(i=0;i<=5;i++)
{
if(i==0)
{
add_model(5,11);
}
else
{
add_model(i+10,i+11);
}
}
display_node();
return 0;
练习2有n个人围成一圈,顺序排号,从第一个开始报数(从1到3报数),凡报到3的退出圈子,问最后最后留下的是原来第几号的那个数(约瑟夫环)。
#include<stdio.h>
#include<stdlib.h>
struct node
{
int num;
struct node *next;
};
struct node *head;
struct node *last;
void cre_list()
{
head = (struct node*)malloc(sizeof(struct node));
last = head;
head->next = head;
}
void del_node()
{
last->next=head->next;
struct node *p1 = last->next;
struct node *p2 ;
int i=1;
while(p1->next!=p1)
{
p2=p1->next;
p2->next=p2->next->next;/*利用p1,p2删除后面第三个节点*/
p1=p2->next;
}
printf("%d\t",p1->num);
printf("\n");
}
void display_node()
{
struct node *ptr =head;
while(ptr->next!=head)
{
ptr=ptr->next;
printf("%d\t ",ptr->num);
}
}
void add_node(int num)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node));
ptr->num = num;
last->next = ptr;
ptr->next = head;
last = ptr;
}
int main()
{
int i;
cre_list();
for(i=0;i<10;i++)
{
add_node(i);
}
display_node();
del_node();
return 0;
}
以上编程思路画个图就全部很清晰了,约瑟夫环利用了p1,p2将其第三个也就是p1,p2后面一个删除,依次循环就可以了。
练习3用双向循环链表进行文件检索。(利用循环链表将一个目录下的文件建成一个循环链表,然后通过输入指令去查看上个文件以及下个文件)
#include<stdio.h>
#include<stdlib.h>
#include"fcntl.h"
#include"dirent.h"
#include<string.h>
struct node
{
char name[20];
struct node *prior;
struct node *next;
};
struct node *head = NULL;
struct node *last = NULL;
void cre_list()
{
DIR *mydir;
struct dirent *myitem;
mydir = opendir(".");
while (( myitem = readdir(mydir)) !=NULL)
{
if(( strcmp(myitem->d_name,".") ==0)||(strcmp(myitem->d_name,"..")==0))/*每个目录下面都有两个隐藏的文件所以这里需要将这两个文件排除*/
{
continue;
}
struct node *p = (struct node *)malloc(sizeof(struct node));
if( head == NULL)
{
strcpy(p->name,myitem->d_name);
head = p;
head->prior = NULL;
head->next = head;
last = p;
}
else
{
strcpy(p->name,myitem->d_name);
last->next = p;
p->prior = last;
last = p;
last->next = head;
}
}
head->prior = last;
closedir(mydir);
}
void display_node()
{
struct node *p = head;
while(p->next != head)
{
printf("%s\t",p->name);
p = p->next;
}
printf("%s\n",p->name);
}
void printfile()
{
int x;
printf("请选择功能\n");
printf("1输出上个文件\n");
printf("2输出下个文件\n");
printf("3正常退出\n");
struct node *p = head;
printf("此刻第一个文件为: %s\n",p->name);
while(1)
{
printf("input\n");
scanf("%d",&x);
switch(x)
{
case 1:
{
p = p->prior;
printf("上一个文件为:%s\n",p->name);
break;
}
case 2:
{
p=p->next;
printf("下一个文件是:%s\n",p->name);
break;
}
case 3:
{
printf("正常退出\n");
exit(0);
}
}
}
}
int main()
{
cre_list();
display_node();
printfile();
return 0;
}
文件检索主要还是建立在双向循环链表之上的,就是把一个目录下的文件有几个算几个串连起来形成一个双向链表,再通过移动指针来查看上一个或者是下个文件。感觉这个做起来 在目录加进来那个地方还是有点不太理解的。。。
相关文章推荐
- 数据库复习总结(7)-表的创建以及插入命令、数据修改、数据删除
- 第十四周项目一数组大折腾(5)创建一个长度为16的整形数组a并初始化前15个元素,输入一个整数b,将b插入到a[0]位置(原有的数据往后“移动”)
- 单链表 初始化 创建 头插法 尾插法 插入 删除 查找 合并 长度
- java实现hbase表创建、数据插入、删除表
- 链表(LinkList)的设计与实现(初始化,创建,插入,删除,逆置)
- java实现单链表的初始化,创建,删除,插入,查找,排序,同项删除,退出等功能
- MySQL--操作简记(create创建操纵表、insert插入、update更新、delete删除数据)
- python对MySQL数据库的操作表单的创建,数据的插入,读取,更新,删除操作
- hive中创建子表并插入数据过程初始化MR报错解决方法
- 数据库 创建数据库 ,表,表中插入数据,以及表、无用字段的删除。
- Java连接HBASE数据库,创建一个表,删除一张表,修改表,输出插入,修改,数据删除,数据获取,显示表信息,过滤查询,分页查询,地理hash
- 在mysql数据库中创建oracle scott用户的四个表及插入初始化数据
- 数据结构中顺序栈的基本函数运用,包括创建空栈,初始化,清空,销毁,增加,删除,遍历等函数
- 单链表的创建、初始化、插入、删除 、输出、求表长、清空、查找操作
- 循环链表 实现对循环链表的初始化,创建,插入,删除,输出操作
- 在mysql数据库中创建oracle scott用户的四个表及插入初始化数据
- 创建临时表,作用:插入数据,取数据,取完数据自动删除。
- 数据结构:头插法、尾插法创建单链表,求链表长度、查找、插入、删除、合并
- 数据结构:单链表(一)的创建、插入、删除、遍历,打印
- 4000 修改CSpreadSheet commit函数,让其插入新行时不再重复删除 创建 写数据