⑴ 如何用c语言编写一个链表
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
struct Node
{
int data;//数据域
struct Node * next;//指针域
};
/*************************************************************************************
*函数名称:Create
*函数功能:创建链表.
*输入:各节点的data
*返回值:指针head
*************************************************************************************/
struct Node * Create()
{
struct Node *head,*p1,*p2;
head = NULL;
p1 = p2 = (struct Node *)malloc(sizeof(struct Node));
printf("Input the linklist (Input 0 to stop):\n");
scanf("%d",&p1->data);
while(p1->data!=0)
{
if(head == NULL){
head = p1;
}else{
p2->next = p1;
p2 =p1;
}
p1 = (struct Node *)malloc(sizeof(struct Node));
scanf("%d",&p1->data);
}
p2->next = NULL;
return head;
}
/*************************************************************************************
*函数名称:insert
*函数功能:在链表中插入元素.
*输入:head 链表头指针,p新元素插入位置,x 新元素中的数据域内容
*返回值:无
*************************************************************************************/
void insert(struct Node * head,int p,int x)
{
struct Node * tmp = head;
struct Node * tmp2 ;
int i ;
for(i = 0;i<p;i++)
{
if(tmp == NULL)
return ;
if(i<p-1)
tmp = tmp->next;
}
tmp2 = (struct Node *)malloc(sizeof(struct Node));
tmp2->data = x;
tmp2->next = tmp->next;
tmp->next = tmp2;
}
/**************************************************************************************
*函数名称:del
*函数功能:删除链表中的元素
*输入:head 链表头指针,p 被删除元素位置
*返回值:被删除元素中的数据域.如果删除失败返回-1
**************************************************************************************/
int del(struct Node * head,int p)
{
struct Node * tmp = head;
int ret , i;
for(i = 0;i<p;i++)
{
if(tmp == NULL)
return -1;
if(i<p-1)
tmp = tmp->next;
}
ret = tmp->next->data;
tmp->next = tmp->next->next;
return ret;
}
/**************************************************************************************
*函数名称:print
*函数功能:打印链表中的元素
*输入:head 链表头指针
*返回值:无
**************************************************************************************/
void print(struct Node *head)
{
struct Node *tmp;
for(tmp = head; tmp!=NULL; tmp = tmp->next)
printf("%d ",tmp->data);
printf("\n");
}
/**************************************************************************************
*函数名称:main
*函数功能:主函数创建链表并打印链表。
**************************************************************************************/
int main(){
struct Node * head = Create();
print(head);
return 0;
}
⑵ 用C语言编辑链表,怎么编辑呢
可能形式上有变化,单链表的本质是一样的:
定义链表:
typedef struct linklist
{
Data;
struct linklist *next;
}linklist;
这是链表的基本单元,
链表都是由这些基本单元构成的
创建链表的方法很多,据了解学过的几种循环都可以,在这给你演示一种:
#include<stdio.h>
#include<malloc.h>
typedef struct link
{
int num;
struct link *next;
}link;
link* creaetlink()
{
link *head ,*p,*q;
head = p = (link *)malloc(sizeof(link))
printf("请输入数据:\n")
q = (link *)malloc(sizeof(link));
scanf("%d",&q->num);
while(num )
{
p-> next = q ;
p = q;
q = (link*)malloc(sizeof(link));
scanf("%d",&q->num);
}
free(q);
p->next = NULl;
return head;
}
这样的资料很多,你有基础,回去看一下熟悉一下,自己在电脑上敲两遍就熟悉了
⑶ C语言 用链表形式编写
#include<windows.h>
#include<stdio.h>
#include<stdlib.h>
#define L sizeof(struct cargo)
struct cargo
{
long num;
char name[10];
float price;
int quantity;
struct cargo *next;
};
int n;
struct cargo *km()//输入
{
struct cargo *head;
struct cargo *p1,*p2;
n=0;
p1=p2=(struct cargo *)malloc(L);
system("color 1");
printf("请输入货物的编码,名字,价格,数量\n");
system("color 2");
scanf("%ld %s %f %d",&p1->num,&p1->name,&p1->price,&p1->quantity);
head=NULL;
while (p1->num!=0&&p1->next!=NULL)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct cargo*)malloc(L);
scanf("%ld %s %f %d",&p1->num,&p1->name,&p1->price,&p1->quantity);
}
p2->next=NULL;
return head;
}
void print(struct cargo *head)//查全部信息
{
struct cargo *p;
printf("尊敬的客户您所查的全部信息共有%d件。\n",n);
system("color 7");
p=head;
if(head!=NULL)
{
printf("具体信息如下:\n");
printf(" ------------------------------------------------------------------------------\n");
printf(" | 商品编号 | 商品名称 | 商品价格 | 商品数量 |\n");
printf(" | | | | |\n");
while (p!=NULL)
{
printf(" ---------------------------------------------------------------------\n");
printf(" | %ld | %s | %.2f | %d |\n",p->num,p->name,p->price,p->quantity);
p=p->next;
}
printf(" ---------------------------------------------------------------------\n");
}
else printf("这是一个空链表!\n");
}
void revise(struct cargo *head,long num)//修改
{
struct cargo *p;
p=head;
while (p!=NULL)
{
if(p->num==num)
{
printf("请重新输入您需要修改的商品的编号,以示确定:\n");
system("color 5");
scanf("%ld",&p->num);
//sleep(100);
printf("请您把此商品的其他信息依次输入:\n");
system("color 7");
scanf("%s %f %d",&p->name,&p->price,&p->quantity);
break;
}
p=p->next;
}
print(head);
}
delete(struct cargo *head,long num)//删除
{
struct cargo *p1,*p2;
if(head==NULL)
{
printf("It's NULL!\n");
}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->num==num)
{
if(p1==head)
{
head=p1->next;
free(p1);
}
else
{
p2->next=p1->next;
p1=NULL;
free(p1);
}
printf("删除成功!\n");
n--;
}
else printf("没有删除的信息!\n");
print(head);
return head;
}
struct cargo*Insert(struct cargo*head,long num,struct cargo *nod)
{
struct cargo *p1;
if(head==NULL)
{
head=nod;//把内存给头
nod->next=NULL;//只有一个信息则下一个为空
n+=1;
return head;
}
p1=head;
while(p1->num!=num&&p1->next!=NULL)
{
p1=p1->next;
}
if(num==p1->num)
{
nod->next=p1->next;
p1->next=nod;
n+=1;
}
else
printf("没有所要插入的节点!\n");
print(head);
return head;
}
struct cargo*find1(struct cargo *head,long num)
{
struct cargo*p;
p=head;
if(head==NULL)
{
printf("空链表!\n");
return head;
}
while(p->num!=num&&p->next!=NULL)
{
p=p->next;
}
if(num==p->num)
{
printf(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf(" @ %ld @ %s @ %.2f @ %d @\n",p->num,p->name,p->price,p->quantity);
}
return head;
}
struct cargo*find2(struct cargo *head,char name[])
{
struct cargo*p;
p=head;
if(head==NULL)
{
printf("空链表!\n");
return head;
}
while(p->name!=name&&p->next!=NULL)
{
p=p->next;
}
if(name==p->name)
{
printf(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf(" @ %ld @ %s @ %.2f @ %d @\n",p->num,p->name,p->price,p->quantity);
}
return head;
}
struct cargo*find3(struct cargo *head,float price)
{
struct cargo*p;
p=head;
if(head==NULL)
{
printf("空链表!\n");
return head;
}
while(p->price!=price&&p->next!=NULL)
{
p=p->next;
}
if(price==p->price)
{
printf(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf(" @ %ld @ %s @ %f @ %d @\n",p->num,p->name,p->price,p->quantity);
}
return head;
}
struct cargo*find4(struct cargo *head,int quantity)
{
struct cargo*p;
p=head;
if(head==NULL)
{
printf("空链表!\n");
return head;
}
while(p->quantity!=quantity&&p->next!=NULL)
{
p=p->next;
}
if(quantity==p->quantity)
{
printf(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf(" @ %ld @ %s @ %f @ %d @\n",p->num,p->name,p->price,p->quantity);
}
return head;
}
{
struct cargo *head=NULL;
struct cargo *pre;
int a,myquantity;
char ch;
char name[20];
float myprice;
long thnum;
printf(" @_@欢迎进入仓库管理系统@_@ \n");
printf("***************************************************************************\n");
printf("* *\n");
printf("* 欢 库*\n");
printf("* *\n");
printf("* *\n");
printf("* 迎 管*\n");
printf("* *\n");;
printf("* *\n");
printf("* 进 输入0代表输入信息, 如果输入四个0代表结束 理*\n");
printf("* *\n");
printf("* *\n");
printf("* 入 系*\n");
printf("* *\n");
printf("* *\n");
printf("* 仓 统*\n");
printf("* *\n");
printf("***************************************************************************\n");
system("color 5");
while(1)
{
printf("请输入您需要的功能:\n");
printf("输入1代表修改货物信息\n");
printf("输入2代表查看全部信息\n");
printf("输入3代表删除信息\n");
printf("输入4代表插入一个新的商品\n");
printf("输入5查找商品信息\n");
system("color 5");
printf("输入6保存且退出系统!\n");
system("color 6");
printf("请输入信息:\n");
printf("请做出选择[ ]\b\b\b ");
scanf("%d",&a);
if(a==0)
{
head=km();
print(head);
}
if(a==2)
{
print(head);
}
if(a==1)
{
printf("请输入您要修改的商品编号:\n");
scanf("%ld",&thnum);
revise(head,thnum);
}
if(a==3)
{
printf("请输入您要删除的商品编号:\n");
scanf("%ld",&thnum);
delete(head,thnum);
}
if(a==4)
{
printf("请输入想要插入的位置:\n");
scanf("%ld",&thnum);
pre=(struct cargo*)malloc(L);
printf("输入插入的信息:\n");
scanf("%ld %s %f %d", &pre->num,&pre->name,&pre->price,&pre->quantity);
Insert(head,thnum,pre);
}
if(a==5)
{
printf("请输入查找方式:\n");
printf("@@@@@@输入a代表用商品编号查找@@@@@@\n");
printf("@@@@@@输入b代表用商品名字查找@@@@@@\n");
printf("@@@@@@输入c代表用商品价格查找@@@@@@\n");
printf("@@@@@@输入d代表用商品数量查找@@@@@@\n");
printf("请输入信息:\n");
printf("请做出选择[ ]\b\b\b ");
scanf("%c",&ch);
if(ch=='a')
{
printf("@@@@@@输入商品编号进行查找@@@@@@\n");
scanf("%ld",&thnum);
find1(head,thnum);
}
else if(ch=='b')
{
printf("@@@@@@输入商品名字进行查找@@@@@@\n");
scanf("%s",&name);
find2(head,name);
}
else if(ch=='c')
{
printf("@@@@@@输入商品价格查找@@@@@@\n");
scanf("%f",&myprice);
find3(head,myprice);
}
else if(ch=='d')
{
printf("@@@@@@输入商品数量查找@@@@@@\n");
scanf("%d",&myquantity);
find4(head,myquantity);
}
else
{
printf("选择错误!!\n");
}
}
if(a==6)
{
printf("您已经退出仓库管理系统,谢谢您的使用!\n");
break;
}
}
system("pause");
return 0;
}
⑷ 用C语言编写一个链表
看完你下面的追问 其实 意思是
让你把一个已有的 单链表
变成反向的单链表 对吧
⑸ 帮我编写一个用C语言编写的单链表的建立,和输入输出操作,谢谢各位
#include <stdio.h>
#include <windows.h>
typedef struct node
{
int num;
struct node *next;
}lnode;
lnode *creat()
{
lnode *head,*p,*q;
int n;
head=NULL;
printf("输入要创建的节点数\n");
scanf("%d",&n);
while(n)
{
p=(lnode *)malloc(sizeof(lnode));
printf("输入数据\n");
scanf("%d",&p->num);
if (head==NULL)
{
head=q=p;
}
else
{
q->next=p;
q=p;
}
n--;
}
q->next=NULL;
return head;
}
lnode *insert(lnode *head)
{
lnode *p,*q,*s;
int n;
char ch;
q=p=head;
printf("输入插入的位置\n");
scanf("%d",&n);
printf("请选择是插入在前还是在后(F or B)\n");
getchar();
ch=getchar();
if(ch=='F'||ch=='f')
{
s=(lnode *)malloc(sizeof(lnode));
printf("请输入数据\n");
scanf("%d",&s->num);
while(p&&--n)
{
q=p;
p=p->next;
}
if (q==p)
{
s->next=q;
return s;
}
else
{
q->next=s;
s->next=p;
return head;
}
}
else if (ch=='B'||ch=='b')
{
s=(lnode *)malloc(sizeof(lnode));
printf("请输入数据\n");
scanf("%d",&s->num);
while(p&&n--)
{
q=p;
p=p->next;
}
if (NULL==q->next)
{
q->next=s;
s->next=NULL;
return head;
}
else
{
q->next=s;
s->next=p;
return head;
}
}
else
{
printf("输入错误\n");
}
}
lnode *del(lnode *head)
{
lnode *p,*q;
int n;
int flag=0;
p=q=head;
printf("请输入删除的数据\n");
scanf("%d",&n);
while(p)
{
if (p->num==n)
{
flag=1;
if (head==p)
{
head=head->next;
}
else if(NULL==p->next)
{
q->next=NULL;
}
else
{
q->next=p->next;
}
}
q=p;
p=p->next;
}
if (flag==0)
{
printf("没有找到数据\n");
system("pause");
}
else
{
printf("删除成功\n");
system("pause");
}
return head;
}
lnode *sort(lnode *head)
{
lnode *t,*f,*min,*p_min,*s;
char ch;
f=NULL;
printf("请输入排序方式:升序(A/a),降序(D/d)\n");
getchar();
ch=getchar();
if (ch=='A'||ch=='a')
{
while(NULL!=head)
{
for (min=head,s=head;s->next!=NULL;s=s->next)
{
if (min->num>s->next->num)
{
p_min=s;
min=s->next;
}
}
if (NULL==f)
{
f=min;
t=min;
}
else
{
t->next=min;
t=min;
}
if (min==head)
{
head=head->next;
}
else
{
p_min->next=min->next;
}
}
if (f!=NULL)
{
t->next=NULL;
}
printf("排序完成\n");
system("pause");
head=f;
return f;
}
else if (ch=='D'||ch=='d')
{
while(NULL!=head)
{
for (min=head,s=head;s->next!=NULL;s=s->next)
{
if (min->num<s->next->num)
{
p_min=s;
min=s->next;
}
}
if (NULL==f)
{
f=min;
t=min;
}
else
{
t->next=min;
t=min;
}
if (min==head)
{
head=head->next;
}
else
{
p_min->next=min->next;
}
}
if (f!=NULL)
{
t->next=NULL;
}
printf("排序完成\n");
system("pause");
head=f;
return f;
}
}
void dispaly(lnode *head)
{
lnode *p;
p=head;
printf("\n");
while(p!=NULL)
{
printf("%d\t",p->num);
p=p->next;
}
}
int getoption()
{
int n;
printf("0 退出\n");
printf("1 创建链表\n");
printf("2 插入节点\n");
printf("3 删除节点\n");
printf("4 排序节点\n");
printf("5 显示链表\n");
printf("请选择操作\t");
scanf("%d",&n);
return n;
}
int main()
{
lnode *temp;
char ch;
int o;
do
{
system("cls");
o=getoption();
switch (o)
{
case 0:
exit(0);
break;
case 1 :
system("cls");
temp=creat();
break;
case 2:
system("cls");
temp=insert(temp);
break;
case 3:
system("cls");
temp=del(temp);
break;
case 4:
system("cls");
temp=sort(temp);
break;
case 5:
system("cls");
dispaly(temp);
system("pause");
break;
}
system("cls");
printf("按0退出,任意键继续\t");
ch=getchar();
if (ch=='0')
{
exit(0);
}
} while (ch!='0');
}
⑹ 用C语言编一个用链表的程序,求平均值的
如果链表的节点为:typedef struct node
{
double data;
struct node *next;
}Node; 则可以这样写: double avgfun(Node *h){ int n=0; double count=0; Node *p;
p = (Node *)malloc(sizeof(Node));
p = h;
while(p->next!=NULL)
{
p = p->next;
count += p->data; n++;
} return count/n;}
⑺ 求c语言链表编程
简单写了个!!看看运行一下
#include<iostream>
#include<conio.h>
using namespace std;
#define LENGTH 20
#define OK 1
#define ERROR 0
typedef long LElemType; /*学号变量类型*/
typedef int IElemType; /*成绩变量类型*/
typedef char CElemType;
typedef int Status; /*返回值的类型*/
typedef struct student
{
LElemType num;
CElemType name[LENGTH];
CElemType sex[LENGTH];
IElemType age;
student *next;
}node;
typedef struct LIST
{
node *head,*tail;
}link;
Status Init(link *s)
{
s->head = s->tail = (node *)malloc(sizeof(node)); /*头结点*/
if(NULL == s->head)
{
cout<<"分配失败!"<<endl;
return ERROR;
}
s->head->num = 0; /*头节点的学号用来放置节点个数*/
s->head->next = NULL;
return OK;
}
node *MakeNode(int i) /*生成存放学生信息的节点*/
{
node *p=(node *)malloc(sizeof(node));
cout<<"请输入第"<<(i+1)<<"个学生的信息: ";
cin>>p->num>>p->name>>p->sex>>p->age;
p->next = NULL;
return p;
}
Status Insert(link *s,int n)
{
for(int i=0; i<n; i++) /*录入n个学生的信息*/
{
node *p = MakeNode(i);
s->tail->next = p;
s->tail = p;
s->head->num++; /*修改头结点中存放学生信息条数每增加一个节点就要++1*/
}
return OK;
}
Status display(link *s)
{
node *p = s->head->next; /*让p指针指向头结点的下一个节点,因为头结点只用来存放了学生的人数*/
if(!p)
{
cout<<"还没有学生信息!"<<endl;
return ERROR;
}
while(p)/*只要p指针指向不为空就要输出*/
{
cout<<p->num<<" "<<p->name<<" "<<p->sex<<" "<<p->age<<endl;
p = p->next;
}
return OK;
}
Status SortByNum(link *s) /*按照学号升序排序*/
{
int n = s->head->num;
node *p,*cur,*next;
for(int i=0; i<n-1; i++)
{
p= s->head;
cur=p->next;
next = cur->next;
for(int j=0; j<n-1-i; j++)
{
if(next->num<cur->num)
{
p->next=next; /*若前面的大于后面的就要交换前后两个值*/
cur->next = next->next;
next->next = cur;
p=next; /*交换之后要修改当前指针与当前指针与下一个指针的位置*/
next=cur->next;
}else{ /*挨着的两个值,前面的不大于后面的只要将指针往后面移动*/
p=cur;
cur = next;
next = next->next;
}
}
}
return OK;
}
Status DeleteStu(link *s,long *num)
{
if(s->head->next==NULL)
{
cout<<"没有任何学生信息记录"<<endl;
return ERROR;
}
else
{
cout<<"请输入你要删除的学生的学号:";
cin>>*num;
node *cur,*p;
cur = s->head;
while(cur->next)
{
if(cur->next->num == *num)
{
break;
}
else{
cur = cur->next;
}
}
if(cur->next)
{
p = cur->next;
cur->next = p->next;
p->next = NULL;
free(p);
s->head->num--;
return OK;
}else{
cout<<"不存在该学生的信息"<<endl;
return ERROR;
}
}
}
Status QueryStu(link *s,long num)
{
node *p = s->head->next; /*让p指针指向头结点的下一个节点,因为头结点只用来存放了学生的人数*/
if(!p)
{
cout<<"还没有学生信息!"<<endl;
return ERROR;
}
while(p)/*只要p指针指向不为空就要输出*/
{
if(p->num==num)
{
cout<<p->num<<" "<<p->name<<" "<<p->sex<<" "<<p->age<<endl;
break;
}
p = p->next;
}
cout<<"没有该学生信息!"<<endl;
return OK;
}
Status Menu()
{
int k=-1;
while(k<1||k>5){
system("cls");
cout<< " 学生信息管理程序实现\n"
" 1、学生信息的录入 \n"
" 2、学生信息的显示 \n"
" 3、学生信息的查询 \n"
" 4、学生信息的删除 \n"
" 5、系统推出 \n\n\n";
if(k==-1)
{
cout<<"请选择功能:";
}else if(k<1 || k>5)
{
cout<<"(选择错误)请重新选择:"<<endl;
}
fflush(stdin);
scanf("%d",&k);
}
return k;
}
int main()
{
link s;
int n=0,k=-1;
long num;
Init(&s);
do{
switch(k=Menu())
{
case 1:
cout<<"你想录入多少学生的记录:n=";
fflush(stdin);
scanf("%d",&n);
if(n!=0)
{
Insert(&s,n);
cout<<n<<"个学生信息录入完毕...";
}
else{
cout<<"输入学生个数要大于0的正整数...";
}
getch();
break;
case 2:
puts("所有学生的信息: \n");
SortByNum(&s);
display(&s);
cout<<"所有信息显示完毕...";
getch();
break;
case 3:
cout<<"输入要查询学生的学号:";
cin>>num;
QueryStu(&s,num);
getch();
break;
case 4:
if(DeleteStu(&s,&num))
{
cout<<"删除操作成功...";
}
else{
cout<<"删除操作失败...";
}
getch();
break;
case 5:
cout<<"成功退出了系统"<<endl;
exit(0);
break;
default:
break;
}
}while(k!=5);
return OK;
}
⑻ 50分求用c语言编写链表程序
写好了,你看下
#include
<stdio.h>
#include
<stdlib.h>
#include
<malloc.h>
typedef
struct
node
{
int
data;
struct
node
*next;
}Node;
void
InitList(Node
**head);
void
CreateList(Node
**head);
void
InsertList(Node
**head,
int
key);
void
DeleteList(Node
**head,
int
key);
void
PrintList(Node
**head);
//初始化链表
void
InitList(Node
**head)
{
(*head)
=
(Node
*)malloc(sizeof(Node));
(*head)->next
=
NULL;
}
//创建链表
void
CreateList(Node
**head)
{
int
i;
printf("您好,请输入您要插入的数据:\n");
scanf("%d",
&i);
while(i
!=
0)
{
InsertList(head,
i);
scanf("%d",
&i);
}
}
//插入链表
void
InsertList(Node
**head,
int
key)
{
Node
*p,
*q,
*s;
q
=
(*head);
p
=
(*head)->next;
while(p)
{
q
=
p;
p
=
p->next;
}
s
=
(Node
*)malloc(sizeof(Node));
s->data
=
key;
s->next
=
NULL;
q->next
=
s;
}
//删除链表
void
DeleteList(Node
**head,
int
key)
{
Node
*p,
*q;
q
=
(*head);
p
=
(*head)->next;
while(p
&&
p->data
!=
key)
{
q
=
p;
p
=
p->next;
}
if(p)
{
q->next
=
p->next;
free(p);
p
=
NULL;
}
}
//输出链表
void
PrintList(Node
**head)
{
Node
*p;
p
=
(*head)->next;
while(p)
{
printf("%d\n",
p->data);
p
=
p->next;
}
}
int
main(void)
{
Node
*head;
int
i;
InitList(&head);
CreateList(&head);
printf("删除前的数据:\n");
PrintList(&head);
printf("请输入您要删除的数据:\n");
scanf("%d",
&i);
DeleteList(&head,
i);
printf("删除后的数据:\n");
PrintList(&head);
return
0;
}
Makefile:
#the
simplest
example
OBJS
=
tmp.o
CC
=
gcc
CFLAGS
=
-Wall
-O
-g
tmp:
$(OBJS)
$(CC)
$(OBJS)
-o
tmp
tmp.o:
tmp.c
$(CC)
$(CFLAGS)
-c
tmp.c
-o
tmp.o
clean:
rm
-f
*.o
*~
tmp
您好,请输入您要插入的数据:
1
2
3
4
0
删除前的数据:
1
2
3
4
请输入您要删除的数据:
1
删除后的数据:
2
3
4
⑼ C语言编程关于链表
我有。。以下是程序的一部分。给我邮箱,我发给你吧~
/*链表的基本操作*/
# define NULL 0
# define ERROR 0
# define LEN sizeof(struct linklist)
struct linklist { /*链表的存储结构的表示*/
int data;
struct linklist *next;
};
int n; /*定义n为全局变量*/
struct linklist *head;
struct linklist *create( ) /*创建一个空链表*/
{
struct linklist *p,*q; /* p、q 为指向struct linklist 类型数据的指针变量*/
p=q=(struct linklist*)malloc(LEN); /*开辟一个新单元*/
scanf ("%d",&p->data);
head=NULL;
while (p->data!=0) /*当所输入数据不为0时,则执行循环体*/
{
n=n+1;
if (n==1) head=p;
else q->next=p;
q=p;
p=(struct linklist*)malloc(LEN); /*注要开辟了一个新单元后,才能输入数据
scanf("%d",&p->data);
}
q->next=NULL;
return(head); /*返回链表的头地址*/
}
⑽ 用C语言编这个链表
# 代表结束输出
效果如下
m q i j#
l m y j#
m q i j
l m y j
j m intersection 交集
l m y j q i union of set 并集
#include<stdio.h>
#include<malloc.h>
typedef char ElemType;
typedef struct linklist
{
ElemType data;
struct linklist *next;
}Node,*LinkList;
LinkList createFromTail(void)
{
LinkList head,tail;
char c;
Node *node;
head=tail=(LinkList)malloc(sizeof(Node));
head->next=NULL;
c=getchar();
while(c!='#')
{
node=(LinkList)malloc(sizeof(Node));
node->next=NULL;
node->data=c;
tail->next=node;
tail=node;
c=getchar();
}
return(head);
}
void print(LinkList h)
{
while(h->next!=NULL)
{
printf("%c ",h->next->data);
h=h->next;
}
}
LinkList insec(LinkList la,LinkList lb)
{
LinkList la_curr;
LinkList lb_curr;
LinkList insec_ab,temp;
la_curr=la->next;
insec_ab=(LinkList)malloc(sizeof(Node));
insec_ab->next=NULL;
while(la_curr!=NULL)
{
lb_curr=lb->next;
while(lb_curr!=NULL&&la_curr->data!=lb_curr->data)
{
lb_curr=lb_curr->next;
}
if(lb_curr!=NULL)
{
temp=(LinkList)malloc(sizeof(Node));
temp->data=la_curr->data;
temp->next=insec_ab->next;
insec_ab->next=temp;
}
la_curr=la_curr->next;
}
return(insec_ab);
}
LinkList mg(LinkList la,LinkList lb)
{
LinkList la_curr,la_tail;
LinkList lb_curr;
LinkList head,tail,temp;
la_curr=la->next;
head=tail=(LinkList)malloc(sizeof(Node));
head->next=NULL;
lb_curr=lb->next;
while(lb_curr!=NULL)
{
temp=(LinkList)malloc(sizeof(Node));
tail->next=temp;
temp->next=NULL;
tail=temp;
temp->data=lb_curr->data;
lb_curr=lb_curr->next;
}
while(la_curr!=NULL)
{
lb_curr=lb->next;
while(lb_curr!=NULL&&la_curr->data!=lb_curr->data)
{
lb_curr=lb_curr->next;
}
if(lb_curr==NULL)
{
temp=(LinkList)malloc(sizeof(Node));
tail->next=temp;
temp->next=NULL;
tail=temp;
temp->data=la_curr->data;
}
la_curr=la_curr->next;
}
return(head);
}
int main(void)
{
LinkList la,lb,lc;
printf("Please input character data for list la\n");
la=createFromTail();
printf("Please input character data for list lb\n");
lb=createFromTail();
printf("This is LinkList la\n");
print(la);
printf("\nThis is LinkList lb\n");
print(lb);
lc=insec(la,lb);
printf("\n Output the intersection\n");
print(lc);
printf("\n Output original linklist la\n");
print(la);
printf("\n Output original linklist lb\n");
print(lb);
lc=mg(la,lb);
printf("\n Output the union of set \n");
print(lc);
system("pause");
}