您的位置:首页 > 其它

链表基本操作

2012-08-02 20:31 218 查看
main.c

#include <stdio.h>

#include <stdlib.h>

#include "list.h"

main(void)

{

int min, max;

char item_choice;

list_t *head;

list_t *new_head;

int n;

printf("------------------------------------\n");

printf("Happy Girls' Day! -_-\n");

printf("------------------------------------\n");

printf("Please input the number of element in the list n:\n");

scanf("%d", &n);

head = createList(n);

printList(head);

item();

while(1)

{

item_choice = getchar();

switch (item_choice)

{

case '1':

printf("Input the element you want to insert: ");

scanf("%d", &n);

head = insertList(head, n);

printf("Now the list: ");

printList(head);

putchar('\n');

item();

break;

case '2':

printf("Input the min and max: ");

scanf("%d%d", &min, &max);

head = delList(head, min, max);

printf("Now the list: ");

printList(head);

putchar('\n');

item();

break;

case '3':

printf("The reversed list: ");

head = reverseList(head);

printList(head);

putchar('\n');

item();

break;

case '4':

printf("The sorted list: ");

head = sortList(head);

printList(head);

putchar('\n');

item();

break;

case '5':

printf("Input element: ");

scanf("%d", &n);

head = insertSortedList(head, n);

printf("Now the list: ");

printList(head);

putchar('\n');

item();

break;

case '6':

printf("Create second list, input number of nodes: ");

scanf("%d", &n);

new_head = createList(n);

new_head = sortList(new_head);

printf("First list: ");

printList(head);

printf("Second list: ");

printList(new_head);

head = mergeTwoSortedList(head, new_head);

printf("Merged list: ");

printList(head);

putchar('\n');

item();

break;

case '0':

clearList(head);

printf("Good luck to you!\n");

return 0;

}

}

return 0;

}

list.h

#ifndef _LIST_H

#define _LIST_H

#include <stdio.h>

#include <stdlib.h>

typedef struct _list

{

int _element;

struct _list *_next;

}list_t;

void item();

void clearList(list_t *head);

list_t* createList(int n);

void printList(list_t *head);

list_t* reverseList(list_t *head);

list_t* delList(list_t *head, int mink, int maxk);

list_t* insertList(list_t *head, int x);

list_t* sortList(list_t *head);

int lengthList(list_t *head);

list_t* insertSortedList(list_t *head, int x);

list_t* mergeTwoSortedList(list_t *head1, list_t *head2);

#endif

list.c
#include "list.h"

/*creat a list: insert at the end of list*/

list_t* createList(int n)

{

list_t *curr, *prev;

list_t *head = NULL;

printf("Please input %d elements:\n", n);

if(n > 0)

{

head = (list_t*)malloc(sizeof(list_t));

if(head == NULL)

{

fprintf(stderr, "error: malloc\n");

exit(0);

}

}

else //n<0, illegal

{

printf("illegal input\n");

return NULL;

}

scanf("%d", &head->_element);

head->_next = NULL;

prev = head;

n = n - 1; //exclude head

while(n--)

{

curr = (list_t*)malloc(sizeof(list_t));

if(curr == NULL)

{

fprintf(stderr, "error: malloc\n");

exit(0);

}

scanf("%d", &curr->_element);

curr->_next = NULL;

prev->_next = curr;

prev = curr;

}

return head;

}

void clearList(list_t *head)

{

list_t *prev = head;

if(prev == NULL)

{

printf("No list\n");

return;

}

list_t *next = head->_next;

free(prev);

while(next != NULL)

{

prev = next;

next = next->_next;

free(prev);

}

}

void printList(list_t *head)

{

list_t *p = head;

if(p == NULL)

{

printf("no element\n");

return;

}

while(p != NULL)

{

printf("%d ", p->_element);

p = p->_next;

}

printf("(length:%d)", lengthList(head));

putchar('\n');

}

list_t* reverseList(list_t *head)

{

list_t *prev;

list_t *curr;

prev = NULL;

curr = head;

while(curr != NULL)

{

head = head->_next;

curr->_next = prev;

prev = curr;

curr = head;

}

head = prev;//put last one point to previous one

return head;

}

list_t* delList(list_t *head, int mink, int maxk)

{

list_t *curr = head;

list_t *prev;

while(curr != NULL)

{

if((head->_element > mink)&&(head->_element < maxk))//if head is deleted

{

head = head->_next;

free(curr);

curr = head;

prev = curr;

}

else if((curr->_element > mink)&&(curr->_element < maxk))

{

prev->_next = curr->_next;

free(curr);

curr = prev->_next;

}

else

{

prev = curr;

curr = curr->_next;

}

}

return head;

}

/*insert one element at the end of list*/

list_t* insertList(list_t *head, int x)

{

list_t *prev;

list_t *curr;

prev = head;

curr = head;

if(curr == NULL) //if list is empty

{

curr = (list_t*)malloc(sizeof(list_t));

if(curr == NULL)

{

printf("error: malloc\n");

exit(0);

}

curr->_element = x;

curr->_next = NULL;

head = curr;

}

else

{

while(curr != NULL) // find end of list

{

prev = curr;

curr = curr->_next;

}

curr = (list_t*)malloc(sizeof(list_t));

if(curr == NULL)

{

printf("error: malloc\n");

exit(0);

}

curr->_element = x;

curr->_next = NULL;

prev->_next = curr;

}

return head;

}

/*sort lists with increment*/

list_t* sortList(list_t *head)

{

list_t *prev_min, *min, *prev_curr, *curr;

list_t *new_head, *new_curr;

new_head = (list_t *)malloc(sizeof(list_t));//the new list has an empty header, simplify operation of insertion for new list

if(new_head == NULL)

{

printf("error: malloc\n");

exit(0);

}

new_head->_element = 0;

new_head->_next = NULL;

new_curr = new_head; //new_curr: last one element of new list

/* original list need 4 pointer: curr & prev_curr used to tranverse; min & prev_min used to keep the minmimum */

prev_min = head;

min = head;

prev_curr = head;

curr = head;

/* remove original unsorted list to another new sorted list */

while(head != NULL) //until the original list is empty

{

min = head;

prev_min = head;

/* traverse all list to find minimum */

while(curr != NULL)

{

if(curr->_element < min->_element)

{

min = curr;

prev_min = prev_curr;

}

prev_curr = curr;

curr = curr->_next;

}

/* find the minimum, then remove it to the new list*/

/*remove the minmun in orginal list*/

if(min == head)//delete head

{

head = head->_next;

}

else

{

prev_min->_next = min->_next;

}

/*place the min into the end of new sorted list*/

while(new_curr->_next != NULL)

new_curr = new_curr->_next;

new_curr->_next = min;

new_curr = min;

new_curr->_next = NULL;

/*another tranverse start*/

curr = head; //

}

/*free the new list header*/

new_curr = new_head;

new_head = new_head->_next;

free(new_curr);

return new_head;

}

/* insert one element into the sorted list with increment*/

list_t* insertSortedList(list_t *head, int x)

{

list_t *prev, *curr;

curr = head;

prev = head;

while(curr != NULL && curr->_element < x)

{

prev = curr;

curr = curr->_next;

}

if(curr == head) //insert at the head of list

{

curr = (list_t*)malloc(sizeof(list_t));

curr->_element = x;

curr->_next = head;

head = curr;

}

else

{

curr = (list_t*)malloc(sizeof(list_t));

curr->_element = x;

curr->_next = prev->_next;

prev->_next = curr;

}

return head;

}

/*merge two sorted list with increment*/

list_t* mergeTwoSortedList(list_t *head1, list_t *head2)

{

list_t *new_head, *new_curr;

new_head = (list_t*)malloc(sizeof(list_t));//new list has a empty header to simplify operations

if(new_head == NULL)

{

printf("error: malloc\n");

exit(0);

}

new_head->_element = 0;

new_head->_next = NULL;

new_curr = new_head;

/*merge two list into another new list*/

while(head1 != NULL && head2 != NULL)

{

if(head1->_element < head2->_element)

{

new_curr->_next = head1; //put head1 into new list

head1 = head1->_next;

new_curr = new_curr->_next;

new_curr->_next = NULL;

}

else

{

new_curr->_next = head2; //put head2 into new list

head2 = head2->_next;

new_curr = new_curr->_next;

new_curr->_next = NULL;

}

}

if(head1 == NULL)

new_curr->_next = head2;

else

new_curr->_next = head1;

/*free new list header*/

new_curr = new_head;

new_head = new_head->_next;

free(new_curr);

return new_head;

}

/*get the length of list*/

int lengthList(list_t *head)

{

int count = 0;

while(head != NULL)

{

count++;

head = head->_next;

}

return count;

}

void item()

{

printf("*************** Welcome to the menu *************\n");

printf("1: Insert one element;\n");

printf("2: Delete one element;\n");

printf("3: Reverse the list;\n");

printf("4: Sort the list;\n");

printf("5: Insert one element into sorted list;\n");

printf("6: Merge two sorted list;\n");

printf("0: Exit.\n");

printf("*************************************************\n");

printf("Select your operation: ");

}

makefile
object = main.o list.o
cc = gcc
flags = -O2 -Wall -g
main:$(object)
$(cc) -o main $(object) $(flags)

#自动推导规则
main.o: list.h

link.o: list.h

.PHONY: clean
clean:
rm calc_main $(object)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息