當前位置:首頁 » 編程語言 » c語言鏈式結構後插法
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言鏈式結構後插法

發布時間: 2022-06-15 03:49:17

c語言數據結構的問題,用尾插法建立鏈表

voidcreat(linklistL)//L為一級指針,也是頭指針
{
intf=1;
node*s,*r;
r=L;
while(f){
s=(node*)malloc(sizeof(node));
scanf("%s",s->a);
if(s->a[0]!='^'){
r->next=s;
r=s;
}
else{
f=0;
free(s);//釋放s
}
}
r->next=NULL;//對r設next為NULL
}

⑵ c語言鏈表插入法求解下列問題

根據題意:

一、鏈表創建:根據輸入的數字,動態創建任意多個節點插入鏈表。(題目規定n<=40,如不想使用malloc動態申請內存,需直接定義最大上限40個節點)。

二、鏈表排序:交換節點內容(不是地址),保留鏈表指針的值(*next的值)。

三、列印鏈表:利用鏈表指針遍歷鏈表。

四、對動態申請的鏈表地址空間釋放(在本程序中創建後程序就結束了,即使不寫free釋放,結束後也會釋放。但在復雜程序中調用創建,後續還有代碼,需像我代碼中寫函數動釋放不用的內存,避免浪費)。

下面是代碼:

#include <stdio.h>

#include <malloc.h>

typedef struct numStruct

{

int num;

struct numStruct *next;

}NST;

NST *insert2List(int num);//根據數字創建節點(動態),插入鏈表(首次自動生成頭節點),成功返回頭節點,失敗返回NULL。

void showList(NST *nsthead);//列印鏈表

void px(NST *nsthead);//鏈表排序

void freeList(NST *nsthead);//釋放鏈表內存

int main()

{

NST *nsthead=NULL;

int i=0,n=50,*nums=NULL;

while(n>40)

scanf("%d",&n);

nums=(int *)malloc(sizeof(int)*n);

if(!nums) return 1;

while(i<n)

scanf("%d",&nums[i++]);

i=0;

while(i<n)

nsthead=insert2List(nums[i++]);

px(nsthead);

showList(nsthead);

freeList(nsthead);

return 0;

}

void freeList(NST *nsthead)

{

NST *temp=NULL,*nst=NULL;

if(nsthead)

{

nst=nsthead->next;

while(nst!=NULL)

{

temp=nst;

nst=nst->next;

free(temp);

}

}

free(nsthead);

}

void showList(NST *nsthead)

{

if(nsthead)

while(nsthead->next!=NULL)

{

printf("%d ",nsthead->next->num);

nsthead=nsthead->next;

}

printf(" ");

}

void px(NST *nsthead)

{

NST *nt1=NULL,*nt2=NULL,ntTemp,*nextSave=NULL;

if(nsthead)

{

nt1=nsthead->next;

while(nt1)

{

nt2=nt1->next;

while(nt2)

{

if(nt1->num>nt2->num)

{

ntTemp=*nt1;

nextSave=nt1->next;

*nt1=*nt2;

nt1->next=nextSave;

nextSave=nt2->next;

*nt2=ntTemp;

nt2->next=nextSave;

}

nt2=nt2->next;

}

nt1=nt1->next;

}

}

}

NST *insert2List(int num)

{

static NST *nsthead=NULL,*nstTail=NULL;

NST *nstNew=NULL;


nstNew=(NST *)malloc(sizeof(NST));

if(!nstNew) return NULL;

nstNew->next=NULL;

nstNew->num=num;

if(!nsthead)

{

nsthead=(NST *)malloc(sizeof(NST));

if(!nsthead) return NULL;

nsthead->next=nstNew;

}

else

nstTail->next=nstNew;

nstTail=nstNew;

return nsthead;

}

⑶ 求解C語言中尾插法建立鏈表的原理

沒看懂你的代碼,但尾插法的原理是很簡單的,它就是兩句話:
設r初始時指向頭結點
設n為要輸入結點的個數
下面是偽代碼:
while(n>0)
{
p=動態為p分配內存
p->data=為當前結點賦值
r->next=p 令r的指針域指向新結點p
r=p;// 令r指向新結點的地址(方便下一次的結點繼續成為r的後繼,反復如此......直至n不大於0)
n--
}

它的核心就兩句話 r->next=p和r=p
如果你依然未懂的話,你就拿一張白紙,拿只筆,在紙上模擬上面那兩句話。相信你肯定會懂的。

⑷ 鏈表實現尾插法(C語言)

據個例子:
#include <stdio.h>
#define LEN sizeof(struct A)
#define NULL 0
struct A
{ int a;
struct A* next;
};
struct A* new() /*建立鏈表的函數,返回鏈表頭指針*/
{ struct A *p1,*p2,*head;
head=p2=p1=(struct A*)malloc(LEN);
scanf("%d",&p1->a;)
while(p2->a!=2155)/*以2155作為輸入節點的結束標志*/
{p1=(struct A*)malloc(LEN);
scanf("%d",&p1->a);
p2->next=p1;
p2=p1;
}
p1->next=NULL;
return head;
}

⑸ C語言單向循環鏈表尾插法問題

建立單向循環鏈表的代碼:

#include<stdio.h>
#include<stdlib.h>

typedefstruct_A{
intdata;
struct_A*next;
}A;
typedefA*IA;

voidcreateDoubleLink(IA*header){
intdata;
IAp;
printf("inputdataendwith-1:");
scanf("%d",&data);
while(data!=-1){
p=(IA)malloc(sizeof(A));
p->data=data;
if(*header==NULL){
*header=p;
(*header)->next=*header;
}
else{
IAq=*header;
while(q->next!=*header){
q=q->next;
}
q->next=p;
q->next->next=*header;
}
scanf("%d",&data);
}
}

voidprint(IAheader){
IAp=header;
if(header==NULL)return;
while(1){
printf("%d ",p->data);
if(p->next==header)break;
p=p->next;
}
printf(" ");
}

intmain()
{
IAheader=NULL;
createDoubleLink(&header);
print(header);
return0;
}

頭結點的意思是鏈表的第一個節點,但這個節點不保存數據。

⑹ c語言單鏈表尾插法編譯通過運行終止,這個寫法哪裡有問題

尾部插入,那麼第一步要做的是遍歷鏈表到結尾,然後創建節點插入;你的代碼中沒有看到遍歷的過程,不知道從哪插呢!

⑺ 用c語言尾插法建立帶頭結點的單鏈表

EOF!=(scanf("%d",&x)是什麼意思?把scanf("%d",&x)寫在while語句的上面為什麼不行?
這里是指輸入成功的話,執行下面的大括弧裡面語句。
pre->next是指當前結點的前一個結點,這里用於交換位置的。你可以畫個圖來查看他們之間的聯系,就會很清楚了

⑻ 用C語言編寫鏈式存儲結構下實現線性表的創建,插入,刪除,按值查找

#include <stdio.h>
#include <stdlib.h>

typedef struct LNode{
int data; //鏈表數據
struct LNode* next; //鏈表指針
}LNode,*LinkList;

/*頭插法-建立單鏈表*/
LinkList HeadCreate(LinkList la)
{
int num;
la=(LinkList)malloc(sizeof(LNode)); //建立頭結點
la->next=NULL;
scanf("%d",&num);
while(num!=10)
{
LNode *p=(LinkList)malloc(sizeof(LNode));
p->data=num;
p->next=la->next;
la->next=p;
scanf("%d",&num);
}
return la;
}

/*尾插法-建立單鏈表*/
LinkList TailCreate(LinkList la)
{
int num;
la=(LinkList)malloc(sizeof(LNode));
la->next=NULL;
LinkList s,r=la;
scanf("%d",&num);
while(num!=10)
{
s=(LinkList)malloc(sizeof(LNode));
s->data=num;
r->next=s;
r=s;
scanf("%d",num);
}
r->next=NULL;
return la;
}

/*單鏈表遍歷*/
void TravelList(LinkList la)
{
LinkList p=la->next;
while(p!=NULL)
{
printf("%d->",p->data);
p=p->next;
}
printf("\n");
}

/*單鏈表的按位查找*/
LinkList GetElem(LinkList la,int i)
{
int j=1;
LNode* p=la->next;
if(i<1)
return NULL;
while(p && j<i)
{
p=p->next;
j++;
}
return p;
}

/*單鏈表的按值查找*/
LinkList LocalElem(LinkList la,int e)
{
LNode* p=la->next;
while(p!=NULL && p->data!=e)
p=p->next;
return p;
}

/*單鏈表插入操作*/
bool InsertList(LinkList la,int i,int e)
{
//在la鏈表中的i位置插入數值e
int j=1;
LinkList p=la,s;
while(p && j<i)
{
p=p->next;
j++;
}
if(p==NULL)
return false;
if((s=(LinkList)malloc(sizeof(LNode)))==NULL)
return false;
s->data=e;
s->next=p->next;
p->next=s;
return true;
}

/*單鏈表刪除操作*/
bool DeleteList(LinkList la,int i)
{
int j=1;
LinkList p=la,q;
while(p && j<i) //p指向第i-1個元素
{
p=p->next;
j++;
}
if(p==NULL || p->next==NULL) //表示不存在第i-1個和第i的元素
return false;
q=p->next;
p->next=q->next;
free(q);
return true;
}

/*單鏈表的表長*/
int LengthList(LinkList la)
{
int nLen=0;
LinkList p=la->next;
while(p)
{
p=p->next;
nLen++;
}
return nLen;
}

/*單鏈表逆置*/
LinkList Reserve(LinkList la)
{
if(la==NULL || la->next==NULL)
return la;
LinkList p=la->next,q=p->next,r=q->next;
la->next=NULL;
p->next=NULL;
while(r!=NULL)
{
q->next=p;
p=q;
q=r;
r=r->next;
}
q->next=p;
la->next=q;
return la;
}

int main()
{
LNode la;
LinkList p;
p=HeadCreate(&la); //頭插法創建單鏈表
TravelList(p);
printf("%p\n",GetElem(p,1)); //獲得第1個結點地址
InsertList(p,2,10); //在鏈表的第2個位置插入元素10
TravelList(p);
DeleteList(p,3); //刪除鏈表的第3個元素
TravelList(p);
printf("%d\n",LengthList(p)); //獲得鏈表長度
p=Reserve(p);
TravelList(p);
return 0;
}

//運行結果
//5 6 12 7 8 14 9 3 2 5 14 10 頭插法創建鏈表
//14->5->2->3->9->14->8->7->12->6->5-> 顯示鏈表
//00382490 第一個結點的地址
//14->10->5->2->3->9->14->8->7->12->6->5-> 插入元素值為10的結點
//14->10->2->3->9->14->8->7->12->6->5-> 刪除第三個結點
//11 獲得鏈表長度
//5->6->12->7->8->14->9->3->2->10->14-> 鏈表逆置
//Press any key to continue

這是我寫的一個線性表鏈式存儲的綜合程序,包含了你所要的創建、刪除、插入、按值查找的功能,還有一些額外的功能。下面加註釋的是程序運行結果,你可以參考試著改改程序,讓程序更加完美。希望對你有幫助,呵呵!

⑼ c語言採用頭插法或尾插法建立鏈表,從鍵盤輸入遞增有序的數據建立鏈表

#include<stdio.h>
#include<stdlib.h>
/*定義鏈表結點*/
typedefstructst_node{
intvalue;
structst_node*next;
}node_t;
/*定義鏈表*/
typedefstruct{
node_thead;
node_t*tail;
}list_t;
/*插入到隊列尾部*/
voidlist_push_back(list_t*l,intvalue){
node_t*t=(node_t*)malloc(sizeof(node_t));
t->value=value;
t->next=NULL;

l->tail->next=t;
l->tail=t;
}
/*有序地插入元素*/
voidlist_push_sort(list_t*l,intvalue){
/*找出小於或等於value的節點,插入到該節點前面*/
node_t*p=l->head.next,*last=&l->head,*t;
for(;p;last=p,p=p->next){
if(value<=p->value){
t=(node_t*)malloc(sizeof(node_t));
t->value=value;
t->next=p;

last->next=t;
return;
}
}
/*如果沒有小於或等於value的節點,則直接插入到末尾*/
list_push_back(l,value);
}
/*使用數組初始化有序鏈表*/
voidlist_init(list_t*l,int*p,ints){
inti=0;
l->head.next=NULL;
l->tail=&l->head;
for(;i<s;++i){
list_push_sort(l,p[i]);
}
}
/*清空鏈表*/
voidlist_clear(list_t*l){
node_t*p=l->head.next,*t;
while(p){
t=p;
p=p->next;
free(t);
}
l->head.next=NULL;
l->tail=&l->head;
}
/*合並有序鏈表*/
voidlist_merge(list_t*l,list_t*r,list_t*o){
node_t*pl=l->head.next,*pr=r->head.next;
while(pl||pr){
if(pl&&pr){
if(pl->value<=pr->value){
list_push_back(o,pl->value);
pl=pl->next;
}else{
list_push_back(o,pr->value);
pr=pr->next;
}
}elseif(pl){
list_push_back(o,pl->value);
pl=pl->next;
}else{
list_push_back(o,pr->value);
pr=pr->next;
}
}
}
/*刪除相同結點*/
voidlist_plicate_delete(list_t*l){
if(&l->head!=l->tail){
node_t*p=l->head.next,*last,*t;
intvalue=p->value;

last=p;
p=p->next;
while(p){
if(value==p->value){
t=p;
last->next=p->next;
p=p->next;
free(t);
}else{
value=p->value;
last=p;
p=p->next;
}
}
}
}
/*列印鏈表*/
voidlist_show(char*name,list_t*l){
node_t*p=l->head.next;
printf("%s:",name);
for(;p;p=p->next){
printf("%d,",p->value);
}
printf(" ");
}
/*主函數*/
voidmain(){
list_tlist1,list2,list3;
inta[]={10,4,6,12,1,8,14,10,14,6};
intb[]={7,11,6,1,13,5,1,14};

/*所有鏈表需要初始化後才能使用*/
list_init(&list1,a,sizeof(a)/sizeof(int));
list_init(&list2,b,sizeof(b)/sizeof(int));
list_init(&list3,NULL,0);

printf("初始值: ");
list_show("List1",&list1);
list_show("List2",&list2);
list_show("List3",&list3);

/*合並鏈表*/
list_merge(&list1,&list2,&list3);
printf("合並後: ");
list_show("List1",&list1);
list_show("List2",&list2);
list_show("List3",&list3);

/*去重復*/
list_plicate_delete(&list3);
printf("去重復後: ");
list_show("List1",&list1);
list_show("List2",&list2);
list_show("List3",&list3);

/*所有鏈表都需要釋放空間*/
list_clear(&list1);
list_clear(&list2);
list_clear(&list3);
}

//這可是血汗錢啊.....

⑽ C語言鏈表尾插法建立,非常需要大家幫忙

你的代碼看著不太習慣……而且……按說應該自己報錯才對……

先說我覺得的核心:
addnode函數里,你while循環終止時,t是指向Null的,然後t直接就被指向了一個新開的空間,和你前面的鏈表完全沒有聯系……

改進:
1,加的時候應該判斷一下,因為你建立頭指針的時候,如果還沒有元素,這個add的位置就應該是l->Next=(lnode *)malloc(sizeof(lnode)),如果已經有元素,那麼需要找到末尾的那個元素(也就是t->Next==Null),然後是t->Next=(lnode *)malloc(sizeof(lnode))
也就是按你目前改的話,addnode中需要:
void addnode(linklist *l, int x){
if((*l)->count==0)
{
(*l)->next=(lnode *)malloc(sizeof(lnode));
(*l)->next->NULL;
(*l)->data=x;
(*l)->count++;
return;
}
lnode *t=l->next;
while(t->next!=NULL) t=t->next;
t->next=(lnode*)malloc(sizeof(lnode));//一定先開啟空間,有了下一個節點的地址,再指過去,不能t已經到空了,讓t=,那是直接把t挪走,而不是讓鏈表連接起來了
t->next->next=NULL;
t->next->data=x;
(*l)->count++;
return;
}

其實更建議定義一個新的函數叫創建節點,應該能讓程序更清晰
lnode* createNode(int data)
{
lnode* node=(lnode*)malloc(sizeof(lnode));
node->data=data;
node->next=NULL;
return node;
}
這樣的話,你的添加節點應該能更好理解,代碼我不寫了,邏輯為:
添加節點(鏈表l,數值x){
如果頭指針下沒有元素
{頭指針下的元素指向一個新創建的元素(*l)->next=createNode(x);
頭指針元素計數加1;
返回
}
聲明新的節點指針t;
如果t不是最後一個元素(這里務必注意:條件應是t->next==NULL,而不是t==NULL),讓t指向其下一個節點並循環;
t的下一個節點指向一個新創建的指針(t->next=createNode(x);
l的計數加一;
}

PS:
linklist在聲明的時候已經定義成了headnode的指針,為什麼在初始化時候要(*l)=...呢?
l=(linklist *)malloc(sizeof(headnode))應該就好了吧,目的是開一個空間,並讓l指過去