赞
踩
-
- #ifndef __HEAD_H_
- #define __HEAD_H_
-
-
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
-
- enum {FALSE=-1,SUCCESS};
- typedef int datatype;
- typedef struct node
- {
- //数据域
- datatype data;
- //指针域
- struct node *next;
- }*linklist;
-
-
- linklist creat();
- linklist insert_head(linklist head,datatype element);
- void output(linklist head);
- linklist insert_rear(linklist head,datatype element);
- linklist delete_head(linklist head);
- linklist delete_rear(linklist head);
-
-
- #endif
- #include"head.h"
-
-
- int main(int argc, const char *argv[])
- {
- //头插
- linklist head=NULL;
- int n;
- printf("please enter n:");
- scanf("%d",&n);
- datatype element;
- for(int i=0;i<n;i++)
- {
- printf("please enter %d element:",i+1);
- scanf("%d",&element);
- //头插
- head=insert_head(head,element);
- //尾插
- // head=insert_rear(head,element);
-
- }
- output(head);
-
- //头删
- head=delete_head(head);
- output(head);
-
- //尾删
- head=delete_rear(head);
- output(head);
-
- return 0;
- }
- #include"head.h"
-
-
- int main(int argc, const char *argv[])
- {
- //头插
- linklist head=NULL;
- int n;
- printf("please enter n:");
- scanf("%d",&n);
- datatype element;
- for(int i=0;i<n;i++)
- {
- printf("please enter %d element:",i+1);
- scanf("%d",&element);
- //头插
- head=insert_head(head,element);
- //尾插
- // head=insert_rear(head,element);
-
- }
- output(head);
-
- //头删
- head=delete_head(head);
- output(head);
-
- //尾删
- head=delete_rear(head);
- output(head);
-
-
-
-
-
-
-
- return 0;
- }
- ubuntu@ubuntu:~/寒假作业/2.3$ cat test.c
- #include"head.h"
-
- linklist creat()
- {
- linklist head=(linklist)malloc(sizeof(struct node));
- if(head==NULL)
- return NULL;
- head->data=0;
- head->next=head;
- return head;
- }
- //头插
- linklist insert_head(linklist head,datatype element)
- {
- linklist s=creat();
- s->data=element;
- if(head==NULL)
- {
- head=s;
- return head;
- }
- linklist p=head;
- while(p->next!=head)
- p=p->next;
- s->next=head;
- head=s;
- p->next=head;
- return head;
- }
-
- void output(linklist head)
- {
- linklist s=head;
- do
- {
- printf("%-4d",s->data);
- s=s->next;
- }while(s!=head);
- puts("");
- }
-
- //尾插
- linklist insert_rear(linklist head,datatype element)
- {
- linklist s=creat();
- s->data=element;
- linklist p=head;
- if(head==NULL)
- {
- head=s;
- return head;
- }
- while(p->next!=head)
- p=p->next;
- p->next=s;
- s->next=head;
-
- return head;
- }
-
- //头删
- linklist delete_head(linklist head)
- {
- if(head==NULL)
- return head;
- linklist del=head;
- linklist p=head;
- while(p->next!=head)
- p=p->next;
- head=head->next;
- p->next=head;
- free(del);
- del=NULL;
- return head;
- }
-
- //尾删
- linklist delete_rear(linklist head)
- {
- if(head==NULL)
- return head;
- linklist p=head;
- if(head->next==head)
- {
- free(head);
- head=NULL;
- return head;
- }
- while(p->next->next!=head)
- p=p->next;
- linklist del=p->next;
- p->next=head;
- free(del);
- del=NULL;
- return head;
- }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。