當前位置:首頁 » 編程語言 » c語言圖片的滾動鏈表實現
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言圖片的滾動鏈表實現

發布時間: 2023-03-30 00:08:26

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();