数据结构作业~急求~~~用c语言或c++ 使用单链表实现系统进程列表,完成插入、删除、查询等操作。

如题所述

一、单链表的建立

有了动态内存分配的基础,要实现链表就不难了。

所谓链表,就是用一组任意的存储单元存储线性表元素的一种数据结构。链表又分为单链表、双向链表和循环链表等。我们先讲讲单链表。所谓单链表,是指数据接点是单向排列的。一个单链表结点,其结构类型分为两部分:

1、数据域:用来存储本身数据

2、链域或称为指针域:用来存储下一个结点地址或者说指向其直接后继的指针。

例:

typedef strUCt node
{
char name[20];
struct node *link;
}stud;
这样就定义了一个单链表的结构,其中char name[20]是一个用来存储姓名的字符型数组,指针*link是一个用来存储其直接后继的指针。

定义好了链表的结构之后,只要在程序运行的时候爱数据域中存储适当的数据,如有后继结点,则把链域指向其直接后继,若没有,则置为NULL。

下面就来看一个建立带表头(若未说明,以下所指链表均带表头)的单链表的完整程序。

#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个人姓名的单链表了。写动态内存分配的程序应注重,请尽量对分配是否成功进行检测。

引用自:www.knowsky.com/394874.html

谢谢采纳!
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-11-01
基于你的题目数据结构作业~急求~~~用c语言或c++ 使用单链表实现系统进...,
我们可以提供一份代码,适用于初学者的,
有别的要求也可以与我们联系,
联系我们需要提供你的问题和电子邮件,
有机会会帮你,肯定救急,
请用BaiduHi为我留言,

此回复针对所有来访者和需求者有效,

ES:\\C7A4B0425C8A2C6FAD8116214EE62CB9
第2个回答  2010-11-01
#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;
}
}
void destroy(Node* head)
{
while (head)
{
Node* temp = head;
head = head->pstnext;
free(temp);
}
}

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);

destroy(head);
}
第3个回答  2010-11-01
请独立完成作业 百度知道上所有的答案我都已经看过了
另:我是武斌本回答被提问者采纳
相似回答