Ⅰ c語言,創建單向鏈表函數
void inputinfo(stu_info **head,int n)
{
int i=1;
stu_info *loc_head=NULL,*tail;
loc_head=(stu_info *)malloc(sizeof(stu_info));
tail=loc_head;
for(i=1;i<=n;i++)
{
stu_info *p=(stu_info *)malloc(sizeof(stu_info));
printf("please input the %d student's infomation,number/name/english/math/phy/c/\n",i);
scanf("%s%s%f%f%f%f",p->num,p->name,&p->english,&p->math,&p->phy,&p->c);
getchar();/*存儲scanf輸入的換行符??*/
tail -> next = p;
tail= p;
}
tail->next=NULL;
*head=loc_head;
}
改好了。
Ⅱ C語言創建單鏈表
s* creat_list()函數改下:
s* create_list()
{
int a[]={1,2,3,4,5},j=4;
s *h,*p,*current;
h=(s *)malloc(sizeof(s));
current = h; /*current指針用於跟隨鏈表進度*/
h->next='\0';
while(j>=0)
{
p=(s *)malloc(sizeof(s));
p->i=a[j];
current->next=p;
current = p;
j--;
}
p ='\0';
current== '\0';
free(p);/*這里把用完的空間釋放掉,是個好習慣*/
free(current);
return h;
}
Ⅲ C語言建立帶頭結點的單鏈表
單鏈表的生成有2種方式:頭插法和尾插法。
1、頭插法
/*********************************************************************
*函數名稱:linklist*CreateLinklistHead()
*函數功能:利用頭插法創建鏈表
*參數:無
*返回值:創建完鏈表後的鏈表頭結點
*說明:無
*********************************************************************/
externlinklist*CreateLinklistHead()
{
intx,i,nodeNum;
linklist*head,*temp;//頭結點與臨時結點
head=(linklist*)malloc(sizeof(linklist));//生成表頭結點
head->next=NULL;//給表頭結點的指針域賦值
printf("請輸入鏈表中結點的個數:");
scanf("%d",&nodeNum);
for(i=1;i<=nodeNum;i++)
{
printf("請輸入第%d個結點的數據:",i);
scanf("%d",&x);
temp=(linklist*)malloc(sizeof(linklist));//生成新的結點
temp->data=x;//對新結點的數據域賦值
//將新結點插到頭結點之後
temp->next=head->next;
head->next=temp;
}
returnhead;//返回新建鏈表的頭結點
}
2、尾插法
/*********************************************************************
*函數名稱:linklist*CreateLinklistRear()
*函數功能:利用尾插法創建鏈表
*參數:無
*返回值:創建完鏈表後的鏈表頭結點
*說明:無
*********************************************************************/
externlinklist*CreateLinklistRear()
{
intx,i,nodeNum;
linklist*head,*rear,*temp;//定義頭結點、尾結點和臨時結點
head=(linklist*)malloc(sizeof(linklist));//生成表頭結點,表頭結點不存放數據
head->next=NULL;//將表頭結點的指針域賦值為NULL
rear=head;//將表頭結點賦值給表尾結點
printf("請輸入鏈表中結點的個數:");
scanf("%d",&nodeNum);
for(i=1;i<=nodeNum;i++)
{
printf("請輸入第%d個結點的數據:",i);
scanf("%d",&x);
temp=(linklist*)malloc(sizeof(linklist));//生成新的結點
temp->data=x;//新增結點的數據域
temp->next=NULL;//新增結點的指針域(由於是尾插法,所以插入的結點都在尾部,即指針域為NULL)
rear->next=temp;//使前一個結點指向新增結點(head->next=temp)
rear=temp;//將新增結點賦值給尾結點(尾插法,插入的結點在尾部)(rear=head->next)
}
//rear->next=NULL;//將尾結點的指針域賦值為空(為了方便檢驗鏈表是否為空鏈表)
returnhead;//返回頭結點
}
Ⅳ C語言創建鏈表,函數調用部分
#include<stdio.h>
#include<windows.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
//定義數據類型名稱
typedef int DataType;
#define flag -1 //定義數據輸入結束的標志數據
//單鏈表結點存儲結構定義
typedef struct Node
{
DataType data;
struct Node *next;
}LNode ,*LinkList;
//建立單鏈表子函數
LNode *Create_LinkList()
{
LNode *s,*head,*L;int i=0,x; //定義指向當前插入元素的指針
while(1)
{
scanf("%d",&x);
if(-1==x)
{ return head;
break;}
s= (LNode *)malloc(sizeof(LNode)); //為當前插入元素的指針分配地址空間
s->data =x;
s->next =NULL;
i++;
if(i==1)
head=s;
else
L->next =s;
L=s;
}
}
//查找子函數(按序號查找)
LNode *Get_LinkList(LinkList L,int i)
{
LNode *p;
int j; //j是計數器,用來判斷當前的結點是否是第i個結點
p=L;
j=1;
while(p!=NULL&&j<i)
{
p=p->next ; //當前結點p不是第i個且p非空,則p移向下一個結點
j++;
}
return p;
}
//插入運運算元函數
void Insert_LinkList(LinkList L,int i,DataType x) //在單鏈表L中第i個位置插入值為x的新結點
{
LNode *p,*s;
p =Get_LinkList(L,i); //尋找鏈表的第i-1個位置結點
if(p==NULL)
{
printf("插入位置不合法!");
exit(-1);
}
else
{
s= (LinkList)malloc(sizeof(LNode)); //為當前插入元素的指針分配地址空間
s->data =x;
s->next =p->next ;
p->next =s;
}
}
//單鏈表的刪除運運算元函數
void Delete_LinkList(LinkList L,int i) //刪除單鏈表上的第i個結點
{
LNode *p,*q;
p=Get_LinkList(L,i-1); //尋找鏈表的第i-1個位置結點
if(p==NULL)
{
printf("刪除的位置不合法!"); //第i個結點的前驅結點不存在,不能執行刪除操作
exit(-1);
}
else
{
if(p->next ==NULL)
{
printf("刪除的位置不合法!"); //第i個結點不存在,不能執行刪除操作
exit(-1);
}
else
{
q=p->next ;
p->next =p->next->next;
free(q);
}
}
}
//求表長運運算元函數
int Length_LinkList(LinkList L)
{
int l; //l記錄L的表長
LNode *p;
p=L;
l=1;
while(p->next)
{
p=p->next;
l++;
}
return l;
}
int main ()
{
LNode *head,*p;
head=(LinkList)malloc(sizeof(LNode));
int x,y;
a:
printf("*******menu*******
");
printf("**創建**********1*
");
printf("**插入**********2*
");
printf("**刪除**********3*
");
printf("**表長**********4*
");
printf("**清屏**********5*
");
printf("**列印**********6*
");
printf("**退出******other*
");
printf("******************
");
int i=1;
while(i)
{
printf("請輸入選項:");
scanf("%d",&i);
switch(i)
{
case 1:head=Create_LinkList(); getchar();break;
case 2:printf("請輸入位置和數據;");
scanf("%d%d",&x,&y);
Insert_LinkList(head,x,y);break;
case 3:printf("請輸入位置;");
scanf("%d",&x);
Delete_LinkList(head,x);break;
case 4:printf("%d",Length_LinkList(head));break;
case 5:system("cls");goto a;
case 6:p=head;
while(p!=NULL)
{printf("%d
",p->data);
p=p->next;}
break;
default :i=0;
}
}
}
我把創建給改了一下
Ⅳ c語言創建單鏈表
#include<stdio.h>
#include<stdlib.h>
/*線性表*/
struct TLink {
int data;
struct TLink * next;
};/*end struct TLink*/
/*生成新元素*/
struct TLink * new_item(int number)
{
struct TLink * r = 0;
r = (struct TLink *)malloc(sizeof(struct TLink));
r->data = number;
r->next = 0;
return r;
}/*end new_item*/
/*在線性表中查詢數據*/
struct TLink * lookup(struct TLink * root, int number)
{
struct TLink * h = root;
while(h) {
if (h->data == number) return h;
h = h->next ;
}/*end lookup*/
return 0;
}
/*在線性表中追加一個數據*/
void append(struct TLink * * root, int number)
{
struct TLink * r = 0, * n = 0;
if (!root) return ;
/*不記錄重復元素*/
if (lookup(*root, number)) return;
/*如果表為空則新建表*/
r = *root;
if (!r) {
*root = new_item(number);
return ;
}/*end if*/
/*為保證為有序線性表,如果數據比表頭還小則作為表頭*/
if (number < r->data ) {
n = new_item(number);
n->next = r;
*root = n;
return ;
}/*end if*/
/*在有序線性表中查找位置插入元素*/
while(r) {
n = r->next ;
/*如果已經是表尾則直接追加*/
if (!n) {
n = new_item(number);
r->next = n;
return ;
}/*end if*/
/*在中央某處插入*/
if (number < n->data ) {
r->next = new_item(number);
r->next->next = n;
return ;
}/*end if*/
r = n;
}/*end while*/
}/*end append*/
/*列印有序線性表*/
void print(struct TLink * root)
{
struct TLink * r = root;
printf("【");
while(r) {
printf("%d ", r->data );
r = r->next ;
}/*end while*/
printf("\b】\n");
}/*end print*/
/*將有序線性表h1合並至有序線性表h0,並銷毀線性表h1*/
void merge(struct TLink ** h0, struct TLink ** h1)
{
struct TLink * h = 0, * k = 0;
if (!h0 || !h1) return ;
h = *h1;
while(h) {
append(h0, h->data );
k = h;
h = h->next ;
free(k);
}/*end h*/
h1 = 0;
}
int main(void)
{
int i = 0; struct TLink * x=0, *y = 0;
int a[] = {8,4,3,9,5,1};
int b[] = {7,2,1,5,6,0};
printf("原數據為:\n數組A:【");
for(i = 0; i < 6; i++) {
printf("%d ", a[i]);
append(&x, a[i]);
}/*next*/
printf("\b】\n數組B:【");
for(i = 0; i < 6; i++) {
printf("%d ", b[i]);
append(&y, b[i]);
}/*next*/
printf("\b】\n轉換為有序線性表\nA:");
print(x);
printf("B:");
print(y);
printf("AB合並後為:");
merge(&x, &y);
print(x);
return 0;
}
/*以上是順序線性表的合並程序,逆序只需將插入條件從小於改為大於即可。
合並結果如果要保留,把合並函數的lookup調用刪除即可*/
Ⅵ c語言用函數創建單鏈表
#include<stdio.h>
#include<stdlib.h>
//鏈表定義
typedef int ElemType;
typedef struct LNode
{
int data;
struct LNode *next;
}LNode,*LinkList;
/*************************************
* 鏈表函數 *
*************************************/
//鏈表初始化
void InitLink(LinkList &L);
//創建函數,尾插法
void CreateLink_T(LinkList &L,int n);
//創建函數,頭插法
void CreateLink_H(LinkList &L,int n);
//銷毀函數
void DestroyLink(LinkList &L);
//判斷是否為空函數
bool EmptyLink(LinkList &L);
//獲取函數
bool GetLink(LinkList &L,int i,int & e);
//插入函數
void InsertLink(LinkList &L,int i,int e);
//刪除函數
void DeleteLink(LinkList &L,int i,int &e);
//遍歷函數
void TraverseLink(LinkList &L);
//鏈表長度函數
int LengthLink(LinkList &L);
//合並函數
void MergeLink(LinkList &L1,LinkList L2);
void main()
{
LinkList L1,L2;
InitLink(L1);
InitLink(L2);
CreateLink_H(L1,2);
CreateLink_T(L2,2);
TraverseLink(L1);
printf("\n");
TraverseLink(L2);
printf("\n");
MergeLink(L1,L2);
TraverseLink(L1);
TraverseLink(L2);
}
//創建函數,尾插法
void InitLink(LinkList &L)
{
L=(LinkList)malloc(sizeof(LNode));
if (!L)
{
printf("Init error\n");
return;
}
L->next=NULL;
}
void CreateLink_T(LinkList &L,int n)
{
if(n<1)
{
printf("n must >=1\n");
return ;
}
else
{
// L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
for(int i=0;i<n;i++)
{
LinkList p=(LinkList)malloc(sizeof(LNode));// the lower letter p
printf("enter the data :\t");
scanf("%d",&(p->data));
p->next=L->next;
L->next=p;
}
}
}
//創建函數,頭插法
void CreateLink_H(LinkList &L,int n)
{
if (n<1)
{
printf("n must >=1\n ");
return;
}
else
{
//L=(LinkList)malloc(sizeof(LNode));
LinkList pre=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
pre=L;
for(int i=0;i<n;i++)
{
LinkList p=(LinkList)malloc(sizeof(LNode));
printf("enter the data:\t");
scanf("%d",&(p->data));
pre->next=p;
pre=p;
}
pre->next=NULL;
}
}
//銷毀函數
void DestroyLink(LinkList &L)
{
LinkList q=L,p=L;
while (p)
{
q=p;
p=p->next;
free(q);
}
L->next=NULL;
}
//判斷是否為空函數
bool EmptyLink(LinkList &L)
{
if (NULL==L->next)
{
return true;
}
else
{
return false;
}
}
//獲取函數
bool GetLink(LinkList &L,int i,int& e)
{
if (i<1)
{
return false;
}
else
{
if (EmptyLink(L))
{
return false;
}
LinkList p=L->next;
int j=1;
while(p&&j<i)
{
p=p->next;
j++;
}
if (!p||j>i)
{
return false;
}
else
{
e=p->data;
return true;
}
}
}
//插入函數
void InsertLink(LinkList &L,int i,int e)
{
if (i<0||i>LengthLink(L))
{
printf("Insert error\n");
return;
}
else
{
LinkList p=L;
int j=0;
while(p&&(j<i))
{
p=p->next;
j++;
}
if (!p||j>i)
{
printf("Insert error\n");
return;
}
else
{
LinkList q=(LinkList)malloc(sizeof(LNode));
q->data=e;
q->next=p->next;
p->next=q;
}
}
}
//刪除函數
void DeleteLink(LinkList &L,int i,int &e)
{
if(i<=0||i>LengthLink(L))
{
printf("delete error\n");
return;
}
else
{
LinkList p=L;
int j=0;
while(p&&j<i-1)
{
p=p->next;
j++;
}
if(!p||j>i)
{
printf("please enter i again\n");
return;
}
else
{
LinkList q=p->next;
e=p->next->data;
p->next=p->next->next;
free(q);
}
}
}
//遍歷函數
void TraverseLink(LinkList &L)
{
LinkList p=L->next;
if(!p)
{
printf("the Link L is empty\n");
}
while(p)
{
printf("%d\n",p->data);
p=p->next;
}
}
//鏈表長度函數
int LengthLink(LinkList &L)
{
int i=0;
LinkList p=L->next;
while(p)
{
p=p->next;
i++;
}
return i;
}
//合並函數
void MergeLink(LinkList &L1,LinkList L2)
{
int i=0,flag=0;
LinkList p1=L1->next,p2=L2->next;
LinkList p=(LinkList)malloc ((LengthLink(L1)+LengthLink(L2)+2)*sizeof(LNode));
LinkList pre=p;
if (!p)
{
printf("MergeLink error\n");
return;
}
p->next=NULL;
while (p1&&p2)
{
if (p1->data>=p2->data)
{
InsertLink(p,i++,p2->data);
p2=p2->next;
}
else
{
InsertLink(p,i++,p1->data);
p1=p1->next;
}
}
while (p1)
{
InsertLink(p,i++,p1->data);
p1=p1->next;
}
while(p2)
{
InsertLink(p,i++,p2->data);
p2=p2->next;
}
while(pre)
{
pre=pre->next;
}
LinkList q=L1;
L1=p;
DestroyLink(q);
DestroyLink(L2);
}
Ⅶ C語言主函數建立一條單向鏈表
#include <stdio.h>
#include <malloc.h>#define N 5
typedef struct LNode
{
double data;
long number;
struct LNode *next;
}LNode,*LinkList;LinkList fun(LinkList L,double *aver)
{
LinkList p,q,h;
double sum=0;
p=L->next;
while(p!=NULL)
{
sum+=p->data;
p=p->next;
}
sum=sum/N;
*aver = sum;
printf("aver=%.2f\n",*aver);
p=L->next;
h=(LinkList)malloc(sizeof(LNode));
h->next = NULL;
while(p!=NULL)
{
if(p->data>=sum)
{
q=(LinkList)malloc(sizeof(LNode));
q->number = p->number;
q->data=p->data;
q->next = h->next;
h->next = q;
}
p=p->next;
}
return h;
}
void main()
{
LinkList L,p,h;
int i;
double aver;
L= (LinkList)malloc(sizeof(LNode));
L->next = NULL;
printf("創建鏈表...\n輸入學生的學號和成績:\n");
for( i=0;i<N;i++)//逆位序輸入N個元素的值,建立帶頭結點的單鏈表L
{
p=(LinkList)malloc(sizeof(LNode));
scanf("%d %lf",&p->number,&p->data);
p->next=L->next;
L->next=p;
}
h=fun(L,&aver);
printf("平均成績為:%.2f\n",aver);
p=h->next;
printf("大於或等於平均成績的學生信息...\n");
printf("學號 成績\n");
while(p!=NULL)
{
printf("%-12d %-3.2f\n",p->number,p->data);
p=p->next;
}
printf("\n");
}
Ⅷ c語言構建一個最簡單的單鏈表
typedef struct node { char name[20]; struct node *link; }stud; 下面就來看一個建立帶表頭(若未說明,以下所指 鏈表 均帶表頭)的單 鏈表 的完整程序。 #include <stdio.h> #include <malloc.h> /*包含動態內存分配函數的頭文件*/ #define N 10 /*N為人數*/ typedef struct node { char name[20]; struct node *link; }stud; stud * creat(int n) /*建立單 鏈表 的函數,形參n為人數*/ { stud *p,*h,*s; /* *h保存表頭結點的指針,*p指向當前結點的前一個結點,*s指向當前結點*/ int i; /*計數器*/ if((h=(stud *)malloc(sizeof(stud)))==NULL) /*分配空間並檢測*/ { printf("不能分配內存空間!"); exit(0); } h->name[0]='\0'; /*把表頭結點的數據域置空*/ h->link=NULL; /*把表頭結點的鏈域置空*/ p=h; /*p指向表頭結點*/ for(i=0;i<n;i++) { if((s= (stud *) malloc(sizeof(stud)))==NULL) /*分配新存儲空間並檢測*/ { printf("不能分配內存空間!"); exit(0); } p->link=s; /*把s的地址賦給p所指向的結點的鏈域,這樣就把p和s所指向的結點連接起來了*/ printf("請輸入第%d個人的姓名",i+1); scanf("%s",s->name); /*在當前結點s的數據域中存儲姓名*/ s->link=NULL; p=s; } return(h); } main() { int number; /*保存人數的變數*/ stud *head; /*head是保存單 鏈表 的表頭結點地址的指針*/ number=N; head=creat(number); /*把所新建的單 鏈表 表頭地址賦給head*/ } 這樣就寫好了一個可以建立包含N個人姓名的單 鏈表 了。寫動態內存分配的程序應注意,請盡量對分配是否成功進行檢測。
Ⅸ 用C語言編程實現單鏈表的基本操作
第二個顯示為什麼?為什麼什麼東西都沒有、
#include<stdio.h>
typedef
struct
sample
{
char
ch;
struct
sample
*next;
}LNode;
LNode
*Createlist(LNode
*head)
//創建單鏈表,頭插入法
{
LNode
*p;
if((head=(LNode
*)malloc(sizeof(LNode)))==NULL)
printf("aply
error\n");
head->next=NULL;
head->ch
=
'\0';
int
i=
0;
printf("請一次輸入A-Z:\n");
for(i
=
0
;
i
<
26;
++i)
{
p=(LNode
*)malloc(sizeof(LNode));
if(!p)
printf("aply
error\n");
scanf("%c",&p->ch);
p->next
=
head->next;
head->next=p;
}
return
head;
}
LNode
*EncryptList(LNode*
head)
{
LNode
*p=
head,*q
=
head->next,*r
=
head->next;
while(p->next)
{
p
=
p->next;
}
int
i
=
0
;
for(i
=
0
;
i
<
3;
i++)
{
p->next
=
r;
p
=
p->next;
q
=
q->next;
r
=
q;
}
p->next
=NULL;
head->next
=
q;
return
head;
}
void
ListPrint(LNode
*head)
{
LNode
*p
=
head->next;
while(p->next!=NULL)
{
printf("%c\t",p->ch);
p=p->next;
}
printf("%c\n",p->ch);
}
int
main(void)
{
LNode
*head;
head
=
Createlist(head);//鏈表初始化
ListPrint(head);
//列印單鏈表數據
head
=
EncryptList(head);
ListPrint(head);
return
0;
}
看看。