A. c語言指針鏈表具體怎麼弄
#include<stdlib.h>/*含malloc()的頭文件*/
#include<stdio.h>
//①定義鏈表數據結構
structnode
{
intnum;
structnode*next;
};
//函數聲明
structnode*creat();
voidprint();
main()
{
structnode*head;
head=NULL;//②建一個空表
head=creat(head);/*創建單鏈表*/
print(head);/*列印單鏈表*/
}
/******************************************/
structnode*creat(structnode*head)/*返回的是與節點相同類型的指針*/
{
structnode*p1,*p2;
inti=1;
//③利用malloc()函數向系統申請分配一個節點
p1=p2=(structnode*)malloc(sizeof(structnode));/*新節點*/
printf("請輸入值,值小於等於0結束,值存放地址為:p1_ADDR=%d ",p1);
scanf("%d",&p1->num);/*輸入節點的值*/
p1->next=NULL;/*將新節點的指針置為空*/
while(p1->num>0)/*輸入節點的數值大於0*/
{
//④將新節點的指針成員賦值為空。若是空表,將新節點連接到表頭;若是非空表,將新節點接到表尾;
if(head==NULL)
head=p1;/*空表,接入表頭*/
else
p2->next=p1;/*非空表,接到表尾*/
p2=p1;
p1=(structnode*)malloc(sizeof(structnode));/*下一個新節點*/
i=i+1;
printf("請輸入值,值小於等於0結束,值存放地址為:p%d_ADDR=%d ",i,p2);
scanf("%d",&p1->num);/*輸入節點的值*/
//⑤判斷一下是否有後續節點要接入鏈表,若有轉到3),否則結束;
}
//==============原來程序更正部分:(多謝@daling_datou提醒)================================
free(p1);//申請到的沒錄入,所以釋放掉
p1=NULL;//使指向空
p2->next=NULL;//到表尾了,指向空
printf("鏈表輸入結束(END) ");
//==============================================
returnhead;/*返回鏈表的頭指針*/
}
/*******************************************/
voidprint(structnode*head)/*出以head為頭的鏈表各節點的值*/
{
structnode*temp;
temp=head;/*取得鏈表的頭指針*/
printf(" 鏈表存入的值為: ");
while(temp!=NULL)/*只要是非空表*/
{
printf("%6d ",temp->num);/*輸出鏈表節點的值*/
temp=temp->next;/*跟蹤鏈表增長*/
}
printf("鏈表列印結束!!");
}
B. 有沒有數據結構(c語言版)嚴蔚敏老師教學視頻下載地址
哥們兒 你可以在網路里收索 《數據結構C語言版》 嚴蔚敏老師40多個課時的視頻都在
C. 誰有譚浩強《C語言程序設計》第四版的教學視頻麻煩了
譚浩強【004】
鏈接:
若資源有問題歡迎追問~
D. C語言 鏈表操作
#include <stdio.h>
#include <malloc.h>
int m;
struct Node
{
int data;
struct Node *next;
}* listA, *listB;
//列印AList列蘆含棗表
//參數AList為顯示的列表
void printList(struct Node *AList)
{
struct Node *post;
post = AList->next;
while (post)
{
printf("%d ",post->data);
post = post->next;
}
printf("\n");
}
//初始化表頭,列表含有表頭
//參數AList為初始化的列表
void init(struct Node **AList)
{
*AList = (struct Node*)malloc(sizeof(struct Node));
(*AList)->data = 0;
(*AList)->next = 0;
}
//創建列表
//參數AList為要創建的列表
void ReadList(struct Node **AList)
{
int i;
struct Node *post;
struct Node *pre;
// 輸入列表長度
printf("請輸入列表的長度為:"); scanf("%d",&m);
//讀取列表A
pre = *AList;
for(i=1; i<=m; i++)
{
post = (struct Node*)malloc(sizeof(struct Node));
printf("第%d個節點值為:"老磨, i);
scanf("%d", &post->data);
post->next = 0;
pre->next = post;
pre = post;
}
}
//插入節點
//參數AList為要插入的列表
//參數Aindex為要插入的節點的位置
void InsertNode(struct Node **AList, int Aindex = -1)
{
int i;
struct Node *pre, *post;
pre = *AList;
//判斷插入位置,默認為頭部插入
if((Aindex>0) && (Aindex<=m))
{
post = (struct Node*)malloc(sizeof(struct Node));
printf("請輸入要插入的節點值為:");
printf("listN%d = ", Aindex);
scanf("%d", &post->data);
post->next = 0;
//尋找插入點
i = 1;
pre = *AList;
while ( i<Aindex )
{
pre = pre->next;
i = i + 1;
}
//插入節點
post->next = pre->next;
pre->next = post;
}
else //插入到頭部
{
post = (struct Node*)malloc(sizeof(struct Node));
printf("listA1 = ");
scanf("%d", &post->data);
post->next = 0;
//插入節點
post->next = pre->next;
pre->陪拆next = post;
}
m = m + 1;
}
//刪除節點
//參數AList為要刪除的列表
//參數Aindex為要刪除的節點的位置
void DeleteNode(struct Node **AList, int Aindex)
{
int i;
struct Node *pre, *post;
pre = *AList;
//判斷刪除位置
if((Aindex>0) && (Aindex<=m))
{
//尋找刪除點
i = 1;
while ( i<Aindex )
{
pre = pre->next;
i = i + 1;
}
//賦值要刪除節點
post = pre->next;
pre->next = post->next;
delete post;
m = m - 1;
}
else //輸入越界
{
printf("不存在此節點");
}
}
//列表存檔
//參數AList為要存檔的列表
int WriteFile(struct Node *AList)
{
struct Node *post;
FILE *fd;
fd = fopen("data.txt", "wt");
if (fd == NULL)
{
printf("File open error");
return 0;
}
post = AList->next;
while (post)
{
fprintf(fd, "%d ", post->data);
post = post->next;
}
fclose(fd);
return 1;
}
//使用文件創建列表
//參數AList為要創建的列表
int ReadFile(struct Node **AList)
{
struct Node *pre, *post;
FILE *fd;
fd = fopen("data.txt", "rb");
if (fd == NULL)
{
printf("File open error");
return 0;
}
//讀取列表
pre = *AList;
while (!feof(fd))
{
post = (struct Node*)malloc(sizeof(struct Node));
fscanf(fd, "%d ", &post->data);
post->next = 0;
pre->next = post;
pre = post;
}
fclose(fd);
return 1;
}
void main(void)
{
//listA使用輸入創建
//listB使用讀取文件創建
init(&listA);
init(&listB);
ReadList(&listA);
printf("輸入的列表為:");
printList(listA);
InsertNode(&listA, 1);
printf("插入第一個節點後的列表為:");
printList(listA);
DeleteNode(&listA, 2);
printf("刪除第二個節點後的列表為:");
printList(listA);
WriteFile(listA);
ReadFile(&listB);
printf("使用文件創建的列表為:");
printList(listB);
}
以上程序可以直接運行,且結果正確。
主函數可以再做的復雜一些和友好一些。刪除節點的位置和插入節點的位置可以作為輸入之類的。
希望對你有所幫助
E. c語言中的鏈表:
##include <stdio.h>
#include <stdlib.h>
#define N 8
typedef struct list //定義鏈表節點類型,list是鏈表名,typedef是定義新類型的關鍵字
{
int data; // 定義數據域
struct list *next; // 定義鏈域
} SLIST;
void fun( SLIST *p)
{
SLIST *t, *s;
t=p->next; s=p;
while(t->next != NULL) //判斷指針t的鏈域是否為空,如果為空則表明已到鏈表的尾部
{ s=t; //s用來暫存結點信息,當循環結束時,s保存的是尾結點
/**********found**********/
t=t->next;
}
/**********found**********/
printf(" %d ",t->data);
s->next=NULL;
/**********found**********/
free(t); //釋放尾結點
}
SLIST *creatlist(int *a) // creatlist用來創建鏈表 ,將數組a中的各個元素依次存入新分配的存儲單元中
{ SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST)); // 申請頭結點,將申請到的存儲空間的首地址賦給指針p和h
for(i=0; i<N; i++)
{ q=(SLIST *)malloc(sizeof(SLIST)); //申請新節點,並將數組的內容存到節點的鏈域中
q->data=a[i]; p->next=q; p=q; // 指針p相當於一個游標,每次申請新結點後就自動往後移一個位置(p=q);
}
p->next=0; //將最後一個結點的鏈域置空
return h; //返回頭結點
}
void outlist(SLIST *h)
{ SLIST *p;
p=h->next;
if (p==NULL) printf("\nThe list is NULL!\n"); //判斷指針p是否為空
else
{ printf("\nHead"); //首先列印顯示頭結點
do { printf("->%d",p->data); p=p->next; } while(p!=NULL); //然後將結點逐個列印出來
printf("->End\n");
}
}
main()
{ SLIST *head;
int a[N]={11,12,15,18,19,22,25,29};
head=creatlist(a); //創建鏈表,並將首地址賦給指針head
printf("\nOutput from head:\n"); outlist(head);
printf("\nOutput from tail: \n");
while (head->next != NULL){
fun(head); //調用函數fun,釋放尾結點
printf("\n\n");
printf("\nOutput from head again :\n"); outlist(head); //列印顯示鏈表的信息
}
}
F. C語言中怎樣建立鏈表
參考以前寫的這個吧,寫的不好,你修改吧
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define CD sizeof(struct Biao)
struct Biao
{
int num;
struct Biao *next;
};
int m,x;
int main()
{
struct Biao *jianli();
void paixu(struct Biao *n);
void chashu(struct Biao *n);
void shanshu(struct Biao *n);
void qiupingjun(struct Biao *n);
void tuichu();
int n;
struct Biao *t;
printf("1.建立鏈表\n2.排序\n3.插數\n4.刪數\n5.求平均值\n");
printf("請輸入選項:\n");
for(;;)
{
scanf("%d",&n);
switch(n)
{
case 1:t=jianli();break;
case 2:paixu(t);break;
case 3:chashu(t);break;
case 4:shanshu(t);break;
case 5:qiupingjun(t);break;
case 6:tuichu();break;
default:printf("輸入錯誤!請重新輸入\n");
}
}
}
struct Biao *jianli()
{
int i,j;
printf("建立鏈表,請輸入數據:\n");
struct Biao *head,*p1,*p2;
p1=p2=(struct Biao * )malloc(CD);//if(p1==NULL)建立鏈表失敗
for(i=0;i<=100;i++)
{
if(i==0)
{
head=p1;
p2->next=p1=(struct Biao * )malloc(CD);
p2=p1;
continue;
}
if(p1==NULL)
break;
else
{
scanf("%d",&p1->num);
if(p1->num==-1)
{
p2->num=NULL;
break;
}
p2->next=p1=(struct Biao * )malloc(CD);
p2=p1;
}
}
if(head->next->num==NULL)
printf("您建立了一個空鏈表\n");
else
{
printf("共有數據:%d\n",i-1);
m=i-1;
printf("輸出鏈表:\n");
p1=head->next;
for(j=0;j<i-1;j++)
{
printf("%d ",p1->num);
p1=p1->next;
}
}
printf("\n");
return(head);
}
void paixu(struct Biao *n)
{
int i,j,a,temp;
a=m;
struct Biao *p1,*p2,*tp;
p2=p1=n->next;
printf("從小到大排序結果:\n");
for(i=0;i<a-1;i++)
{
p1=p2;
tp=p1;
for(j=i+1;j<a;j++)
{
if((p1->next->num)<(tp->num))
{
tp=p1->next;
}
p1=p1->next;
}
temp=tp->num;tp->num=p2->num;p2->num=temp;
p2=p2->next;
}
p1=n->next;
for(i=0;i<a;i++)
{
printf("%d ",p1->num);
p1=p1->next;
}
printf("\n");
}
void chashu(struct Biao *n)
{
int a,i,j;
struct Biao *p1,*p2,*tp;
m=m+1;
x=m;
a=m;
p1=n;
printf("請插入數字:\n");
p2=(struct Biao *)malloc(CD);
scanf("%d",&p2->num);
p1=n->next;
for(i=0;i<a-1;i++)
{
if(p1->num<=p2->num&&p1->next->num>p2->num)
{
tp=p1->next;
p1->next=p2;
p2->next=tp;
break;
}
if(p1->num<p2->num&&p1->next->num>p2->num)
{
tp=p1->next;
p1->next=p2;
p2->next=tp;
break;
}
if(n->next->num>p2->num)
{
tp=n->next;
n->next=p2;
p2->next=tp;
break;
}
p1=p1->next;
}
p1=n;//
for(i=0;i<a-1;i++)
{
p1=p1->next;
}
if(p1->num<=p2->num)
{
tp=p1->next;
p1->next=p2;
p2->next=tp;
}//演算法不簡便
printf("插入後的數據:\n");
p1=n->next;
for(i=0;i<a;i++)
{
printf("%d ",p1->num);
p1=p1->next;
}
printf("\n");
printf("數據個數:%d\n",a);
}
void shanshu(struct Biao *n)
{
int a,i,j;
a=x;
struct Biao *p1,*p2;
printf("請輸入要刪除的數:\n");
scanf("%d",&j);
for(;;)
{
p1=n;
for(i=0;i<a;i++)
{
if(p1->next->num==j)
{
p2=p1->next;
p1->next=p1->next->next;
a-=1;
x-=1;
break;
}
p1=p1->next;
}
if(i==a)
break;
}
printf("結果:\n");
p1=n->next;
for(i=0;i<a;i++)
{
printf("%d ",p1->num);
p1=p1->next;
}
printf("\n");
printf("剩餘數據個數:%d\n",x);
}
void qiupingjun(struct Biao *n)
{
int s,i;
struct Biao *p1;
s=0;
p1=n->next;
for(i=0;i<x;i++)
{
s+=p1->num;
p1=p1->next;
}
printf("平均值為:%f\n",s*1.0/x);
}
void tuichu()
{
exit(0);
}
G. 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);
說再多不如你實際操作,你可以寫一個很簡單的鏈表數據試試...
希望對你有所幫助!
H. c語言鏈表高手幫下忙 謝(請用通俗的話)
書上的意思是這樣的:你新建立了一個結點,其首址為s,(如s=(LinkList)malloc(sizeof(Node))//這句意思是:開辟新結點,並將該節點地址賦給s,也就是s指向該新結點).。
你要將這個新結點插入到p所指結點的後面,那麼:1,s的next指針和p原先的next指針所指向的結點(也就是原先p所指結點的後一個結點)拉手(s->next=p->next;)2.p的next指針和s拉手(p->next=s;)。然後結點就插入了鏈表。
s是首址,指向你剛剛開辟的新結點(你要在鏈表中插入,肯定得先開辟一個新結點,鏈表插入刪除之類操作處理的單位就是結點。每個結點包含指針域,數據域,數據域是用來裂含放數據的,和指針沒關系,指針域是用來存放next指針,指向該節點的下一個結點,這樣個個仔源檔節點之間都有鏈接)。
不知道我講的通不通俗,希望有幫助念亂