c語言鏈表遍歷
❶ 如何用c語言輸出整個單鏈表中的數據
單鏈表中頭結點有兩個作用:一是標識該鏈表的存在,而是可以通過頭結點遍歷整個鏈表。所以不能通過移動頭結點指針遍歷鏈表,因為一旦移動了,下次就無法定位該鏈表了!
void dispList(LinkList *L)
{
LinkList *p=L->next;//定義一個結點指針p指向頭結點的下一個結點
while(p){ //如果p不為空則循環
printf("%d",p->data);
p=p->next;//移動指針p遍歷鏈表
}
}
❷ 求C語言單鏈表倒序遍歷程序~
額。。寫完了才發現好像題目意思理解錯了,是倒序遍歷啊,不過我已經把整個鏈表倒過來了,直接遍歷即可,遍歷完了可以再倒回去。。。= =要不你就按原本的順序遍歷,每次都插入到最前面,這樣就新建一個鏈表和原本順序相反,就可以了~
========================================================
#include <stdio.h>
#include <stdlib.h>
typedef struct _node {
int value;
struct _node *next;
} node;
node * make_node(int value) {
node *new_node = (node *) malloc(sizeof(node));
new_node->value = value;
new_node->next = 0;
return new_node;
}
node * add_after(node *pos, int value) {
node *new_node = make_node(value);
pos->next = new_node;
return new_node;
}
void print_node_list(node *head) {
while (head) {
printf("%d ", head->value);
head = head->next;
}
printf("\n");
}
void free_node_list(node *head) {
node *temp;
while (head) {
temp = head;
head = head->next;
free(temp);
}
}
node * reverse(node *head) {
node *f = 0, *s = 0;
while (head) {
f = s;
s = head;
head = head->next;
s->next = f;
}
return s;
}
int main() {
int i = 0;
node *head = make_node(i), *last = head;
while (i < 10) {
last = add_after(last, ++i);
}
print_node_list(head);
head = reverse(head);
print_node_list(head);
free_node_list(head);
return 0;
}
❸ C語言單鏈表遍歷時出錯,我不知道錯在哪,求高人指點
//CreatList_L要改一一下
void CreatList_L(LinkList &L,int n) //創建鏈表
{
L=(LNode*)malloc(sizeof(LNode));
LNode *current;
current=L;
printf("請輸入第1個數據:\n");
scanf("%d",&L->data);
for(int i=1;i<n;i++)
{
LNode *Newnode=(LNode*)malloc(sizeof(LNode));
printf("請輸入第%d個數據:\n",i+1);
scanf("%d",&Newnode->data);
current->next=Newnode;
current=current->next;
}
current->next=NULL;
}
❹ 用C語言編寫程序建立鏈表結構體類型實現鏈表初始化遍歷和插入演算法
#include <stdio.h>
#include <stdlib.h>
#define telemtype char
#define ok 1
#define error 0
#define overflow -1
typedef int status;
typedef struct bitnode
{
telemtype data;
struct bitnode *lchild,*rchild;
}bitnode,*bitree;
void preordertraverse(bitree T)
{
if(T)
{
printf("%c ",T->data);
preordertraverse(T->lchild);
preordertraverse(T->rchild);
}
}
status createbitree(bitree &T)
{
int ch;
ch=getchar();
if(ch==' ')
T=NULL;
else
{
if(!(T=(bitnode*)malloc(sizeof(bitnode))))
exit(overflow);
T->data=ch;
createbitree(T->lchild);
createbitree(T->rchild);
}
return ok;
}
void prinbtree(bitree T)
{
if(T!= NULL)
{
printf("%c", T->data);
if(T->lchild!=NULL||T->rchild!=NULL)
{
printf("(");
prinbtree(T->lchild);
if(T->rchild!=NULL)
{
printf(",");
}
prinbtree(T->rchild);
printf(")");
}
}
}
int main()
{
bitree T=NULL;
printf("先序輸入二叉樹:\n");
createbitree(T);
printf("先序遍歷二叉樹為:\n");
preordertraverse(T);
printf("\n");
prinbtree(T);
printf("\n");
return 0;
}
我寫的,希望對你有用!
❺ c語言,鏈表遍歷函數,題目看不懂,求代碼,如圖
int sum(Lis head)
{
int s=0;
while(head->next)
{
head=head->next;
s+=head->v;
}
return s;
}
❻ 如何用c語言輸出整個單鏈表中的數據
單鏈表中頭結點有兩個作用:一是標識該鏈表的存在,而是可以通過頭結點專遍歷整個鏈表。所以不屬能通過移動頭結點指針遍歷鏈表,因為一旦移動了,下次就無法定位該鏈表了!
void dispList(LinkList *L)
{
LinkList *p=L->next;//定義一個結點指針p指向頭結點的下一個結點
while(p){ //如果p不為空則循環
printf("%d",p->data);
p=p->next;//移動指針p遍歷鏈表
}
}
❼ 關於c語言鏈表輸入與遍歷的問題
//幫你調試好了,vc6 pass
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void ScanList(struct node *q)
{
struct node *m,*t;
m=q;
int x;
scanf("%d\n",&x);
while(true)
{
if(x==-1)//輸入-1 結束整個程序
break;
else
{
t=(struct node*)malloc(sizeof(node));
t->data=x;
m->next=t;
m=t;
t->next=NULL;
}
scanf("%d",&x);
}
}
void PrintList(struct node *q)
{
struct node *m;
m=q;
m=m->next;//帶有head頭節點的鏈表,頭節點不存放數據
while(m!=NULL)
{
printf("%d\n",m->data);
m=m->next;
}
}
void main()
{
struct node *hl;
hl= (struct node*)malloc(sizeof(node));
ScanList(hl);
PrintList(hl);
}
❽ c語言遍歷鏈表問題
B通過的條件是B不為空,既B的值不是0x000000。。只要滿足這個,B都可以進行循環。
通常回的錯誤,B雖然已答經釋放的空間,但是並沒有賦給它0x0000000導致B成為野指針,野指針可以通過!=NULL的判斷,但是他的空間是不能操作的。
❾ c語言遍歷是什麼意思
c語言遍歷是指沿著某條搜索路線,依次對樹(或圖)中每個節點均做一次訪問。訪版問結點所做的權操作依賴於具體的應用問題, 具體的訪問操作可能是檢查節點的值、更新節點的值等。不同的遍歷方式,其訪問節點的順序是不一樣的。遍歷是是c語言上進行其它運算之基礎。
(9)c語言鏈表遍歷擴展閱讀:
由於從給定的某個節點出發,有多個可以前往的下一個節點,所以在順序計算(即非並行計算)的情況下,只能推遲對某些節點的訪問——即以某種方式保存起來以便稍後再訪問。常見的做法是採用棧(LIFO)或隊列(FIFO)。
由於樹本身是一種自我引用(即遞歸定義)的數據結構,因此很自然也可以用遞歸方式,或者更准確地說,用corecursion,來實現延遲節點的保存。這時(採用遞歸的情況)這些節點被保存在call stack中。
❿ 數據結構鏈表遍歷C語言
求點贊!#include"stdio.h"
#include"stdlib.h"
#defineNULL0
#defineError0
typedefstructLNode{
intdata;
structLNode*next;
}LNode,*LinkList;
LinkListCreatList(LinkList,int);
LinkListCreatList(LinkListL,intn)
{
LinkListp;
inti;
L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
for(i=n;i>0;--i){
p=(LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next=L->next;
L->next=p;
}
returnL;
}
voidGetelem(LinkListL)
//遍歷鏈表
{
LinkListq;
q=L->next;
for(q->next;q;q=q->next)
printf("%d",q->data);
}
voidmain(){
LinkListL;
inta;
puts("請輸入鏈表長度:");
scanf("%d",&a);
L=CreatList(L,a);//L要接收函數返回指針
Getelem(L);
}