⑴ c語言 建立一個鏈表,實現增刪改查
下面是以前寫的一個關於鏈表的綜合操作,你可以看看,應該可以滿足你的要求。
/********************************************************************
created: 2009/09/15
created: 16:9:2009 17:20
filename: E:\dd\lianbiao\lianbiao.cpp
author:
purpose: 一個win32 的控制台程序
實現單項鏈表的數據刪除、插入、排序等功能
*********************************************************************/
#include <stdio.h>
#include "windows.h"
#include "malloc.h"
#define LEN sizeof(struct student)
#define NULL 0
#define N 5 //N為所要創建的鏈表的長度
int n = 0; //定義全局變數n,表示鏈表的節點個數
/*******************************************************************
Author/data: /2009/09/15
Description: 聲明一個結構體作為鏈表的節點.
tip: student 只是一個標簽,是不能聲明變數的
*********************************************************************/
struct student
{
int num;
float cj;
struct student *Pnext;
};
/*******************************************************************
Author/data: /2009/09/15
Description: 創建一個動態鏈表
參數: 返回鏈表的第一個節點的地址
x 為鏈表的長度
*********************************************************************/
struct student *pa(int x)
{
struct student *head;
struct student *p1,*p2;
// n = 0;
p1 = p2 = (struct student *)malloc(LEN); //開辟一個結構體內存
fflush(stdin); // 清除緩沖區數據 避免直接讀入緩沖區數據
scanf("%d,%f",&p1->num,&p1->cj);
head = NULL;
while(p1->Pnext != NULL)
{
n = n+1;
if(n == 1)
{
head = p1; // 鏈表的頭地址
}
else
{
p2->Pnext = p1; //鏈接鏈表數據
}
/* 判斷是否最後一個 */
if(n >= x)
{
p1->Pnext = NULL;
}
else
{
p2 = p1;
p1 = (struct student *)malloc(LEN);
fflush(stdin);
scanf("%d,%f",&p1->num,&p1->cj);
}
}
return(head);
}
/*******************************************************************
Author/data: /2009/09/15
Description: 輸出一個鏈表
參數: head為第一個節點的地址
*********************************************************************/
void print(struct student *head)
{
struct student *p;
int i = 0;
printf("\nNow,These %d records are:\n",n);
p = head;
if(head != NULL) // 如果鏈表不是空的,則進行數據輸出
{
do
{
printf("%d\t",i++);
printf("%5d %5.1f\n",p->num,p->cj); // 輸出鏈表數據
p = p->Pnext;
}while(p != NULL);
}
}
/*******************************************************************
Author/data: /2009/09/16
Description: 釋放動態鏈表的地址空間
參數: 鏈表的頭地址head
無返回值
*********************************************************************/
void freelinkspace(struct student *head)
{
struct student Ltemp;
Ltemp.Pnext = head->Pnext; // Ltemp 用來存放->next,避免空間被
// 釋放後,找不到下一個結點的地址
while(head->Pnext != NULL) // 判斷是否已經空間釋放到最後一個
{
free(head);
head = Ltemp.Pnext;
Ltemp.Pnext = head->Pnext;
}
free(head); // 釋放最後一個結點空間
}
/*******************************************************************
Author/data: /2009/09/15
Description: 刪除鏈表鏈表中的一個結點
參數: head 為第一個節點的地址
num 為要刪除結點的序號(head->num)
*********************************************************************/
struct student *del(struct student *head,int num)
{
struct student *p1,*p2;
p1 = head;
while(p1->num!=num && p1->Pnext!=NULL) // 尋找要刪除的結點
{
p2 = p1; // p2 存放刪除結點的前一個結點地址
p1 = p1->Pnext; // p1 存放要刪除的結點
}
if(num == p1->num) // 是否找到要刪除結點
{
if(p1 == head) // 刪除的是第一個結點
{
head = p1->Pnext;
}
else if(p1->Pnext == NULL) // 刪除的是最後一個結點
{
p2->Pnext = NULL;
}
else // 刪除中間的結點
{
p2->Pnext = p1->Pnext;
p1->Pnext = NULL;
}
printf("delete: %d\n",num);
n = n-1; // 鏈表長度 - 1
}
else
{
printf("%d not been found! \n",num);
}
delete(p1);
return(head);
}
/*******************************************************************
Author/data: /2009/09/16
Description: 添加一個結點到鏈表中
參數: head 為第一個結點的地址指針
stud 為要插入的結點的地址指針
*********************************************************************/
struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p0 = stud;
p1 = head;
while(p0->num>p1->num && p1->Pnext!=NULL) // 找到添加結點的位置
{
p2 = p1; // p2 存放要添加的前一個結點的地址
p1 = p1->Pnext; // p1 存放要添加的後一個結點的地址
}
if(p0->num<=p1->num && p1->Pnext!=NULL)
{
if(p1 == head) // 添加結點到第一個位置
{
p0->Pnext = p1;
head = p0;
}
else
{
p2->Pnext = p0;
p0->Pnext = p1;
}
}
else // 添加結點到最後一個位置
{
p1->Pnext = p0;
p0->Pnext = NULL;
}
n = n+1; // 結點數目 + 1
return(head);
}
/*******************************************************************
Author/data: /2009/09/16
Description: 鏈表的重新排序===按照.num(不能重復)的大小從小到大排
列鏈表數據
參數: head 接收鏈表第一個節點的地址指針
返回值為新生成鏈表的第一個節點的地址指針
*********************************************************************/
struct student *linkpaix(struct student *head)
{
struct student *stemp,*ltemp,*shead,*head_h; /* */
struct student *p,*q; /* 申請兩個鏈表指針,用來儲存鏈表交換過
程的中間值 */
head_h = head;
ltemp = head;
p = (struct student *) malloc(LEN);
q = (struct student *) malloc(LEN); /* 為p,q開辟動態存儲空間 */
/* -==== 先將鏈表的第一個數據與其他數據比較 ====- */
while(head->Pnext != NULL)
{
shead = head;
head = head->Pnext;
if(ltemp->num > head->num)
{
if(ltemp == shead)
{
ltemp ->Pnext = head ->Pnext;
head ->Pnext = ltemp;
}
else
{
p->Pnext = head ->Pnext;
q->Pnext = ltemp ->Pnext;
head ->Pnext = q->Pnext;
shead ->Pnext = ltemp;
ltemp ->Pnext = p->Pnext;
}
head_h = head;
head = ltemp;
ltemp = head_h;
}
}
/* -==== 先將鏈表的第一個數據與其他數據比較 ====- */
/* -==== 比較鏈表第一個以外的數據 ====- */
while(ltemp ->Pnext != NULL)
{
stemp = ltemp;
ltemp = ltemp ->Pnext;
head = ltemp;
while(head->Pnext != NULL)
{
shead = head;
head = head->Pnext;
if(ltemp->num > head->num)
{
if(ltemp == shead)
{
p->Pnext = head ->Pnext;
stemp ->Pnext = head;
head ->Pnext = ltemp;
ltemp ->Pnext = p->Pnext;
}
else
{
p->Pnext = head ->Pnext;
q->Pnext = ltemp ->Pnext;
stemp ->Pnext = head;
head ->Pnext = q->Pnext;
shead ->Pnext = ltemp;
ltemp ->Pnext = p->Pnext;
}
head = ltemp;
ltemp = stemp ->Pnext;
}
}
}
/* -==== 比較鏈表第一個以外的數據 ====- */
free(p);
free(q); // 釋放p,q的動態存儲空間
return(head_h);
}
/*******************************************************************
Author/data: /2009/09/15
Description: 主函數
參數:
*********************************************************************/
void main()
{
struct student *phead,*pins; // 定義2個鏈表指針
int delnum, selflog,flog_a = 1,Nf = 1; // 要刪除的對象id
char delflog ; // 刪除標志y/n
char insflog, flog_nx = 'n';
char flog_free ; // 釋放標志
/* === 輸入N個數據 === N 為定值
printf("please input %d numbers:\n",N);
phead = pa(N); // 創建一個動態鏈表,並賦值
print(phead); // 輸出鏈表數據
*/
/* === 輸入Nx個數據 === Nx 為輸入值 === */
int Nx; // Nx 想要輸入的鏈表長度
printf("How long do you want establish? \t");
fflush(stdin);
scanf("%d",&Nx);
/* -== 判斷創建的是否是一個空鏈表 ==- */
while(Nx == 0)
{
printf("您要創建的是一個空鏈表,是否確定?y/n \t");
fflush(stdin);
scanf("%c",&flog_nx);
if(flog_nx == 'n')
{
printf("How long do you want input?\t");
fflush(stdin);
scanf("%d",&Nx);
}
else if(flog_nx == 'y') goto endl;
else
{
printf("wrong input!\n");
printf("How long do you want input?\t");
fflush(stdin);
scanf("%d",&Nx);
}
}
printf("please input %d numbers: ",Nx);
printf("如:1,3 兩個數中間以,隔開\n");
phead = pa(Nx); // 創建一個動態鏈表,並賦值
print(phead); // 輸出鏈表數據
/* -== 鏈表操作 ==- */
while(flog_a)
{
if(phead == NULL) {printf("鏈表已空,無法操作\n"); flog_a = 0; break;}
printf("\n操作\n1:\t插入數據\n2:\t刪除數據\n3:\t排序\n4:\t清屏\n5:\t輸出現在的鏈表數據\n0:\texit\n");
printf("\nPlease input:\t");
fflush(stdin);
if(scanf("%d",&selflog)) // select flog 選擇項
switch(selflog)
{
case 1 :
/* ====== 插入數據到鏈表 ====== */
printf("insert someone? y/n\t");
fflush(stdin);
scanf("%c",&insflog);
while(insflog != 'n')
{
while(insflog != 'y'&& insflog != 'n')
{
printf("wrnong input,please input again. \n");
printf("another one? y/n\t");
fflush(stdin);
scanf("%c",&insflog);
}
printf("please input the date:\n");
pins = (struct student *)malloc(LEN);
fflush(stdin);
scanf("%d,%f",&pins->num,&pins->cj);
phead = insert(phead,pins);
print(phead);
printf("another one? y/n\t");
fflush(stdin);
scanf("%c",&insflog);
while(insflog != 'y'&& insflog != 'n')
{
printf("wrnong input,please input again. \n");
printf("another one? y/n\t");
fflush(stdin);
scanf("%c",&insflog);
}
}
/* ====== 插入數據到鏈表 ====== */
break;
case 2 :
/* ====== 刪除鏈表中的數據 ====== */
printf("del someone? y/n\t");
fflush(stdin);
scanf("%c",&delflog);
while(delflog != 'n' && phead != NULL)
{
while(delflog != 'y'&& delflog != 'n')
{
printf("wrnong input,please input again. \n");
printf("del someone? y/n\t");
fflush(stdin);
scanf("%c",&delflog);
}
printf("please input the student what you want del:\n");
fflush(stdin);
scanf("%d",&delnum);
phead = del(phead,delnum);
print(phead);
printf("another one? y/n\t");
fflush(stdin);
scanf("%c",&delflog);
if((n+1)==0)
{
printf("There is no more num could be del!\n");
break;
}
}
/* ====== 刪除鏈表中的數據 ====== */
break;
case 3 :
/* ====== 排列鏈表數據 ====== */
printf("\n排序之後:");
phead = linkpaix(phead);
print(phead); // 排序該數據鏈表
/* ====== 排列鏈表數據 ====== */
break;
case 4 :// clrscr();
system("cls");
break;
case 5 : print(phead); break;
case 0 : flog_a = 0 ; break; /* 退出 */
default : printf("wrong input\nPlease input again");
break;
}
else printf("非法輸入!\n");
}
endl: while(1)
{
if(Nx == 0)
{
printf("Can't establish the link!\n");
break;
}
printf("\n保留數據?y/n\t"); // 是否釋放地址空間
fflush(stdin);
scanf("%c",&flog_free);
if(flog_free == 'y')
{
break;
}
else if(flog_free == 'n')
{
freelinkspace(phead);
break;
}
else
{
printf("wrong input!\n");
}
}
printf("OVER!\nGOOD LUCK!\n");
}
⑵ C語言鏈表的建立與插入
#include
<stdio.h>
#include
<stdlib.h>
//使用結構體構建鏈表
struct
node{
int
data;
struct
node
*next;
};
void
main()
{
int
a,n=1;
struct
node
*p,*head,*t;
head=(struct
node
*)malloc(sizeof(struct
node));
//p=(struct
node
*)malloc(sizeof(struct
node));
//申請動態空間
p=head;
//申請動態空間
t=(struct
node
*)malloc(sizeof(struct
node));
for(;n<=5;n++)
//輸入1,3,5,7,9
{
p->data=2*n-1;
p->next=(struct
node
*)malloc(sizeof(struct
node));
p=p->next;
}
printf("原始鏈表如下:\n");
//輸出原始鏈表
for(p=head;p->next!=NULL;p=p->next)
{
printf("%d
",p->data);
}
printf("\n請輸入需要插入的數據\n");
//輸入所要插入的新數據
scanf("%d",&a
);
for(p=head;p->next!=NULL;)
//按順序插入相應位置
{
if(p->data
<=
a
&&
(p->next)->data
>=
a)
{
t->data
=a;
t->next
=p->next
;
p->next=t;
break;
}
p=p->next
;
}
printf("插入新數據後的鏈表\n");
//輸出插入新數據的鏈表
for(p=head;p->next!=NULL;)
{
printf("%d
",p->data);
p=p->next;
}
printf("\n");
free(p);
free(head);
free(t);
}
⑶ c語言鏈表,非常感謝
//WIN-TC是什麼turbo? 我在DEV-C下改的,應該也沒問題吧
#include<stdio.h>
#include<malloc.h>
//#define NULL 0 //不用重定義NULL把,空指針用NULL就可以
struct student
{
int num;
char name[20];
float score;
struct student *next;
};
int n=0;
/*建立動態鏈表*/
/* 這個函數,有重要的問題,你在函數里創建了臨時類,st,head指向它,但是當這個函數結束時,這個類會被銷毀,head將是個沒有指向的指針,所以當在函數中返回類時,需要堆分配內存
*/
struct student *creat(void)
{
struct student *p1=NULL,*head=NULL; // 這里一個學生類指針即可
struct student *st = (struct student *)malloc(sizeof(struct student)); //同上解釋
printf("Please input records:\n");
scanf("%d%s%f",&(st->num),st->name,&(st->score));
p1=st;
p1->next = NULL;
if(st->num!=0)
head=p1;
while(p1->num!=0)
/*這個鏈表的創立,無非就是那些步驟,你必然明白,再不行,畫個示意圖就明白了,注意的是,什麼時候指針賦值,什麼時候用指針控制變數
*/
{n=n+1;
printf("%dEnd\n",n);
p1->next = (struct student *)malloc(sizeof(struct student));
//這句話至少得有一個「->」,需要控制變數
p1->next->next = NULL;
scanf("%d%s%f",&(p1->next->num),p1->next->name,&(p1->next->score));
if(n!=1)
{
p1 = p1->next;
}
}
printf("\nOver.\n");
/*printf("%o\n",head);
if(head!=NULL)
printf("%10d%10s%10.1f\n",head->num,head->name,head->score);*/
return(head);
}
/*輸出*/
void print(struct student *head)
{
struct student *p;
p=head;
/*printf("%o\n",head);*/
getch();
//if(head==NULL)
//printf("\nList null!\n");
//else
printf("\nNow,there are %d records:\n",n);
while(p!=NULL)
{
printf("%10d%10s%10.1f\n",p->num,p->name,p->score);
p=p->next;
}
}
int main()
{
struct student *head;
head=creat();
/*printf("%o\n",head);
if(head!=NULL)
printf("%10d%10s%10.1f\n",head->num,head->name,head->score);*/
print(head);
getch();
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語言如何用動態鏈表儲存數據
單鏈表,雙鏈表,堆 都可以,不過看您要存儲什麼數據 以單鏈表為例: 定義一個節點結構 typedef struct LNode{ ElementType date; struct Lnode *next; }Lnode; 然後用malloc開辟需要的節點空間,把數據存進去就可以了 p = (Lnode) malloc (sizeof(Lnode)); //開辟一個節點,p為所開辟空間的指針 至於查找,從頭節點開始q = p->next ;一個個查就行了。