当前位置:首页 > 《线性表的链式存储》实验报告
《线性表的链式存储》实验报告
1.需解决的的问题
利用线性表的链式存储结构,设计一组输入数据。
2.数据结构的定义
structLinklist { longnum;
structLinklist *prior; structLinklist *next; };
3.程序的结构图
创立 新 链表
插入元素 选择菜单,用户选择操作删除元素 查找元素 下一次操作退出程序 输出链表元素
4.函数的功能
1)链表的创建:创建一个新链表,并输入元素。 structLinklist *create() {
structLinklist *head; structLinklist *p; structLinklist *q; long x;
head = (structLinklist*)malloc(sizeof(structLinklist)); q = head; head->next = NULL;
printf(\(0 to END)scanf(\ while(x!=0) {
p = (structLinklist*)malloc(sizeof(structLinklist)); p->num = x; p->next = NULL;
q->next = p; p->prior = q; q = p; scanf(\ }
return head; }
2)元素的插入:在指定的位置插入指定的元素。 void insert(structLinklist *head) {
structLinklist *p;
structLinklist *q = head; inti; int j = 0;
p = (structLinklist*)malloc(sizeof(structLinklist));
printf(\\\n\
scanf(\
printf(\ scanf(\ if(i<1) {
printf(\ printf(\ scanf(\ }
while(j<=i-1 && q->next!=NULL) {
q = q->next; j++; }
if(q->next==NULL && j!=i) {
p->next = NULL; p->prior = q;
q->next = p; } else
{
q->prior->next = p; p->prior = q->prior; p->next = q;
q->prior = q; } }
3)元素的删除:删除指定位置的元素。 void del(structLinklist *head) {
structLinklist *p; structLinklist *q; inti; int j=0;
printf(\ scanf(\ if(i<1) {
printf(\ printf(\ scanf(\ }
p = head;
while(j<=i-1 && p!=NULL) {
p = p->next; j++; } if(p==NULL) {
printf(\ exit(1); } else
{
q = p;
p->prior->next = p->next; p->next->prior = p->prior; p = p->next; free(q); } }
4)元素的查找:查找所有指定元素的所在位置。 void search(structLinklist *head) {
structLinklist *p;
long number, i=1, j=0;
printf(\ scanf(\ p = head->next;
printf(\
while(p!=NULL) {
if(p->num==number) { printf(\ j = 1;
共分享92篇相关文档