0 / 0 / 1
Регистрация: 22.11.2017
Сообщений: 42
|
|
1
|
Объединение списков
04.03.2018, 02:03. Показов 1472. Ответов 0
Обьединить списки помогите найти ошибку
C | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
| #include <stdio.h>
#include <stdlib.h>
struct listNode {
char data;
struct listNode *nextPtr;
};
typedef struct listNode ListNode;
typedef ListNode* ListNodePtr;
void insert( ListNodePtr *sPtr, char value );
int isEmpty( ListNodePtr sPtr );
void printList( ListNodePtr currentPtr );
void instructions( void );
ListNodePtr MergeLists(ListNodePtr list1, ListNodePtr list2);
int main( void )
{
ListNodePtr startPtr = NULL;
ListNodePtr startPtr2 = NULL;
int choice;
char item;
instructions();
printf( "? " );
scanf( "%d", &choice );
/* loop while user does not choose 3 */
while ( choice != 4 ) {
switch ( choice ) {
case 1:
printf( "Enter a character: " );
scanf( "\n%c", &item );
insert( &startPtr, item ); /* insert item in list */
printList( startPtr );
break;
case 2:
printf( "Enter a character2sp: " );
scanf( "\n%c", &item );
insert( &startPtr2, item ); /* insert item in list */
printList(startPtr2);
break;
case 5:
MergeLists(startPtr,startPtr2);
break;
default:
printf( "Invalid choice.\n\n" );
instructions();
break;
} /* end switch */
printf( "? " );
scanf( "%d", &choice );
} /* end while */
printf( "End of run.\n" );
return 0; /* indicates successful termination */
} /* end main */
/* display program instructions to user */
void instructions( void )
{
printf( "Enter your choice:\n"
"1 to insert an element into the list.\n"
"2 to insert an element into the list2.\n"
"4 to end.\n" );
} /* end function instructions */
/* Insert a new value into the list in sorted order */
void insert( ListNodePtr *sPtr, char value )
{
ListNodePtr newPtr; /* pointer to new node */
ListNodePtr previousPtr; /* pointer to previous node in list */
ListNodePtr currentPtr; /* pointer to current node in list */
newPtr = malloc( sizeof( ListNode ) ); /* create node */
if ( newPtr != NULL ) { /* is space available */
newPtr->data = value; /* place value in node */
newPtr->nextPtr = NULL; /* node does not link to another node */
previousPtr = NULL;
currentPtr = *sPtr;
/* loop to find the correct location in the list */
while ( currentPtr != NULL && value > currentPtr->data ) {
previousPtr = currentPtr;
/* walk to ... */
currentPtr = currentPtr->nextPtr; /* ... next node */
} /* end while */
/* insert new node at beginning of list */
if ( previousPtr == NULL ) {
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
} /* end if */
else { /* insert new node between previousPtr and currentPtr */
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
} /* end else */
} /* end if */
else {
printf( "%c not inserted. No memory available.\n", value );
} /* end else */
} /* end function insert */
int isEmpty( ListNodePtr sPtr )
{
return sPtr == NULL;
}
/* Print the list */
void printList( ListNodePtr currentPtr )
{
if ( currentPtr == NULL ) {
printf( "List is empty.\n\n" );
}
else {
printf( "The list is:\n" );
/* while not the end of the list */
while ( currentPtr != NULL ) {
printf( "%c --> ", currentPtr->data );
currentPtr = currentPtr->nextPtr;
} /* end while */
printf( "NULL\n\n" );
} /* end else */
} /* end function printList */
ListNodePtr MergeLists(ListNodePtr list1, ListNodePtr list2) {
ListNodePtr head;
if (list1 == NULL) return list2;
if (list2 == NULL) return list1;
if (list1->data > list2->data) {
head = list1;
} else {
head = list2;
list2 = list1;
list1 = head;
}
while(list1->nextPtr != NULL) {
if (list1->nextPtr->data > list2->data) {
ListNodePtr tmp = list1->nextPtr;
list1->nextPtr = list2;
list2 = tmp;
}
list1 = list1->nextPtr;
}
list1->nextPtr = list2;
return head;
} |
|
0
|