您的位置:首页 > 理论基础 > 数据结构算法

苏嵌 暑假实训之第一天之数据结构单链双链表的初始化创建插入中间删除表之篇章。。。。

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;
}
文件检索主要还是建立在双向循环链表之上的,就是把一个目录下的文件有几个算几个串连起来形成一个双向链表,再通过移动指针来查看上一个或者是下个文件。感觉这个做起来 在目录加进来那个地方还是有点不太理解的。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐