Ⅰ 求c語言單鏈表 源代碼
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct people
{
char name[10];
int age;
struct people * next;
};
int main()
{
struct people * head=NULL;
struct people * prev , * current;
int flag=1;
while(flag!=0)
{
printf("請輸入學生姓名,年齡:(年齡輸入0結束所有輸入工作)
");
current=(struct people *)malloc(sizeof(struct people));
if(head==NULL)
head=current;
else
prev->next=current;
current->next=NULL;
scanf("%s",¤t->name);
scanf("%d",¤t->age);
prev=current;
flag=current->age;
}
printf("Output:
");
if(head==NULL)
printf("無資料。
");
else
{
current=head;
while(current->next!=NULL)
{
printf("姓名:%s
年齡:%d
",current->name,current->age);
current=current->next;
}
}
}
至於排序,斷開舊鏈表,將前後指針鏈接到新的節點就好
如果還有問題歡迎再問哈
Ⅱ 用C語言實現建立一個單鏈表的過程,並實現列印鏈表中每一個元素,寫出完整程序
這是個很簡單的鏈表創建和輸出
#include<stdio.h>
#include<stdlib.h>
typedef struct linkednode
{
char data;
struct linkednode *next;
}node,*link_list;//鏈表節點的結構及重命名
link_list creat()//創建一個鏈表返回類型是鏈表的首地址
{
link_list L;
node *p1,*p2;
char data;
L=(node*)malloc(sizeof(node));//開辟存儲空間
p2=L;
while((data=getchar())!=' ')//輸入回車鍵時結束輸入
{
p1=(node*)malloc(sizeof(node));
p1->data=data;
p2->next=p1;
p2=p1;
}
p2->next=NULL;
return L;
}
void print(link_list L)//把鏈表輸出
{
node *p;
p=L->next;
while(p!=NULL)
{
printf("%c",p->data);
p=p->next;
}
printf(" ");
}
void main()
{
link_list L=NULL;
char x;
printf("請輸入鏈表節點: ");
L=creat();
print(L);
}
Ⅲ C語言鏈表基礎代碼
比較簡單的插入和刪除 特殊情況沒有考慮。
採用的是帶頭結點的尾插法建立鏈表。
這個程序寫的插入是在某個元素之前插入,特殊情況就是在第一個元素之前插入沒有考慮
刪除也有特殊情況沒有考慮,刪除第一個元素沒有考慮
下面是代碼:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node * next;
};
int main ()
{
struct node *head,*p,*tail,*t;
head = (struct node *)malloc (sizeof (struct node));
head -> next = NULL;
tail = head;
int n;
printf("請輸入元素個數: \n");
scanf("%d",&n);
while (n--)
{
p = (struct node *)malloc (sizeof (struct node));
scanf ("%d",&p->data);
p -> next = NULL;
tail -> next = p;
tail = p;
}
printf ("插入一個元素\n請輸入要在哪個元素之前插入:");
scanf ("%d",&n);
p = head -> next;
while (p != NULL)
{
t = p -> next;
if (t -> data == n)
{
struct node *temp;
temp = (struct node *)malloc (sizeof (struct node));
temp -> next = NULL;
printf("請輸入要插入的元素:");
scanf ("%d",&temp->data);
temp -> next = p -> next ;
p -> next = temp;
break;
}
p = p -> next;
}
p = head -> next;
while (p != NULL)
{
printf ("%d ",p->data);
p = p -> next;
}
printf ("刪除一個元素\n請輸入要刪除哪個元素:");
scanf ("%d",&n);
p = head -> next;
while (p != NULL)
{
t = p -> next;
if (t -> data == n)
{
p -> next = t -> next;
t -> next = NULL;
break;
}
p = p -> next;
}
p = head -> next;
while (p != NULL)
{
printf ("%d ",p->data);
p = p -> next;
}
return 0;
}
Ⅳ 求C語言線性表中的順序表和鏈表的完整創建代碼,謝謝大神
//xieai999
#include <stdio.h>
typedef struct sty
{
char r[1000];
int len;
}Sqlist;//順序表
typedef struct e
{
char e;
struct e *next;
}LT;//鏈表
void CreatLT(LT **s,char a[])//初始化鏈表
{
LT *p,*q;
(*s)=(LT *)malloc(sizeof(LT));
(*s)->e=a[0];
(*s)->next=NULL;
q=(*s);
int i=1;
while(a[i]!='\\0')
{
p=(LT *)malloc(sizeof(LT));
p->e=a[i];
p->next=NULL;
q->next=p;
q=q->next;
i++;
}
}
void linkTOlist(LT *s,Sqlist **r)//轉換
{
(*r)=(Sqlist *)malloc(sizeof(Sqlist));
int i=0;
while(s!=NULL)
{
(*r)->r[i]=s->e;//鏈表轉換成順序表
s=s->next;
i++;
}
(*r)->len=i;
}
main()
{
char s[1000];
printf("請輸入初始化的字元串\
");
scanf("%s",&s);
LT *A;Sqlist *B;
CreatLT(&A,s);
printf("轉換進行中......\
");
linkTOlist(A,&B);
printf("轉換後輸出為\
");
int k=0;
for(k;k<B->len;k++)
printf("%c ",B->r[k]);
return 0;
}
Ⅳ 求一個C語言鏈表源程序代碼
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *next;
};
/*建立鏈表*/
struct node *creat(int n)
{
int x, i;
struct node *head, *p, *r;
head=(struct node*)malloc(sizeof(struct node));
r=head;
printf("請輸入數字\r\n");
for(i=0; i<n; i++)
{
scanf("%d", &x);
p=(struct node*)malloc(sizeof(struct node));
p->num=x;
r->next=p;
r=p;
}
r->next=NULL;
return(head);
}
/*刪除重復結點*/
void delet(struct node *head)
{
struct node *p, *q, *r;
p=head->next;
while(p!=NULL)
{
q=p;
while(q->next!=NULL)
{
r=q->next;
if(r->num==p->num)
{
if(r->next!=NULL)
{
q->next=r->next;
free(r);
}
else
{
q->next=NULL;
free(r);
}
}
else
{
q=r;
}
}
p=p->next;
}
}
/*排序*/
void sort(struct node *head)
{
struct node *p, *q, *small;
int temp;
for(p=head->next; p->next!=NULL; p=p->next)
{
small=p;
for(q=p->next; q!=NULL ;q=q->next)
{
if(q->num<small->num)
small=q;
}
if(small!=p)
{
temp=small->num;
small->num=p->num;
p->num=temp;
}
}
}
/*輸出*/
void output(struct node *head)
{
struct node *pt;
pt=head->next;
while(pt!=NULL)
{
printf("%d\r\n", pt->num);
pt=pt->next;
}
}
main()
{
int n;
struct node *head;
printf("輸入數字的個數n\r\n");
scanf("%d", &n);
head=creat(n);
printf("輸入的數字\r\n");
output(head);
delet(head);
printf("刪除重復結點後輸出數字\r\n");
output(head);
sort(head);
printf("排序後輸出數字\r\n");
output(head);
free(head);
}
希望能對你有幫助,俺也學C不到兩個月,共同進步啊!
Ⅵ c語言鏈表代碼
#include<stdio.h>
intmutuallyprime(inta[100],intb[100])
{
inti,j,n;
for(i=0,n=0;a[i];i++)
{
for(j=0;b[j];j++)
{
if(a[i]==b[j])
n++;
}
}
if(n>1)
return0;
else
return1;
}
intmain()
{
intm,n,a[100],b[100],i,j;
scanf("%d%d",&m,&n);
for(i=1,j=0;i<=m;i++)
{
if(!(m%i))
{
a[j]=i;
j++;
}
}
a[j]=0;
for(i=1,j=0;i<=n;i++)
{
if(!(n%i))
{
b[j]=i;
j++;
}
}
b[j]=0;
if(mutuallyprime(a,b))
printf("%d與%d為互質數 ",m,n);
else
printf("%d與%d不為互質數 ",m,n);
return0;
}
Ⅶ C語言「迴文」程序代碼
首先我對你的 "並且當輸入的字元串第一個字元為#時,輸入為空時,不輸出。" 這句話比較費解, 不明白你的意思!
你把這說清楚了我再補充回答你~
我寫了個參考代碼給你參考, 首先是輸入你要判斷的字元串的個數, 然後再依次輸入所有的字元串, 最後判斷輸入的所有字元串是否是"迴文"! 因為不理解你那句話, 所以暫時沒做什麼空和什麼"#"處理.
詳細c代碼:
#include <stdio.h>
#include <string.h>
#define STR_LEN 128
#define STR_NUM 64
int main()
{
int i = 0, j = 0, n = 0;
int len = 0;
char *start = NULL;
char *end = NULL;
char str[STR_NUM][STR_LEN] = {0};
printf("Please input the number of string: \n");
scanf("%d", &n);
printf("Please input all the string: \n");
for (i = 0; i < n; i++)
{
scanf("%s", str[i]);
}
for (j = 0; j < n; j++)
{
start = str[j];
len = strlen(str[j]);
end = str[j] + len - 1;
while (start - end <= 0)
{
if (*start++ != *end--)
{
break;
}
}
if (start > end)
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
return 0;
}
例子,運行後:
Please input the number of string:
4
Please input all the string:
aba
112ds
madam
xyzyx
yes
no
yes
yes
Press any key to continue
補充回答:
大概明白你的意思,我會把例子貼上, 若不符合你的要求我再幫修改!
詳細代碼如下:
#include <stdio.h>
#include <string.h>
#define STR_LEN 128
#define STR_NUM 64
int main()
{
int i = 0;
int len = 0;
int str_count = 0;
char *start = NULL;
char *end = NULL;
char str[STR_NUM][STR_LEN] = {0};
printf("Please input all the string: \n");
while (1)
{
scanf("%s", str[str_count]);
if (str[str_count][0] == '#')
{
break;
}
else
{
str_count++;
continue;
}
}
for (i = 0; i < str_count; i++)
{
start = str[i];
len = strlen(str[i]);
end = str[i] + len - 1;
while (start - end <= 0)
{
if (*start++ != *end--)
{
break;
}
}
if (start > end)
{
printf("yes\n");
}
else
{
printf("no\n");
}
}
return 0;
}
運行實例1:
Please input all the string:
xyzyx
adghf
#
yes
no
Press any key to continue
運行實例2:
Please input all the string:
1232ss
sakljfkla
333dafs
aba
ee3
xyzyx
dfj222
madam
111$111
slsl33
#
no
no
no
yes
no
yes
no
yes
yes
no
Press any key to continue
Ⅷ C語言鏈表
#include<stdio.h>
#include<stdlib.h>
#defineN8
typedefstructlist
{intdata;
structlist*next;
}SLIST;
SLIST*insertlist(SLIST*p,int*pvalue)
{
SLIST*tmp=p,*tmp_p;
intt=*pvalue;
while(tmp->next!=NULL&&tmp->next->data<t)
tmp=tmp->next;
tmp_p=(SLIST*)malloc(sizeof(SLIST));
tmp_p->next=tmp->next;
tmp_p->data=t;
tmp->next=tmp_p;
returnp;
}
SLIST*creatlist(int*a)
{
SLIST*h,*p,*q;inti;
h=p=(SLIST*)malloc(sizeof(SLIST));
for(i=0;i<N;i++)
{q=(SLIST*)malloc(sizeof(SLIST));
q->data=a[i];
q->next=NULL;
p->next=q;
p=q;
}
p->next=0;
returnh;
}
voidoutlist(SLIST*h)
{
/*這里輸出鏈表中各個數據*/
SLIST*tmp=h->next;
while(tmp!=NULL){
printf("%d",tmp->data);
tmp=tmp->next;
}
}
intmain(void)
{
SLIST*head;
inta[N];
intnvalue,i;
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
scanf("%d",&nvalue);//插入的結點數據
head=creatlist(a);
head=insertlist(head,&nvalue);
outlist(head);
return0;
}