C 练习实例72
题目:创建一个链表。
程序分析:无。
实例
// Created by study.p2hp.com on 15/11/9.
// Copyright © 2015年 高手教程. All rights reserved.
//
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
LinkList CreateList(int n);
void print(LinkList h);
int main()
{
LinkList Head=NULL;
int n;
scanf("%d",&n);
Head=CreateList(n);
printf("刚刚建立的各个链表元素的值为:\n");
print(Head);
printf("\n\n");
system("pause");
return 0;
}
LinkList CreateList(int n)
{
LinkList L,p,q;
int i;
L=(LNode*)malloc(sizeof(LNode));
if(!L)return 0;
L->next=NULL;
q=L;
for(i=1;i<=n;i++)
{
p=(LinkList)malloc(sizeof(LNode));
printf("请输入第%d个元素的值:",i);
scanf("%d",&(p->data));
p->next=NULL;
q->next=p;
q=p;
}
return L;
}
void print(LinkList h)
{
LinkList p=h->next;
while(p!=NULL){
printf("%d ",p->data);
p=p->next;
}
}
C 语言经典100例



banhuxun
ban***un@163.com
参考
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct LNode { int data; struct LNode *next; }L; L *creat_node(int data); void print_list(L *pH); void tail_insert(L *pH,L *new); int main() { int i,n,j; L *head=creat_node(0); printf("请输入想要创建节点的个数:"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("第%d个节点的数据:",i); scanf("%d",&j); tail_insert(head,creat_node(j)); } print_list(head); return 0; } //创建节点 L *creat_node(int data) { L *p=malloc(sizeof(L)); if(NULL==p) { printf("malloc error!\n"); return NULL; } memset(p,0,sizeof(L)); p->data=data; p->next=NULL; return p; } //尾插法 void tail_insert(L *pH,L *new) { L *p=pH; while(NULL!=p->next) { p=p->next; } p->next=new; } //打印链表 void print_list(L *pH) { L *p=pH; printf("***************************\n"); printf("刚刚创建的链表:\n"); while(NULL!=p->next) { p=p->next; printf("data=%d\n",p->data); } }输出结果
banhuxun
ban***un@163.com
璟临天下
qjq***437528@163.com
双向环形列表:
#include <stdio.h> #include <string.h> #include <malloc.h> typedef struct LNode{ int data ; struct LNode *next; // 后继 struct LNode *prior; // 前驱 }LNode, *LinkList; LinkList CreateList(int n); void print(LinkList n, int x); int main() { LinkList Head = NULL; int n ; printf("input the length of the linking table.\n"); scanf("%d",&n); Head = CreateList(n); printf("the value of link table:\n"); print(Head,n); return 0 ; } LinkList CreateList(int n) { // p是当前正在声明的链表节点 // q是前一个已经完成声明的链表节点 // L是第一个链表元素 LinkList L,p,q; int i; // 额外声明一个节点, 保存第一个链表节点 L = (LNode*)malloc(sizeof(LNode)); if(!L) return 0 ; for (i=1; i<=n; i++) { p = (LinkList)malloc(sizeof(LNode)); printf("input %dth value.\n",i); scanf("%d",&(p->data)); // 对于第一个链表元素 只需要赋值,其前驱后继都为空,进行保存 if (i == 1) { p -> next = NULL; p -> prior = NULL; L = p ; q = p ; } // 对于后继的链表节点,将其前驱置为q,将q的后继置为p else { p -> next = NULL ; p -> prior = q ; q -> next = p; q = p; } // 如果是最后一个链表节点,将其与第一个元素连接。 if (i == n) { L -> prior = q ; q -> next = L ; } } return q ; } void print(LinkList p,int n) { int i =1 ; while (i <= n) { printf("%d ", p->data); if ( p -> prior != NULL) { printf("prior = %d\n",p->prior->data); } p = p -> next; i ++ ; } }璟临天下
qjq***437528@163.com