链表的学习--创建、添加和删除元素

//list.cpp
#include "list.h" #include <stdlib.h> #include <stdio.h> void initList(List *list) { list->first = NULL; } void destroyList(List *list) { Element *e = list->first; Element *next = list->first; list->first = NULL; while (e) { next = e->next; // e->next wird zwischengespeichert e->next = NULL; // und bleibt erreichbar, nachdem e free(e); // freigegeben wurde. e = next; } } void prependToList(List *list, int i) { Element *e = malloc(sizeof(Element)); e->value = i; e->next = list->first; list->first = e; } void printList(List *list) { Element *e; for (e = list->first; e != NULL; e = e->next) { printf("%i ", e->value); } printf("\n"); } size_t sizeOfList(List *list) { Element *e; size_t count = 0; // Iterationsbedingung ist wahr, wenn e != NULL for (e = list->first; e; e = e->next) { count++; } return count; } void appendToList(List *list,int i) { Element *e; e = list->first; if(e==NULL){ e=malloc(sizeof(Element)); e->value=i; e->next = NULL; list->first=e; return; } while(e->next != NULL) { e = e->next; } e->next=malloc(sizeof(Element)); e->next->value = i; e->next->next=NULL; } int removeFirst(List *list) { Element *e; int temp; e = list->first; if(e == NULL){ printf("List is NULL!\n"); return 0; } temp=e->value; list->first = list->first->next; free(e); return temp; } int removeLast(List *list) { Element *e; int temp; e = list->first; if(e == NULL){ printf("List is NULL!\n"); return 0; } if(e->next== NULL) { return removeFirst(list); } while(e->next->next != NULL) { e = e->next; } temp=e->next->value; free(e->next); e->next = NULL; return temp; } void insertAfterPosition(List *list, size_t pos, int i) { Element *e,*g; e = list->first; if(e == NULL){ printf("List is NULL!\n"); return; } if(pos>sizeOfList(list))//new add { printf("pos is larger than size of list!\n"); return; } size_t j; for (j=0; j<pos-1; j++) { e = e->next; } g=malloc(sizeof(Element)); g->value=i; g->next=e->next; e->next=g; } int removeAfterPosition(List *list,size_t pos){ Element *e,*g; int temp; e = list->first; if(e == NULL){ printf("List is NULL!\n"); return 0; } if(pos>sizeOfList(list))//new add { printf("pos is larger than size of list!\n"); return 0; } size_t j; for (j=0; j<pos-1; j++) { e = e->next; } g=e->next; temp=g->value; e->next=e->next->next; free(g); return temp; } void insertSorted(List *list, int i) { Element *e,*g; e = list->first; if(e == NULL){ printf("List is NULL!\n"); return; } if(list->first->next== NULL) { prependToList(list, i); sortList(list); return; } for(e = list->first; e->next != NULL; e = e->next){ if((e->value)>(e->next->value)){ sortList(list); break; } } g=malloc(sizeof(Element)); g->value=i; if(i<=list->first->value)//new add { prependToList(list, i); return; } for(e = list->first; e->next != NULL; e = e->next){ if((e->value<=i)&&(e->next->value>=i)){ g->next=e->next; e->next=g; break; } } if(e->next == NULL)//new add { e->next=g; g->next=NULL; } } void sortList( List *list) { Element *e; int temp; size_t j; size_t size=sizeOfList(list); for (j = size - 1; j > 0; j--) { size_t p = 0; for(e = list->first; e->next != NULL; e = e->next){ p++; if (p > j) break; if((e->value)>(e->next->value)){ temp=e->value; (e->value)=(e->next->value); (e->next->value)=temp; } } } }

 

//list.h
#include <stddef.h> // enthält Definition von size_t struct Element { int value; struct Element *next; }; struct List { struct Element *first; }; typedef struct List List; typedef struct Element Element; void initList(List* list); void destroyList(List *list); int removeFirst(List *list); void appendToList(List *list,int i); void printList(List *list); int removeLast(List *list); size_t sizeOfList(List *list); void insertAfterPosition(List *list, size_t pos, int i); void prependToList(List *list, int value); void insertSorted(List *list, int i); void sortList( List *list); int removeAfterPosition(List *list,size_t pos);

 

//prog.cpp
#include "list.h" #include <stdio.h> int main() { struct List list; int appnum,insertpos,removepos,insertnum,insertsortnum; initList(&list); int i = 0; for (i=5; i < 15; ++i) { prependToList(&list, i); } printList(&list); printf("Please input append number:"); scanf("%d",&appnum); printf("Size of list: %zu\n", sizeOfList(&list)); appendToList(&list, appnum); printf("appendToList:\n"); printList(&list); printf("Size of list: %zu\n", sizeOfList(&list)); removeFirst(&list); printf("removeFirst:\n"); printList(&list); printf("Size of list: %zu\n", sizeOfList(&list)); removeLast(&list); printf("removeLast:\n"); printList(&list); printf("Size of list: %zu\n", sizeOfList(&list)); printf("Please input insert position and number:"); scanf("%d %d",&insertpos,&insertnum); insertAfterPosition(&list,insertpos,insertnum); printf("insertAfterPosition %d:\n",insertpos); printList(&list); printf("Please input remove position:"); scanf("%d",&removepos); removeAfterPosition(&list,removepos); printf("removeAfterPosition %d:\n",removepos); printList(&list); printf("Please input insertsorted number:"); scanf("%d",&insertsortnum); printf("Size of list: %zu\n", sizeOfList(&list)); insertSorted(&list,insertsortnum); printf("insertSorted:\n"); printList(&list); printf("Size of list: %zu\n", sizeOfList(&list)); destroyList(&list); printf("Size of list: %zu\n", sizeOfList(&list)); }

 

转载于:https://www.cnblogs.com/lishuai0214/p/4309731.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值