① c语言编写程序实现图的遍历操作
楼主你好,下面是源程序!
/*/////////////////////////////////////////////////////////////*/
/* 图的深度优先遍历 */
/*/////////////////////////////////////////////////////////////*/
#include <stdlib.h>
#include <stdio.h>
struct node /* 图顶点结构定义 */
{
int vertex; /* 顶点数据信息 */
struct node *nextnode; /* 指下一顶点的指标 */
};
typedef struct node *graph; /* 图形的结构新型态 */
struct node head[9]; /* 图形顶点数组 */
int visited[9]; /* 遍历标记数组 */
/********************根据已有的信息建立邻接表********************/
void creategraph(int node[20][2],int num)/*num指的是图的边数*/
{
graph newnode; /*指向新节点的指针定义*/
graph ptr;
int from; /* 边的起点 */
int to; /* 边的终点 */
int i;
for ( i = 0; i < num; i++ ) /* 读取边线信息,插入邻接表*/
{
from = node[i][0]; /* 边线的起点 */
to = node[i][1]; /* 边线的终点 */
/* 建立新顶点 */
newnode = ( graph ) malloc(sizeof(struct node));
newnode->vertex = to; /* 建立顶点内容 */
newnode->nextnode = NULL; /* 设定指标初值 */
ptr = &(head[from]); /* 顶点位置 */
while ( ptr->nextnode != NULL ) /* 遍历至链表尾 */
ptr = ptr->nextnode; /* 下一个顶点 */
ptr->nextnode = newnode; /* 插入节点 */
}
}
/********************** 图的深度优先搜寻法********************/
void dfs(int current)
{
graph ptr;
visited[current] = 1; /* 记录已遍历过 */
printf("vertex[%d]\n",current); /* 输出遍历顶点值 */
ptr = head[current].nextnode; /* 顶点位置 */
while ( ptr != NULL ) /* 遍历至链表尾 */
{
if ( visited[ptr->vertex] == 0 ) /* 如过没遍历过 */
dfs(ptr->vertex); /* 递回遍历呼叫 */
ptr = ptr->nextnode; /* 下一个顶点 */
}
}
/****************************** 主程序******************************/
void main()
{
graph ptr;
int node[20][2] = { {1, 2}, {2, 1}, /* 边线数组 */
{1, 3}, {3, 1},
{1, 4}, {4, 1},
{2, 5}, {5, 2},
{2, 6}, {6, 2},
{3, 7}, {7, 3},
{4, 7}, {4, 4},
{5, 8}, {8, 5},
{6, 7}, {7, 6},
{7, 8}, {8, 7} };
int i;
clrscr();
for ( i = 1; i <= 8; i++ ) /* 顶点数组初始化 */
{
head[i].vertex = i; /* 设定顶点值 */
head[i].nextnode = NULL; /* 指针为空 */
visited[i] = 0; /* 设定遍历初始标志 */
}
creategraph(node,20); /* 建立邻接表 */
printf("Content of the gragh's ADlist is:\n");
for ( i = 1; i <= 8; i++ )
{
printf("vertex%d ->",head[i].vertex); /* 顶点值 */
ptr = head[i].nextnode; /* 顶点位置 */
while ( ptr != NULL ) /* 遍历至链表尾 */
{
printf(" %d ",ptr->vertex); /* 印出顶点内容 */
ptr = ptr->nextnode; /* 下一个顶点 */
}
printf("\n"); /* 换行 */
}
printf("\nThe end of the dfs are:\n");
dfs(1); /* 打印输出遍历过程 */
printf("\n"); /* 换行 */
puts(" Press any key to quit...");
getch();
}
/*//////////////////////////////////////////*/
/* 图形的广度优先搜寻法 */
/* ///////////////////////////////////////*/
#include <stdlib.h>
#include <stdio.h>
#define MAXQUEUE 10 /* 队列的最大容量 */
struct node /* 图的顶点结构定义 */
{
int vertex;
struct node *nextnode;
};
typedef struct node *graph; /* 图的结构指针 */
struct node head[9]; /* 图的顶点数组 */
int visited[9]; /* 遍历标记数组 */
int queue[MAXQUEUE]; /* 定义序列数组 */
int front = -1; /* 序列前端 */
int rear = -1; /* 序列后端 */
/***********************二维数组向邻接表的转化****************************/
void creategraph(int node[20][2],int num)
{
graph newnode; /* 顶点指针 */
graph ptr;
int from; /* 边起点 */
int to; /* 边终点 */
int i;
for ( i = 0; i < num; i++ ) /* 第i条边的信息处理 */
{
from = node[i][0]; /* 边的起点 */
to = node[i][1]; /* 边的终点 */
/* 建立新顶点 */
newnode = ( graph ) malloc(sizeof(struct node));
newnode->vertex = to; /* 顶点内容 */
newnode->nextnode = NULL; /* 设定指针初值 */
ptr = &(head[from]); /* 顶点位置 */
while ( ptr->nextnode != NULL ) /* 遍历至链表尾 */
ptr = ptr->nextnode; /* 下一个顶点 */
ptr->nextnode = newnode; /* 插入第i个节点的链表尾部 */
}
}
/************************ 数值入队列************************************/
int enqueue(int value)
{
if ( rear >= MAXQUEUE ) /* 检查伫列是否全满 */
return -1; /* 无法存入 */
rear++; /* 后端指标往前移 */
queue[rear] = value; /* 存入伫列 */
}
/************************* 数值出队列*********************************/
int dequeue()
{
if ( front == rear ) /* 队列是否为空 */
return -1; /* 为空,无法取出 */
front++; /* 前端指标往前移 */
return queue[front]; /* 从队列中取出信息 */
}
/*********************** 图形的广度优先遍历************************/
void bfs(int current)
{
graph ptr;
/* 处理第一个顶点 */
enqueue(current); /* 将顶点存入队列 */
visited[current] = 1; /* 已遍历过记录标志置疑1*/
printf(" Vertex[%d]\n",current); /* 打印输出遍历顶点值 */
while ( front != rear ) /* 队列是否为空 */
{
current = dequeue(); /* 将顶点从队列列取出 */
ptr = head[current].nextnode; /* 顶点位置 */
while ( ptr != NULL ) /* 遍历至链表尾 */
{
if ( visited[ptr->vertex] == 0 ) /*顶点没有遍历过*/
{
enqueue(ptr->vertex); /* 奖定点放入队列 */
visited[ptr->vertex] = 1; /* 置遍历标记为1 */
printf(" Vertex[%d]\n",ptr->vertex);/* 印出遍历顶点值 */
}
ptr = ptr->nextnode; /* 下一个顶点 */
}
}
}
/*********************** 主程序 ************************************/
/*********************************************************************/
void main()
{
graph ptr;
int node[20][2] = { {1, 2}, {2, 1}, /* 边信息数组 */
{6, 3}, {3, 6},
{2, 4}, {4, 2},
{1, 5}, {5, 1},
{3, 7}, {7, 3},
{1, 7}, {7, 1},
{4, 8}, {8, 4},
{5, 8}, {8, 5},
{2, 8}, {8, 2},
{7, 8}, {8, 7} };
int i;
clrscr();
puts("This is an example of Width Preferred Traverse of Gragh.\n");
for ( i = 1; i <= 8; i++ ) /*顶点结构数组初始化*/
{
head[i].vertex = i;
head[i].nextnode = NULL;
visited[i] = 0;
}
creategraph(node,20); /* 图信息转换,邻接表的建立 */
printf("The content of the graph's allist is:\n");
for ( i = 1; i <= 8; i++ )
{
printf(" vertex%d =>",head[i].vertex); /* 顶点值 */
ptr = head[i].nextnode; /* 顶点位置 */
while ( ptr != NULL ) /* 遍历至链表尾 */
{
printf(" %d ",ptr->vertex); /* 打印输出顶点内容 */
ptr = ptr->nextnode; /* 下一个顶点 */
}
printf("\n"); /* 换行 */
}
printf("The contents of BFS are:\n");
bfs(1); /* 打印输出遍历过程 */
printf("\n"); /* 换行 */
puts(" Press any key to quit...");
getch();
}
② C语言链表的使用方法
D
答案D设置完,p就从链表中丢掉了。
p就是一个指向结构体node的指针。
p->next就是p包含的执行下一个node的指针,在本题,就是q。
③ 谁能提供一个C语言结构体实现链表的例子,代码能直接运行的
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
typedef struct Node
{
int key;
Node * next;
}Node; //定义结构
int main()
{
int *a,n,i;
Node* L0,*p;
scanf("%d",&n);//输入链表长度
a=(int*)malloc(sizeof(int)*n);//给数组a动态分配n个空间
L0=(Node*)malloc(sizeof(Node));
L0->next=NULL; //建立头结点
srand((unsigned)time( NULL ) ); //与rand()函数一起使用
for(i=0;i<n;i++)
{
a[i]=rand()%100; //产生100以内的随机数,也可以自己输入
}
for(i=0;i<n;i++)
{
p=(Node*)malloc(sizeof(Node));
p->key=a[i]; //分配一个结点空间,并赋值
p->next=L0->next;
L0->next=p; //连接单链表,这里是精髓
}
while(L0->next)
{
L0=L0->next;
printf("%d ",L0->key); //遍历单链表
}
return 0;
}
还是自己把链表弄懂吧,很有意思的,也是数据结构的基础内容。
④ c语言建立动态链表
struct student
{
long no;
char name[10];
int age;
struct student *next;
};
struct student *head = NULL;
void Add(struct student *stu)
{
struct student *p,*q;
if(head == NULL)
head = stu;
else
{
p = head;
while(p != NULL)
{
q = p;
p = p->next;
}
q ->next = stu;
}
}
void Print()
{
struct student *p = head,i = 0;
if(p == NULL) return ;
while(p != NULL)
{
printf("学生 %d\n",i+1);
printf("学号:%ld\n",p->no);
printf("姓名:%s\n",p->name);
printf("年龄:%d\n",p->age);
p = p->next;
i++;
}
}
void Destroy()
{
struct student *p = head,*q;
if(head == NULL) return ;
while(p!= NULL)
{
q = p ->next;
free(p);
p = q;
}
head = NULL;
}
调用:
int main()
{
int i ;
struct student *stu = (struct student*)malloc(sizeof(struct student)*4);
for(i = 0;i < 4;i++)
{
scanf("%ld",&stu[i]->no);
scanf("%s",stu[i]->name);
scanf("%d",&stu[i]->age);
stu->next = NULL;
Add(&stu[i]);
}
Print();
getch();
return 0;
}
⑤ C语言中链表的原理该如何使用坐等指教 多谢啦
一楼的答案很标准了,我再通俗地说明下-----你把链表看成一辆火车,火车头就是头结点,然后每节车厢有号码标识,比如1号车厢车尾就连上2号车厢车头,这就像1号节点的指针域里面存储的是下一个节点的地址,每节车厢里面容纳乘客的地方就相当于数据域。
至于链表的种类有很多,有单链表的,双连表的,循环链表的,还有有头结点和无头节点的。总之原理都大同小异。
对于链表的操作主要有插入和删除:
插入:在第i个节点之前插入一个节点:
1、首先查找第i-1个节点 if(p满足条件)
2、给新节点分配空间q =(Node*)malloc(sizeof(Node));给q数据域赋值。
3、q->next = p->next;
4、p->next = q;
删除:如果要删除符合条件的节点(删除q所指节点)
1、首先找到要删除节点的前驱节点 if(p->next->data满足条件)
2、用q指向要删除的节点 q = p->next;
3、p->next = q->next;
4、free(q);
说再多不如你实际操作,你可以写一个很简单的链表数据试试...
希望对你有所帮助!
⑥ 关于C语言链表的操作实例
一般连表程序在c语言中要用link list来实现,我贴一个我写的程序厅枯指在这里,可以运行,这个程序里包含一个structer纪录学生信息,学生号码已极学生姓名,纪录通过insert_node添加,通过delete_node删除,并在最开始的时候通扮配过create list function来创建最原始的数据,不用理会里面的reverse function。
#include <stdio.h>
#include <stdlib.h>
struct list
{
int num;
char name[10];
struct list *next;
};
typedef struct list node;
typedef node *link;
link find_node_loc(link ptr, link ptr1, int reversed)
{
link ptr0=ptr;
link ptr2=NULL;
if(reversed)
{
while(ptr0!=NULL && (ptr0->num > ptr1->num))
{
ptr2=ptr0;
ptr0=ptr0->next;
}
}
else
{
while(ptr0!=NULL && (ptr0->num < ptr1->num))
{
ptr2=ptr0;
ptr0=ptr0->next;
}
}
return ptr2;
}
link find_node(link head,int IDnum)
{
link ptr;
ptr=head;
while (ptr!=NULL)
{
if(ptr->num==IDnum)
return ptr;
ptr=ptr->next;
}
return ptr;
}
void free_list(link head)
{
link ptr;
while(head!=NULL)
{
ptr=head;
head=head->next;
free(ptr);
}
}
link create_list()
{
link insert_node( link head, link ptr, link newnode);
link p1,p2;
link head;
link ptrf;
int panan=1;
int number=1;
int reversed=0;
head=(link)malloc(sizeof(node));
if(!head){
printf("Memory Allocation Fail! \n");
exit(1);
}
else{
head->num=NULL;
printf("败郑Please enter the %d student ID==> ",number);
scanf("%d",&panan);
while(panan!=0){
if (number==1){
head->num=panan;
printf("Please enter the new student name==> ");
scanf("%s",&head->name);
head->next=NULL;
number++;
}
else{
printf("Please enter the %d student ID==> ",number);
scanf("%d",&panan);
if(panan!=0){
p1=(link)malloc(sizeof(node));
p1->num=panan;
printf("Please enter the new student name==> ");
scanf("%s",&p1->name);
p1->next=NULL;
number++;
p2=p1;
ptrf = find_node_loc(head, p2, 0);
head=insert_node(head,ptrf,p2);
}
}
}
return head;
}
}
void print_list(link ptr)
{
int i=1;
while(ptr!=NULL){
printf("Student %d\n",i++);
printf("ID number:%d\n",ptr->num);
printf("Student name:%s\n",ptr->name);
printf("\n");
ptr=ptr->next;
}
}
link insert_node(link head,link ptr, link newnode)
{
link pt1;
if (ptr!=NULL){
if(ptr->next!=NULL){
pt1=ptr->next;
ptr->next=newnode;
newnode->next=pt1;}
else{
ptr->next=newnode;
newnode->next=NULL;
}
}
else{
pt1=head;
head=newnode;
head->next=pt1;
}
return head;
}
link reverse(link newhead,int reversed){
link ptr1,ptr2,newhead1;
newhead1=newhead;
do{
newhead=newhead->next;
}while(newhead->next!=NULL);
do{
ptr1=newhead1;
newhead1=newhead1->next;
ptr2=find_node_loc(newhead,ptr1,reversed%2);
newhead=insert_node(newhead,ptr2,ptr1);
}while(newhead1!=newhead);
return newhead;
}
link delete_node(link head, link ptr,link ptr1)
{
link ptr2;
if(ptr!=head){
if(ptr->next==NULL){
ptr1->next=NULL;
free(ptr);
}
else{
ptr2=ptr->next;
free(ptr);
ptr1->next=ptr2;
}
}
else{
head=ptr->next;
free(ptr);
}
return head;
}
int main()
{
link head,ptr,newnode,ptr1;
int flag=1,reversed=0;
int IDnum,i;
head=create_list();
if(head->num==NULL){
printf("You didnt create a list,try again\n");
getchar();
getchar();
}
else{
ptr=head;
printf("\n\nStudent Records Created!\n\n");
flag=1;
while(flag)
{
printf("\n******************************************\n");
printf("1: Insert new student record\n");
printf("2: Delete student record\n");
printf("3: Display students record\n");
printf("4: Reverse student record\n");
printf("0: Quit\n");
printf("\nPlease select a option to maintain the student record:\n");
scanf("%d",&i);
switch(i)
{
case 0:
{
flag=0;
break;
}
case 1:
{
newnode=(link)malloc(sizeof(node));
if(!newnode)
{
printf("Memory Allocation Fail! \n");
exit(1);
}
else
newnode->next=NULL;
printf("Please enter the new student ID==> ");
scanf("%d",&newnode->num);
printf("Please enter the new student name==> ");
scanf("%s",&newnode->name);
ptr = find_node_loc(head, newnode, reversed%2);
head = insert_node(head, ptr, newnode);
if (!head)
{
printf("Memory Allocation Fail! \n");
exit(1);
}
printf("\n**********************************************");
printf("\nStudent record after insertion:\n\n");
print_list(head);
printf("**********************************************");
break;
}
case 2:
{
printf("reversed:%d",reversed);
printf("Which student you want to delete? (ID)==>");
scanf("%d",&IDnum);
if(IDnum)
{
ptr=find_node(head,IDnum);
ptr1=find_node_loc(head,ptr,reversed%2);
if(!ptr)
printf("no record\n");
else
{
head=delete_node(head,ptr,ptr1);
printf("\n**********************************************");
printf("\nStudent record after deleted node:\n");
print_list(head);
printf("**********************************************");
}
break;
}
else
exit(1);
}
case 3:
{
print_list(head);
break;
}
case 4:
reversed++;
head=reverse(head,reversed);
print_list(head);
break;
default:
printf("Wrong input! Please input a number in (0-3)");
}
}
}
return 0;
}
⑦ c语言建立动态链表,我刚学编的程序,请高人帮忙指出毛病
1. 第一个for循环结构中
scanf("%d,%s",p2->num,p2->name);
改为 scanf("%d,%s",&(p2->num),p2->name);
p1=p2->next;
改为 p2->next=p1;
2.第二个for循环结构中
struct stu *p;
p=head; 每一次循环指针p都指向head
应该把他定义到for外面,赋值为head
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define NULL 0
void main()
{
struct stu
{int num;
char name[10];
struct stu *next;
};
struct stu *head,*p1,*p2,*p;
int n;
p1=(struct stu*)malloc(sizeof(struct stu));
head=p1;
for(n=0;n<=3;n++)
{p2=p1;
printf("input:");
scanf("%d,%s",&(p2->num),p2->name);
p1=(struct stu*)malloc(sizeof(struct stu));
p2->next=p1;
}
p1->next=NULL;
p=head;
for(n=0;n<=3;n++)
{
printf("%d,%s\n",p->num,p->name);
p=p->next;
}
getch();
}
后面这个for结构可以改为
do
{
printf("%d,%s\n",p->num,p->name);
p=p->next;
}while(p->next!=NULL);
⑧ C# 中 如何让图片框带滚动条,显示较大的图片
首先设置窗体的AutoScroll属性为True
然后拖一个PictureBox控件上来
再拖一个ToolStripMenu上来,并添加菜单项 “文件”—“打开”
双击"打开"菜单,转到其click事件处理函数中来添加如下代码:
OpenFileDialog openFileDlg = new OpenFileDialog();
openFileDlg.Filter = "*.jpg|*.jpg;*.jpeg";
if (openFileDlg.ShowDialog() == DialogResult.OK)
{
Image img = Image.FromFile(openFileDlg.FileName);
this.pictureBox1.Size = img.Size;
this.pictureBox1.Image = img;
}
openFileDlg.Dispose();